1 | /*
|
---|
2 | * Model ... the domain model
|
---|
3 | * Element ... the table to add new elements to
|
---|
4 | */
|
---|
5 | function addExtension(model, element) {
|
---|
6 | /* Specify the name of your row entry */
|
---|
7 | var name = 'TSP Solution';
|
---|
8 |
|
---|
9 | /* Create the content of your row by using the model */
|
---|
10 | var content = $('<div/>');
|
---|
11 |
|
---|
12 | // look up coordinates for TSP
|
---|
13 | var coords = [];
|
---|
14 |
|
---|
15 | coords = _.find(model.get('results'), function(elem) {
|
---|
16 | return elem.Name == "Best TSP Solution";
|
---|
17 | }).Value;
|
---|
18 |
|
---|
19 | // draw TSP
|
---|
20 | var canvasWidth = 400;
|
---|
21 | var canvasHeight = 400;
|
---|
22 | var radius = 6;
|
---|
23 | var canvas = $('<canvas/>').prop({
|
---|
24 | width: canvasWidth,
|
---|
25 | height: canvasHeight
|
---|
26 | }).appendTo(content);
|
---|
27 |
|
---|
28 | var ctx = undefined;
|
---|
29 | if (canvas[0].getContext == undefined) {
|
---|
30 | ctx = G_vmlCanvasManager.initElement(canvas[0]).getContext("2d");
|
---|
31 | } else {
|
---|
32 | ctx = canvas[0].getContext("2d");
|
---|
33 | }
|
---|
34 | ctx.fillStyle = "#FFFFFF";
|
---|
35 | ctx.fillRect(0, 0, canvasWidth, canvasHeight);
|
---|
36 | ctx.fillStyle = "#000000";
|
---|
37 | if (coords && coords.length > 0) {
|
---|
38 | // determine max/min boundings
|
---|
39 | var xMin = Number.MAX_VALUE;
|
---|
40 | var xMax = Number.MIN_VALUE;
|
---|
41 | var yMin = Number.MAX_VALUE;
|
---|
42 | var yMax = Number.MIN_VALUE;
|
---|
43 | for (var i = 0; i < coords.length; i++) {
|
---|
44 | if (xMin > coords[i][0]) xMin = coords[i][0];
|
---|
45 | if (yMin > coords[i][1]) yMin = coords[i][1];
|
---|
46 | if (xMax < coords[i][0]) xMax = coords[i][0];
|
---|
47 | if (yMax < coords[i][1]) yMax = coords[i][1];
|
---|
48 | }
|
---|
49 |
|
---|
50 | var border = 20;
|
---|
51 | var xStep = 1;
|
---|
52 | if (xMax != xMin) xStep = (canvasWidth - 2 * border) / (xMax - xMin);
|
---|
53 | var yStep = 1;
|
---|
54 | if (yMax != yMin) yStep = (canvasHeight - 2 * border) / (yMax - yMin);
|
---|
55 |
|
---|
56 | ctx.beginPath();
|
---|
57 | var x = Math.round(border + (coords[0][0] - xMin) * xStep);
|
---|
58 | var y = Math.round(canvasHeight - (border + (coords[0][1] - yMin) * yStep));
|
---|
59 | ctx.moveTo(x, y);
|
---|
60 | for (var i = 1; i < coords.length + 1; i++) {
|
---|
61 | var x = Math.round(border + (coords[i % coords.length][0] - xMin) * xStep);
|
---|
62 | var y = Math.round(canvasHeight - (border + (coords[i % coords.length][1] - yMin) * yStep));
|
---|
63 | ctx.lineTo(x, y);
|
---|
64 | }
|
---|
65 | ctx.closePath();
|
---|
66 | ctx.stroke();
|
---|
67 | for (var i = 0; i < coords.length + 1; i++) {
|
---|
68 | var x = Math.round(border + (coords[i % coords.length][0] - xMin) * xStep);
|
---|
69 | var y = Math.round(canvasHeight - (border + (coords[i % coords.length][1] - yMin) * yStep));
|
---|
70 | ctx.beginPath();
|
---|
71 | ctx.arc(x, y, radius, 0, 2 * Math.PI);
|
---|
72 | ctx.closePath();
|
---|
73 | ctx.fill();
|
---|
74 | }
|
---|
75 | } else {
|
---|
76 | ctx.font = "20px Arial";
|
---|
77 | var txt = "No coordinates defined or in wrong format.";
|
---|
78 | var w = ctx.measureText(txt).width;
|
---|
79 | ctx.fillText("No coordinates defined or in wrong format.", (canvasWidth - w) / 2, (canvasHeight - 20) / 2);
|
---|
80 | }
|
---|
81 |
|
---|
82 | /* Creates a new row and adds it to element */
|
---|
83 | var row = $('<tr/>');
|
---|
84 | var nameTd = $('<td/>').text(name).appendTo(row);
|
---|
85 | var contentTd = $('<td/>').appendTo(row);
|
---|
86 | content.appendTo(contentTd);
|
---|
87 | row.appendTo(element);
|
---|
88 | } |
---|