|
15 | 15 | graph.add_point = function(p) { |
16 | 16 | add_point(p); |
17 | 17 | }; |
| 18 | + |
| 19 | + graph.add_canvas_point = function(p) { |
| 20 | + add_canvas_point(p); |
| 21 | + }; |
| 22 | + |
| 23 | + graph.initialize_canvas = function() { |
| 24 | + initialize_canvas(); |
| 25 | + }; |
| 26 | + |
| 27 | + graph.show_canvas = function() { |
| 28 | + show_canvas(); |
| 29 | + }; |
| 30 | + |
| 31 | + graph.hide_canvas = function() { |
| 32 | + hide_canvas(); |
| 33 | + }; |
| 34 | + |
18 | 35 |
|
19 | 36 | var size = [500, 350], |
20 | 37 | padding = [20, 30, 20, 40], // top right bottom left |
|
42 | 59 | downy = Math.NaN, |
43 | 60 | dragged = null, |
44 | 61 | selected = points[0]; |
| 62 | + |
| 63 | + var plot, gcanvas, gctx; |
| 64 | + |
| 65 | + var chart = document.getElementById("chart"); |
45 | 66 |
|
46 | 67 | var vis = d3.select("#chart").append("svg:svg") |
47 | 68 | .attr("width", size[0] + padding[3] + padding[1]) |
|
70 | 91 | .attr("class", "line") |
71 | 92 | .attr("d", line(points)); |
72 | 93 |
|
| 94 | + initialize_canvas(); |
| 95 | + |
73 | 96 | // variables for speeding up dynamic plotting |
74 | 97 | var line_path = vis.select("path")[0][0]; |
75 | 98 | var line_seglist = line_path.pathSegList; |
|
118 | 141 | points.push(point); |
119 | 142 | var newx = x.call(self, len, len); |
120 | 143 | var newy = y.call(self, p, len); |
| 144 | + line_seglist.appendItem(line_path.createSVGPathSegLinetoAbs(newx, newy)); |
| 145 | + }; |
| 146 | + |
| 147 | + function add_canvas_point(p) { |
| 148 | + var len = points.length; |
| 149 | + var oldx = x.call(self, len-1, len-1); |
| 150 | + var oldy = y.call(self, points[len-1].y, len-1); |
| 151 | + var point = { x: len, y: p }; |
| 152 | + points.push(point); |
| 153 | + var newx = x.call(self, len, len); |
| 154 | + var newy = y.call(self, p, len); |
| 155 | + gctx.beginPath(); |
| 156 | + gctx.moveTo(oldx, oldy); |
| 157 | + gctx.lineTo(newx, newy); |
| 158 | + gctx.closePath(); |
| 159 | + gctx.stroke(); |
| 160 | + // FIXME: FireFox bug |
| 161 | + }; |
| 162 | + |
| 163 | + function show_canvas() { |
| 164 | + gcanvas.style.zIndex = 100; |
| 165 | + }; |
121 | 166 |
|
122 | | - // adding circle points with dom manipulation |
123 | | - // var c2 = cpoint.cloneNode(false); |
124 | | - // c2.setAttribute("cx", newx); |
125 | | - // c2.setAttribute("cy", newy); |
126 | | - // vis_node.appendChild(c2); |
| 167 | + function hide_canvas() { |
| 168 | + gcanvas.style.zIndex = -100; |
| 169 | + }; |
127 | 170 |
|
128 | | - // adding circle points with d3 |
129 | | - // vis.append("svg:circle").attr('cx',newx).attr('cy',newy).attr('r',1) |
130 | | - |
131 | | - // adding new line segments with dom manipulation |
132 | | - line_seglist.appendItem(line_path.createSVGPathSegLinetoAbs(newx, newy)); |
| 171 | + function initialize_canvas() { |
| 172 | + plot = {} |
| 173 | + plot.rect = chart.children[0].getElementsByTagName("rect")[0] |
| 174 | + plot.width = plot.rect.width['baseVal'].value |
| 175 | + plot.height = plot.rect.height['baseVal'].value |
| 176 | + plot.left = plot.rect.getCTM().e |
| 177 | + plot.top = plot.rect.getCTM().f |
133 | 178 |
|
134 | | - // adding new line segments with d3 and custom attribute generation |
135 | | - // vis.select("path").attr("d", generate_path_attribute([point], x, y)); |
| 179 | + gcanvas = document.createElement('canvas'); |
| 180 | + chart.appendChild(gcanvas); |
| 181 | + gcanvas.style.position = 'absolute' |
| 182 | + gcanvas.width = plot.width; |
| 183 | + gcanvas.height = plot.height; |
| 184 | + gcanvas.offsetLeft = plot.left; |
| 185 | + gcanvas.offsetTop = plot.top; |
| 186 | + gcanvas.style.left = plot.left + 'px' |
| 187 | + gcanvas.style.top = plot.top + 'px' |
| 188 | + gctx = gcanvas.getContext( '2d' ); |
| 189 | + gctx.globalCompositeOperation = "destination-atop"; |
| 190 | + gctx.lineWidth = 1; |
| 191 | + gctx.fillStyle = "rgba(0,255,0, 0.05)"; |
| 192 | + gctx.fillRect(0, 0, gcanvas.width, gcanvas.height); |
| 193 | + gctx.strokeStyle = "rgba(255,65,0, 1.0)"; |
| 194 | + gcanvas.style.border = 'solid 1px red' |
| 195 | + gcanvas.style.zIndex = -100 |
136 | 196 | }; |
137 | | - |
| 197 | + |
138 | 198 | function update() { |
139 | 199 | var lines = vis.select("path").attr("d", line(points)); |
140 | 200 |
|
|
0 commit comments