Commit | Line | Data |
---|---|---|
6a1aa64f DV |
1 | // Copyright 2006 Dan Vanderkam (danvdk@gmail.com) |
2 | // All Rights Reserved. | |
3 | ||
4 | /** | |
3df0ccf0 DV |
5 | * @fileoverview Based on PlotKit, but modified to meet the needs of dygraphs. |
6 | * In particular, support for: | |
7 | * - grid overlays | |
8 | * - error bars | |
9 | * - dygraphs attribute system | |
6a1aa64f DV |
10 | */ |
11 | ||
6a1aa64f | 12 | /** |
3df0ccf0 | 13 | * Creates a new DygraphLayout object. |
6a1aa64f | 14 | * @param {Object} options Options for PlotKit.Layout |
285a6bda | 15 | * @return {Object} The DygraphLayout object |
6a1aa64f | 16 | */ |
efe0829a DV |
17 | DygraphLayout = function(dygraph, options) { |
18 | this.dygraph_ = dygraph; | |
19 | this.options = {}; // TODO(danvk): remove, use attr_ instead. | |
fc80a396 | 20 | Dygraph.update(this.options, options ? options : {}); |
efe0829a | 21 | this.datasets = new Array(); |
6a1aa64f | 22 | }; |
efe0829a DV |
23 | |
24 | DygraphLayout.prototype.attr_ = function(name) { | |
25 | return this.dygraph_.attr_(name); | |
26 | }; | |
27 | ||
28 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
29 | this.datasets[setname] = set_xy; | |
30 | }; | |
31 | ||
32 | DygraphLayout.prototype.evaluate = function() { | |
33 | this._evaluateLimits(); | |
34 | this._evaluateLineCharts(); | |
35 | this._evaluateLineTicks(); | |
36 | }; | |
37 | ||
38 | DygraphLayout.prototype._evaluateLimits = function() { | |
39 | this.minxval = this.maxxval = null; | |
f6401bf6 DV |
40 | if (this.options.dateWindow) { |
41 | this.minxval = this.options.dateWindow[0]; | |
42 | this.maxxval = this.options.dateWindow[1]; | |
43 | } else { | |
44 | for (var name in this.datasets) { | |
45 | if (!this.datasets.hasOwnProperty(name)) continue; | |
46 | var series = this.datasets[name]; | |
47 | var x1 = series[0][0]; | |
48 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
85b99f0b | 49 | |
f6401bf6 DV |
50 | var x2 = series[series.length - 1][0]; |
51 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
52 | } | |
efe0829a DV |
53 | } |
54 | this.xrange = this.maxxval - this.minxval; | |
55 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); | |
56 | ||
57 | this.minyval = this.options.yAxis[0]; | |
58 | this.maxyval = this.options.yAxis[1]; | |
59 | this.yrange = this.maxyval - this.minyval; | |
60 | this.yscale = (this.yrange != 0 ? 1/this.yrange : 1.0); | |
61 | }; | |
62 | ||
63 | DygraphLayout.prototype._evaluateLineCharts = function() { | |
64 | // add all the rects | |
65 | this.points = new Array(); | |
66 | for (var setName in this.datasets) { | |
85b99f0b DV |
67 | if (!this.datasets.hasOwnProperty(setName)) continue; |
68 | ||
69 | var dataset = this.datasets[setName]; | |
70 | for (var j = 0; j < dataset.length; j++) { | |
71 | var item = dataset[j]; | |
72 | var point = { | |
ff00d3e2 | 73 | // TODO(danvk): here |
85b99f0b DV |
74 | x: ((parseFloat(item[0]) - this.minxval) * this.xscale), |
75 | y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale), | |
76 | xval: parseFloat(item[0]), | |
77 | yval: parseFloat(item[1]), | |
78 | name: setName | |
79 | }; | |
80 | ||
81 | // limit the x, y values so they do not overdraw | |
82 | if (point.y <= 0.0) { | |
83 | point.y = 0.0; | |
84 | } | |
85 | if (point.y >= 1.0) { | |
86 | point.y = 1.0; | |
87 | } | |
9ac5e4ae | 88 | // if ((point.x >= 0.0) && (point.x <= 1.0)) { |
85b99f0b | 89 | this.points.push(point); |
9ac5e4ae | 90 | // } |
85b99f0b | 91 | } |
efe0829a DV |
92 | } |
93 | }; | |
94 | ||
95 | DygraphLayout.prototype._evaluateLineTicks = function() { | |
96 | this.xticks = new Array(); | |
97 | for (var i = 0; i < this.options.xTicks.length; i++) { | |
98 | var tick = this.options.xTicks[i]; | |
99 | var label = tick.label; | |
100 | var pos = this.xscale * (tick.v - this.minxval); | |
101 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
102 | this.xticks.push([pos, label]); | |
103 | } | |
104 | } | |
105 | ||
106 | this.yticks = new Array(); | |
107 | for (var i = 0; i < this.options.yTicks.length; i++) { | |
108 | var tick = this.options.yTicks[i]; | |
109 | var label = tick.label; | |
110 | var pos = 1.0 - (this.yscale * (tick.v - this.minyval)); | |
111 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
112 | this.yticks.push([pos, label]); | |
113 | } | |
114 | } | |
115 | }; | |
116 | ||
6a1aa64f DV |
117 | |
118 | /** | |
119 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
120 | * @private | |
121 | */ | |
285a6bda | 122 | DygraphLayout.prototype.evaluateWithError = function() { |
6a1aa64f DV |
123 | this.evaluate(); |
124 | if (!this.options.errorBars) return; | |
125 | ||
126 | // Copy over the error terms | |
127 | var i = 0; // index in this.points | |
128 | for (var setName in this.datasets) { | |
85b99f0b DV |
129 | if (!this.datasets.hasOwnProperty(setName)) continue; |
130 | var j = 0; | |
131 | var dataset = this.datasets[setName]; | |
132 | for (var j = 0; j < dataset.length; j++, i++) { | |
133 | var item = dataset[j]; | |
134 | var xv = parseFloat(item[0]); | |
135 | var yv = parseFloat(item[1]); | |
136 | ||
137 | if (xv == this.points[i].xval && | |
138 | yv == this.points[i].yval) { | |
139 | this.points[i].errorMinus = parseFloat(item[2]); | |
140 | this.points[i].errorPlus = parseFloat(item[3]); | |
141 | } | |
142 | } | |
6a1aa64f DV |
143 | } |
144 | }; | |
145 | ||
146 | /** | |
147 | * Convenience function to remove all the data sets from a graph | |
148 | */ | |
285a6bda | 149 | DygraphLayout.prototype.removeAllDatasets = function() { |
6a1aa64f DV |
150 | delete this.datasets; |
151 | this.datasets = new Array(); | |
152 | }; | |
153 | ||
154 | /** | |
155 | * Change the values of various layout options | |
156 | * @param {Object} new_options an associative array of new properties | |
157 | */ | |
285a6bda | 158 | DygraphLayout.prototype.updateOptions = function(new_options) { |
fc80a396 | 159 | Dygraph.update(this.options, new_options ? new_options : {}); |
6a1aa64f DV |
160 | }; |
161 | ||
162 | // Subclass PlotKit.CanvasRenderer to add: | |
163 | // 1. X/Y grid overlay | |
164 | // 2. Ability to draw error bars (if required) | |
165 | ||
166 | /** | |
167 | * Sets some PlotKit.CanvasRenderer options | |
168 | * @param {Object} element The canvas to attach to | |
285a6bda | 169 | * @param {Layout} layout The DygraphLayout object for this graph. |
6a1aa64f DV |
170 | * @param {Object} options Options to pass on to CanvasRenderer |
171 | */ | |
9317362d DV |
172 | DygraphCanvasRenderer = function(dygraph, element, layout, options) { |
173 | // TODO(danvk): remove options, just use dygraph.attr_. | |
9317362d | 174 | this.dygraph_ = dygraph; |
fbe31dc8 DV |
175 | |
176 | // default options | |
177 | this.options = { | |
f474c2a3 DV |
178 | "strokeWidth": 0.5, |
179 | "drawXAxis": true, | |
180 | "drawYAxis": true, | |
181 | "axisLineColor": "black", | |
182 | "axisLineWidth": 0.5, | |
183 | "axisTickSize": 3, | |
184 | "axisLabelColor": "black", | |
185 | "axisLabelFont": "Arial", | |
186 | "axisLabelFontSize": 9, | |
187 | "axisLabelWidth": 50, | |
188 | "drawYGrid": true, | |
189 | "drawXGrid": true, | |
43af96e7 | 190 | "gridLineColor": "rgb(128,128,128)", |
e7746234 EC |
191 | "fillAlpha": 0.15, |
192 | "underlayCallback": null | |
fbe31dc8 | 193 | }; |
fc80a396 | 194 | Dygraph.update(this.options, options); |
6a1aa64f | 195 | |
fbe31dc8 | 196 | this.layout = layout; |
b0c3b730 | 197 | this.element = element; |
fbe31dc8 DV |
198 | this.container = this.element.parentNode; |
199 | ||
fbe31dc8 DV |
200 | this.height = this.element.height; |
201 | this.width = this.element.width; | |
202 | ||
203 | // --- check whether everything is ok before we return | |
204 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
205 | throw "Canvas is not supported."; | |
206 | ||
207 | // internal state | |
208 | this.xlabels = new Array(); | |
209 | this.ylabels = new Array(); | |
210 | ||
211 | this.area = { | |
212 | x: this.options.yAxisLabelWidth + 2 * this.options.axisTickSize, | |
213 | y: 0 | |
214 | }; | |
215 | this.area.w = this.width - this.area.x - this.options.rightGap; | |
216 | this.area.h = this.height - this.options.axisLabelFontSize - | |
217 | 2 * this.options.axisTickSize; | |
218 | ||
b0c3b730 DV |
219 | this.container.style.position = "relative"; |
220 | this.container.style.width = this.width + "px"; | |
fbe31dc8 DV |
221 | }; |
222 | ||
223 | DygraphCanvasRenderer.prototype.clear = function() { | |
224 | if (this.isIE) { | |
225 | // VML takes a while to start up, so we just poll every this.IEDelay | |
226 | try { | |
227 | if (this.clearDelay) { | |
228 | this.clearDelay.cancel(); | |
229 | this.clearDelay = null; | |
230 | } | |
231 | var context = this.element.getContext("2d"); | |
232 | } | |
233 | catch (e) { | |
76171648 | 234 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
235 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
236 | this.clearDelay.addCallback(bind(this.clear, this)); | |
237 | return; | |
238 | } | |
239 | } | |
240 | ||
241 | var context = this.element.getContext("2d"); | |
242 | context.clearRect(0, 0, this.width, this.height); | |
243 | ||
2160ed4a | 244 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 DV |
245 | var el = this.xlabels[i]; |
246 | el.parentNode.removeChild(el); | |
2160ed4a DV |
247 | } |
248 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 DV |
249 | var el = this.ylabels[i]; |
250 | el.parentNode.removeChild(el); | |
2160ed4a | 251 | } |
fbe31dc8 DV |
252 | this.xlabels = new Array(); |
253 | this.ylabels = new Array(); | |
254 | }; | |
255 | ||
256 | ||
257 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
258 | var canvas = null; | |
259 | try { | |
21d3323f | 260 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 261 | canvas = document.createElement("canvas"); |
fbe31dc8 | 262 | else |
b0c3b730 | 263 | canvas = canvasName; |
fbe31dc8 DV |
264 | var context = canvas.getContext("2d"); |
265 | } | |
266 | catch (e) { | |
267 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
268 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
269 | if ((!ie) || (ie[1] < 6) || (opera)) | |
270 | return false; | |
271 | return true; | |
272 | } | |
273 | return true; | |
6a1aa64f | 274 | }; |
6a1aa64f DV |
275 | |
276 | /** | |
277 | * Draw an X/Y grid on top of the existing plot | |
278 | */ | |
285a6bda | 279 | DygraphCanvasRenderer.prototype.render = function() { |
6a1aa64f DV |
280 | // Draw the new X/Y grid |
281 | var ctx = this.element.getContext("2d"); | |
e7746234 EC |
282 | |
283 | if (this.options.underlayCallback) { | |
284 | this.options.underlayCallback(ctx, this.area, this.layout); | |
285 | } | |
286 | ||
6a1aa64f DV |
287 | if (this.options.drawYGrid) { |
288 | var ticks = this.layout.yticks; | |
289 | ctx.save(); | |
f2f24402 | 290 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
291 | ctx.lineWidth = this.options.axisLineWidth; |
292 | for (var i = 0; i < ticks.length; i++) { | |
293 | var x = this.area.x; | |
294 | var y = this.area.y + ticks[i][0] * this.area.h; | |
295 | ctx.beginPath(); | |
296 | ctx.moveTo(x, y); | |
297 | ctx.lineTo(x + this.area.w, y); | |
298 | ctx.closePath(); | |
299 | ctx.stroke(); | |
300 | } | |
301 | } | |
302 | ||
303 | if (this.options.drawXGrid) { | |
304 | var ticks = this.layout.xticks; | |
305 | ctx.save(); | |
f2f24402 | 306 | ctx.strokeStyle = this.options.gridLineColor; |
6a1aa64f DV |
307 | ctx.lineWidth = this.options.axisLineWidth; |
308 | for (var i=0; i<ticks.length; i++) { | |
309 | var x = this.area.x + ticks[i][0] * this.area.w; | |
310 | var y = this.area.y + this.area.h; | |
311 | ctx.beginPath(); | |
312 | ctx.moveTo(x, y); | |
313 | ctx.lineTo(x, this.area.y); | |
314 | ctx.closePath(); | |
315 | ctx.stroke(); | |
316 | } | |
317 | } | |
2ce09b19 DV |
318 | |
319 | // Do the ordinary rendering, as before | |
2ce09b19 | 320 | this._renderLineChart(); |
fbe31dc8 DV |
321 | this._renderAxis(); |
322 | }; | |
323 | ||
324 | ||
325 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
326 | if (!this.options.drawXAxis && !this.options.drawYAxis) | |
327 | return; | |
328 | ||
329 | var context = this.element.getContext("2d"); | |
330 | ||
34fedff8 DV |
331 | var labelStyle = { |
332 | "position": "absolute", | |
333 | "fontSize": this.options.axisLabelFontSize + "px", | |
334 | "zIndex": 10, | |
f474c2a3 | 335 | "color": this.options.axisLabelColor, |
34fedff8 DV |
336 | "width": this.options.axisLabelWidth + "px", |
337 | "overflow": "hidden" | |
338 | }; | |
339 | var makeDiv = function(txt) { | |
340 | var div = document.createElement("div"); | |
341 | for (var name in labelStyle) { | |
85b99f0b DV |
342 | if (labelStyle.hasOwnProperty(name)) { |
343 | div.style[name] = labelStyle[name]; | |
344 | } | |
fbe31dc8 | 345 | } |
34fedff8 DV |
346 | div.appendChild(document.createTextNode(txt)); |
347 | return div; | |
fbe31dc8 DV |
348 | }; |
349 | ||
350 | // axis lines | |
351 | context.save(); | |
f474c2a3 | 352 | context.strokeStyle = this.options.axisLineColor; |
fbe31dc8 DV |
353 | context.lineWidth = this.options.axisLineWidth; |
354 | ||
fbe31dc8 | 355 | if (this.options.drawYAxis) { |
8b7a0cc3 | 356 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
2160ed4a DV |
357 | for (var i = 0; i < this.layout.yticks.length; i++) { |
358 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
359 | if (typeof(tick) == "function") return; |
360 | var x = this.area.x; | |
361 | var y = this.area.y + tick[0] * this.area.h; | |
362 | context.beginPath(); | |
363 | context.moveTo(x, y); | |
364 | context.lineTo(x - this.options.axisTickSize, y); | |
365 | context.closePath(); | |
366 | context.stroke(); | |
367 | ||
34fedff8 | 368 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
369 | var top = (y - this.options.axisLabelFontSize / 2); |
370 | if (top < 0) top = 0; | |
371 | ||
372 | if (top + this.options.axisLabelFontSize + 3 > this.height) { | |
373 | label.style.bottom = "0px"; | |
374 | } else { | |
375 | label.style.top = top + "px"; | |
376 | } | |
377 | label.style.left = "0px"; | |
378 | label.style.textAlign = "right"; | |
379 | label.style.width = this.options.yAxisLabelWidth + "px"; | |
b0c3b730 | 380 | this.container.appendChild(label); |
fbe31dc8 | 381 | this.ylabels.push(label); |
2160ed4a | 382 | } |
fbe31dc8 DV |
383 | |
384 | // The lowest tick on the y-axis often overlaps with the leftmost | |
385 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
386 | // compensate if necessary. | |
387 | var bottomTick = this.ylabels[0]; | |
388 | var fontSize = this.options.axisLabelFontSize; | |
389 | var bottom = parseInt(bottomTick.style.top) + fontSize; | |
390 | if (bottom > this.height - fontSize) { | |
391 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
392 | fontSize / 2) + "px"; | |
393 | } | |
394 | } | |
395 | ||
396 | context.beginPath(); | |
397 | context.moveTo(this.area.x, this.area.y); | |
398 | context.lineTo(this.area.x, this.area.y + this.area.h); | |
399 | context.closePath(); | |
400 | context.stroke(); | |
401 | } | |
402 | ||
403 | if (this.options.drawXAxis) { | |
404 | if (this.layout.xticks) { | |
2160ed4a DV |
405 | for (var i = 0; i < this.layout.xticks.length; i++) { |
406 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
407 | if (typeof(dataset) == "function") return; |
408 | ||
409 | var x = this.area.x + tick[0] * this.area.w; | |
410 | var y = this.area.y + this.area.h; | |
411 | context.beginPath(); | |
412 | context.moveTo(x, y); | |
413 | context.lineTo(x, y + this.options.axisTickSize); | |
414 | context.closePath(); | |
415 | context.stroke(); | |
416 | ||
34fedff8 | 417 | var label = makeDiv(tick[1]); |
fbe31dc8 DV |
418 | label.style.textAlign = "center"; |
419 | label.style.bottom = "0px"; | |
420 | ||
421 | var left = (x - this.options.axisLabelWidth/2); | |
422 | if (left + this.options.axisLabelWidth > this.width) { | |
423 | left = this.width - this.options.xAxisLabelWidth; | |
424 | label.style.textAlign = "right"; | |
425 | } | |
426 | if (left < 0) { | |
427 | left = 0; | |
428 | label.style.textAlign = "left"; | |
429 | } | |
430 | ||
431 | label.style.left = left + "px"; | |
432 | label.style.width = this.options.xAxisLabelWidth + "px"; | |
b0c3b730 | 433 | this.container.appendChild(label); |
fbe31dc8 | 434 | this.xlabels.push(label); |
2160ed4a | 435 | } |
fbe31dc8 DV |
436 | } |
437 | ||
438 | context.beginPath(); | |
439 | context.moveTo(this.area.x, this.area.y + this.area.h); | |
440 | context.lineTo(this.area.x + this.area.w, this.area.y + this.area.h); | |
441 | context.closePath(); | |
442 | context.stroke(); | |
443 | } | |
444 | ||
445 | context.restore(); | |
6a1aa64f DV |
446 | }; |
447 | ||
fbe31dc8 | 448 | |
6a1aa64f DV |
449 | /** |
450 | * Overrides the CanvasRenderer method to draw error bars | |
451 | */ | |
285a6bda | 452 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
6a1aa64f DV |
453 | var context = this.element.getContext("2d"); |
454 | var colorCount = this.options.colorScheme.length; | |
455 | var colorScheme = this.options.colorScheme; | |
43af96e7 | 456 | var fillAlpha = this.options.fillAlpha; |
6a1aa64f | 457 | var errorBars = this.layout.options.errorBars; |
5954ef32 | 458 | var fillGraph = this.layout.options.fillGraph; |
21d3323f DV |
459 | |
460 | var setNames = []; | |
ca43052c | 461 | for (var name in this.layout.datasets) { |
85b99f0b DV |
462 | if (this.layout.datasets.hasOwnProperty(name)) { |
463 | setNames.push(name); | |
464 | } | |
ca43052c | 465 | } |
21d3323f | 466 | var setCount = setNames.length; |
6a1aa64f | 467 | |
ff00d3e2 DV |
468 | // Update Points |
469 | // TODO(danvk): here | |
2160ed4a DV |
470 | for (var i = 0; i < this.layout.points.length; i++) { |
471 | var point = this.layout.points[i]; | |
6a1aa64f DV |
472 | point.canvasx = this.area.w * point.x + this.area.x; |
473 | point.canvasy = this.area.h * point.y + this.area.y; | |
474 | } | |
6a1aa64f DV |
475 | |
476 | // create paths | |
9317362d | 477 | var isOK = function(x) { return x && !isNaN(x); }; |
6a1aa64f | 478 | |
80aaae18 DV |
479 | var ctx = context; |
480 | if (errorBars) { | |
6a834bbb DV |
481 | if (fillGraph) { |
482 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
483 | } | |
484 | ||
6a1aa64f DV |
485 | for (var i = 0; i < setCount; i++) { |
486 | var setName = setNames[i]; | |
487 | var color = colorScheme[i % colorCount]; | |
6a1aa64f DV |
488 | |
489 | // setup graphics context | |
80aaae18 DV |
490 | ctx.save(); |
491 | ctx.strokeStyle = color; | |
492 | ctx.lineWidth = this.options.strokeWidth; | |
6a1aa64f DV |
493 | var prevX = -1; |
494 | var prevYs = [-1, -1]; | |
495 | var count = 0; | |
496 | var yscale = this.layout.yscale; | |
f474c2a3 DV |
497 | // should be same color as the lines but only 15% opaque. |
498 | var rgb = new RGBColor(color); | |
43af96e7 NK |
499 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
500 | fillAlpha + ')'; | |
f474c2a3 | 501 | ctx.fillStyle = err_color; |
05c9d0c4 DV |
502 | ctx.beginPath(); |
503 | for (var j = 0; j < this.layout.points.length; j++) { | |
504 | var point = this.layout.points[j]; | |
6a1aa64f DV |
505 | count++; |
506 | if (point.name == setName) { | |
5954ef32 | 507 | if (!isOK(point.y)) { |
5011e7a1 | 508 | prevX = -1; |
ae85914a | 509 | continue; |
5011e7a1 | 510 | } |
ff00d3e2 | 511 | // TODO(danvk): here |
6a1aa64f DV |
512 | var newYs = [ point.y - point.errorPlus * yscale, |
513 | point.y + point.errorMinus * yscale ]; | |
514 | newYs[0] = this.area.h * newYs[0] + this.area.y; | |
515 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
5954ef32 DV |
516 | if (prevX >= 0) { |
517 | ctx.moveTo(prevX, prevYs[0]); | |
518 | ctx.lineTo(point.canvasx, newYs[0]); | |
519 | ctx.lineTo(point.canvasx, newYs[1]); | |
520 | ctx.lineTo(prevX, prevYs[1]); | |
521 | ctx.closePath(); | |
522 | } | |
523 | prevYs[0] = newYs[0]; | |
524 | prevYs[1] = newYs[1]; | |
525 | prevX = point.canvasx; | |
526 | } | |
527 | } | |
528 | ctx.fill(); | |
529 | } | |
530 | } else if (fillGraph) { | |
ff00d3e2 | 531 | // TODO(danvk): merge this code with the logic above; they're very similar. |
5954ef32 DV |
532 | for (var i = 0; i < setCount; i++) { |
533 | var setName = setNames[i]; | |
534 | var setNameLast; | |
535 | if (i>0) setNameLast = setNames[i-1]; | |
536 | var color = colorScheme[i % colorCount]; | |
537 | ||
538 | // setup graphics context | |
539 | ctx.save(); | |
540 | ctx.strokeStyle = color; | |
541 | ctx.lineWidth = this.options.strokeWidth; | |
542 | var prevX = -1; | |
543 | var prevYs = [-1, -1]; | |
544 | var count = 0; | |
545 | var yscale = this.layout.yscale; | |
546 | // should be same color as the lines but only 15% opaque. | |
547 | var rgb = new RGBColor(color); | |
43af96e7 NK |
548 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
549 | fillAlpha + ')'; | |
5954ef32 DV |
550 | ctx.fillStyle = err_color; |
551 | ctx.beginPath(); | |
552 | for (var j = 0; j < this.layout.points.length; j++) { | |
553 | var point = this.layout.points[j]; | |
554 | count++; | |
555 | if (point.name == setName) { | |
556 | if (!isOK(point.y)) { | |
557 | prevX = -1; | |
558 | continue; | |
559 | } | |
560 | var pX = 1.0 + this.layout.minyval * this.layout.yscale; | |
561 | if (pX < 0.0) pX = 0.0; | |
562 | else if (pX > 1.0) pX = 1.0; | |
563 | var newYs = [ point.y, pX ]; | |
564 | newYs[0] = this.area.h * newYs[0] + this.area.y; | |
565 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
6a1aa64f | 566 | if (prevX >= 0) { |
05c9d0c4 DV |
567 | ctx.moveTo(prevX, prevYs[0]); |
568 | ctx.lineTo(point.canvasx, newYs[0]); | |
569 | ctx.lineTo(point.canvasx, newYs[1]); | |
570 | ctx.lineTo(prevX, prevYs[1]); | |
571 | ctx.closePath(); | |
6a1aa64f DV |
572 | } |
573 | prevYs[0] = newYs[0]; | |
574 | prevYs[1] = newYs[1]; | |
575 | prevX = point.canvasx; | |
576 | } | |
05c9d0c4 | 577 | } |
6a1aa64f DV |
578 | ctx.fill(); |
579 | } | |
80aaae18 DV |
580 | } |
581 | ||
582 | for (var i = 0; i < setCount; i++) { | |
583 | var setName = setNames[i]; | |
584 | var color = colorScheme[i%colorCount]; | |
585 | ||
586 | // setup graphics context | |
587 | context.save(); | |
588 | var point = this.layout.points[0]; | |
589 | var pointSize = this.dygraph_.attr_("pointSize"); | |
590 | var prevX = null, prevY = null; | |
591 | var drawPoints = this.dygraph_.attr_("drawPoints"); | |
592 | var points = this.layout.points; | |
593 | for (var j = 0; j < points.length; j++) { | |
594 | var point = points[j]; | |
595 | if (point.name == setName) { | |
596 | if (!isOK(point.canvasy)) { | |
597 | // this will make us move to the next point, not draw a line to it. | |
598 | prevX = prevY = null; | |
599 | } else { | |
600 | // A point is "isolated" if it is non-null but both the previous | |
601 | // and next points are null. | |
602 | var isIsolated = (!prevX && (j == points.length - 1 || | |
603 | !isOK(points[j+1].canvasy))); | |
604 | ||
605 | if (!prevX) { | |
606 | prevX = point.canvasx; | |
607 | prevY = point.canvasy; | |
608 | } else { | |
609 | ctx.beginPath(); | |
610 | ctx.strokeStyle = color; | |
611 | ctx.lineWidth = this.options.strokeWidth; | |
612 | ctx.moveTo(prevX, prevY); | |
613 | prevX = point.canvasx; | |
614 | prevY = point.canvasy; | |
615 | ctx.lineTo(prevX, prevY); | |
616 | ctx.stroke(); | |
617 | } | |
618 | ||
619 | if (drawPoints || isIsolated) { | |
620 | ctx.beginPath(); | |
621 | ctx.fillStyle = color; | |
7bf6a9fe DV |
622 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
623 | 0, 2 * Math.PI, false); | |
80aaae18 DV |
624 | ctx.fill(); |
625 | } | |
626 | } | |
627 | } | |
628 | } | |
629 | } | |
6a1aa64f | 630 | |
6a1aa64f DV |
631 | context.restore(); |
632 | }; |