|
1 | 1 | (function($){
|
2 | 2 |
|
| 3 | + function computeBoundsAndJustify(ctx, nodes, width, height) { |
| 4 | + var borderx = 20, bordery = 20, r = 7, h = 13; |
| 5 | + ctx.font = '13px Arial'; |
| 6 | + var bws = [], xs = [], ys = []; |
| 7 | + $.each(nodes, function (index, node) { |
| 8 | + var w = ctx.measureText(node.label).width; |
| 9 | + node.labelWidth = w; |
| 10 | + node.bw = w + r + r; |
| 11 | + node.bh = h + r + r; |
| 12 | + node.br = r; |
| 13 | + bws.push(node.bw); |
| 14 | + xs.push(node.x); |
| 15 | + ys.push(node.y); |
| 16 | + }); |
| 17 | + var maxbw = Math.max.apply(Math, bws); |
| 18 | + var maxbh = h + r + r; |
| 19 | + var minx = Math.min.apply(Math, xs); |
| 20 | + var maxx = Math.max.apply(Math, xs); |
| 21 | + var miny = Math.min.apply(Math, ys); |
| 22 | + var maxy = Math.max.apply(Math, ys); |
| 23 | + var bx = borderx + maxbw / 2; |
| 24 | + var by = bordery + maxbh / 2; |
| 25 | + if(minx == maxx) { |
| 26 | + minx -= 1; |
| 27 | + maxx += 1; |
| 28 | + } |
| 29 | + if(miny == maxy) { |
| 30 | + miny -= 1; |
| 31 | + maxy += 1; |
| 32 | + } |
| 33 | + $.each(nodes, function (index, node) { |
| 34 | + node.x = bx + (node.x - minx) * (width - 2 * bx) / (maxx - minx); |
| 35 | + node.y = by + (node.y - miny) * (height - 2 * by) / (maxy - miny); |
| 36 | + node.bx = node.x - node.bw / 2; |
| 37 | + node.by = node.y - node.bh / 2; |
| 38 | + }); |
| 39 | + } |
| 40 | + |
3 | 41 | function drawNode(ctx, node) {
|
4 |
| - ctx.textBaseline = 'middle'; |
5 | 42 | ctx.font = '13px Arial';
|
6 |
| - var measured = ctx.measureText(node.label); |
7 |
| - var w = measured.width, h = 13; |
8 |
| - var x = node.x - w / 2, y = node.y - h / 2, r = 7; |
9 |
| - ctx.fillText(node.label, x, y + h / 2); |
| 43 | + ctx.textBaseline = 'middle'; |
| 44 | + var x = node.bx, y = node.by, r = node.br, w = node.bw, h = node.bh; |
| 45 | + ctx.fillText(node.label, x + r, y + h / 2); |
10 | 46 | ctx.lineWidth = 2;
|
11 |
| - ctx.beginPath(); |
12 |
| - ctx.moveTo(x, y - r); |
13 |
| - ctx.lineTo(x + w, y - r); |
14 |
| - ctx.quadraticCurveTo(x + w + r, y - r, x + w + r, y); |
15 |
| - ctx.lineTo(x + w + r, y + h); |
16 |
| - ctx.quadraticCurveTo(x + w + r, y + h + r, x + w, y + h + r); |
17 |
| - ctx.lineTo(x, y + h + r); |
18 |
| - ctx.quadraticCurveTo(x - r, y + h + r, x - r, y + h); |
19 |
| - ctx.lineTo(x - r, y); |
20 |
| - ctx.quadraticCurveTo(x - r, y - r, x, y - r); |
| 47 | + ctx.beginPath(); |
| 48 | + ctx.moveTo(x + r, y); |
| 49 | + ctx.lineTo(x + w - r, y); |
| 50 | + ctx.quadraticCurveTo(x + w, y, x + w, y + r); |
| 51 | + ctx.lineTo(x + w, y + h - r); |
| 52 | + ctx.quadraticCurveTo(x + w, y + h, x + w - r, y + h); |
| 53 | + ctx.lineTo(x + r, y + h); |
| 54 | + ctx.quadraticCurveTo(x, y + h, x, y + h - r); |
| 55 | + ctx.lineTo(x, y + r); |
| 56 | + ctx.quadraticCurveTo(x, y, x + r, y); |
21 | 57 | ctx.closePath();
|
22 | 58 | ctx.stroke();
|
23 |
| - |
24 |
| - node.bx = x - r; |
25 |
| - node.by = y - r; |
26 |
| - node.bw = w + r + r; |
27 |
| - node.bh = h + r + r; |
28 | 59 | }
|
29 | 60 |
|
30 | 61 | function lerp(a, b, t) {
|
|
173 | 204 | node.x = Math.cos(radian);
|
174 | 205 | node.y = Math.sin(radian);
|
175 | 206 | });
|
176 |
| - if (graph.nodes.length == 1) { |
177 |
| - graph.minx = 1; |
178 |
| - graph.maxx = 1; |
179 |
| - graph.miny = 1; |
180 |
| - graph.maxy = 1; |
181 |
| - } else { |
182 |
| - graph.minx = -1; |
183 |
| - graph.maxx = 1; |
184 |
| - graph.miny = -1; |
185 |
| - graph.maxy = 1; |
186 |
| - } |
187 | 207 | }
|
188 | 208 |
|
189 | 209 | $(document).ready(function() {
|
190 | 210 | $('.trac-workflow-graph').each(function (index) {
|
191 | 211 | var data = window['graph_' + this.id.slice(-8)];
|
192 |
| - var borderx = 50, bordery = 50; |
193 | 212 | var width = data.width, height = data.height;
|
194 | 213 | var nodes = [], actions = [], edges = [];
|
195 | 214 | for (var i = 0; i < data.nodes.length; ++i)
|
|
218 | 237 |
|
219 | 238 | var graph = {nodes: nodes, edges: edges};
|
220 | 239 | layoutCircular(graph);
|
221 |
| - |
222 |
| - if(graph.minx == graph.maxx) { |
223 |
| - graph.minx -= 1; |
224 |
| - graph.maxx += 1; |
225 |
| - } |
226 |
| - if(graph.miny == graph.maxy) { |
227 |
| - graph.miny -= 1; |
228 |
| - graph.maxy += 1; |
229 |
| - } |
| 240 | + computeBoundsAndJustify(ctx, nodes, width, height); |
230 | 241 |
|
231 | 242 | $.each(nodes, function (index, node) {
|
232 |
| - node.x = borderx + (node.x - graph.minx) * (width - 2 * borderx) |
233 |
| - / (graph.maxx - graph.minx); |
234 |
| - node.y = bordery + (node.y - graph.miny) * (height - 2 * bordery) |
235 |
| - / (graph.maxy - graph.miny); |
236 | 243 | drawNode(ctx, node);
|
237 | 244 | });
|
238 | 245 | $.each(edges, function (index, edge) {
|
|
0 commit comments