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: | |
0abfbd7e | 7 | * - grid overlays |
3df0ccf0 DV |
8 | * - error bars |
9 | * - dygraphs attribute system | |
ad1798c2 DV |
10 | * |
11 | * High level overview of classes: | |
12 | * | |
13 | * - DygraphLayout | |
14 | * This contains all the data to be charted. | |
15 | * It uses data coordinates, but also records the chart range (in data | |
16 | * coordinates) and hence is able to calculate percentage positions ('In | |
17 | * this view, Point A lies 25% down the x-axis.') | |
18 | * Two things that it does not do are: | |
19 | * 1. Record pixel coordinates for anything. | |
20 | * 2. (oddly) determine anything about the layout of chart elements. | |
21 | * The naming is a vestige of Dygraph's original PlotKit roots. | |
22 | * | |
23 | * - DygraphCanvasRenderer | |
24 | * This class determines the charting area (in pixel coordinates), maps the | |
25 | * percentage coordinates in the DygraphLayout to pixels and draws them. | |
26 | * It's also responsible for creating chart DOM elements, i.e. annotations, | |
27 | * tick mark labels, the title and the x/y-axis labels. | |
6a1aa64f DV |
28 | */ |
29 | ||
6a1aa64f | 30 | /** |
3df0ccf0 | 31 | * Creates a new DygraphLayout object. |
285a6bda | 32 | * @return {Object} The DygraphLayout object |
6a1aa64f | 33 | */ |
b2c9222a | 34 | DygraphLayout = function(dygraph) { |
efe0829a | 35 | this.dygraph_ = dygraph; |
efe0829a | 36 | this.datasets = new Array(); |
937029df | 37 | this.annotations = new Array(); |
b2c9222a DV |
38 | this.yAxes_ = null; |
39 | ||
40 | // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and | |
41 | // yticks are outputs. Clean this up. | |
42 | this.xTicks_ = null; | |
43 | this.yTicks_ = null; | |
6a1aa64f | 44 | }; |
efe0829a DV |
45 | |
46 | DygraphLayout.prototype.attr_ = function(name) { | |
47 | return this.dygraph_.attr_(name); | |
48 | }; | |
49 | ||
50 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
51 | this.datasets[setname] = set_xy; | |
52 | }; | |
53 | ||
5c528fa2 DV |
54 | DygraphLayout.prototype.setAnnotations = function(ann) { |
55 | // The Dygraph object's annotations aren't parsed. We parse them here and | |
70a50f94 | 56 | // save a copy. If there is no parser, then the user must be using raw format. |
973e2b79 | 57 | this.annotations = []; |
70a50f94 | 58 | var parse = this.attr_('xValueParser') || function(x) { return x; }; |
5c528fa2 DV |
59 | for (var i = 0; i < ann.length; i++) { |
60 | var a = {}; | |
a685723c | 61 | if (!ann[i].xval && !ann[i].x) { |
5c528fa2 DV |
62 | this.dygraph_.error("Annotations must have an 'x' property"); |
63 | return; | |
64 | } | |
ce5e8d36 | 65 | if (ann[i].icon && |
33030f33 DV |
66 | !(ann[i].hasOwnProperty('width') && |
67 | ann[i].hasOwnProperty('height'))) { | |
68 | this.dygraph_.error("Must set width and height when setting " + | |
ce5e8d36 DV |
69 | "annotation.icon property"); |
70 | return; | |
71 | } | |
5c528fa2 | 72 | Dygraph.update(a, ann[i]); |
a685723c | 73 | if (!a.xval) a.xval = parse(a.x); |
5c528fa2 | 74 | this.annotations.push(a); |
ce49c2fa | 75 | } |
ce49c2fa DV |
76 | }; |
77 | ||
b2c9222a DV |
78 | DygraphLayout.prototype.setXTicks = function(xTicks) { |
79 | this.xTicks_ = xTicks; | |
80 | }; | |
81 | ||
82 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. | |
83 | DygraphLayout.prototype.setYAxes = function (yAxes) { | |
84 | this.yAxes_ = yAxes; | |
85 | }; | |
86 | ||
87 | DygraphLayout.prototype.setDateWindow = function(dateWindow) { | |
88 | this.dateWindow_ = dateWindow; | |
89 | }; | |
90 | ||
efe0829a DV |
91 | DygraphLayout.prototype.evaluate = function() { |
92 | this._evaluateLimits(); | |
93 | this._evaluateLineCharts(); | |
94 | this._evaluateLineTicks(); | |
ce49c2fa | 95 | this._evaluateAnnotations(); |
efe0829a DV |
96 | }; |
97 | ||
98 | DygraphLayout.prototype._evaluateLimits = function() { | |
99 | this.minxval = this.maxxval = null; | |
b2c9222a DV |
100 | if (this.dateWindow_) { |
101 | this.minxval = this.dateWindow_[0]; | |
102 | this.maxxval = this.dateWindow_[1]; | |
f6401bf6 DV |
103 | } else { |
104 | for (var name in this.datasets) { | |
105 | if (!this.datasets.hasOwnProperty(name)) continue; | |
106 | var series = this.datasets[name]; | |
48841144 NN |
107 | if (series.length > 1) { |
108 | var x1 = series[0][0]; | |
109 | if (!this.minxval || x1 < this.minxval) this.minxval = x1; | |
110 | ||
111 | var x2 = series[series.length - 1][0]; | |
112 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
113 | } | |
f6401bf6 | 114 | } |
efe0829a DV |
115 | } |
116 | this.xrange = this.maxxval - this.minxval; | |
117 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); | |
118 | ||
b2c9222a DV |
119 | for (var i = 0; i < this.yAxes_.length; i++) { |
120 | var axis = this.yAxes_[i]; | |
26ca7938 DV |
121 | axis.minyval = axis.computedValueRange[0]; |
122 | axis.maxyval = axis.computedValueRange[1]; | |
ea4942ed DV |
123 | axis.yrange = axis.maxyval - axis.minyval; |
124 | axis.yscale = (axis.yrange != 0 ? 1.0 / axis.yrange : 1.0); | |
ff022deb | 125 | |
c742293b RK |
126 | if (axis.g.attr_("logscale")) { |
127 | axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval); | |
128 | axis.ylogscale = (axis.ylogrange != 0 ? 1.0 / axis.ylogrange : 1.0); | |
129 | if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) { | |
130 | axis.g.error('axis ' + i + ' of graph at ' + axis.g + | |
131 | ' can\'t be displayed in log scale for range [' + | |
132 | axis.minyval + ' - ' + axis.maxyval + ']'); | |
133 | } | |
d03e78ed | 134 | } |
ea4942ed | 135 | } |
efe0829a DV |
136 | }; |
137 | ||
138 | DygraphLayout.prototype._evaluateLineCharts = function() { | |
139 | // add all the rects | |
140 | this.points = new Array(); | |
141 | for (var setName in this.datasets) { | |
85b99f0b DV |
142 | if (!this.datasets.hasOwnProperty(setName)) continue; |
143 | ||
144 | var dataset = this.datasets[setName]; | |
b2c9222a | 145 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
ea4942ed | 146 | |
85b99f0b DV |
147 | for (var j = 0; j < dataset.length; j++) { |
148 | var item = dataset[j]; | |
d03e78ed | 149 | |
3637724f | 150 | var yval; |
7d0e7a0d | 151 | if (axis.logscale) { |
0037b2a4 | 152 | yval = 1.0 - ((Dygraph.log10(parseFloat(item[1])) - Dygraph.log10(axis.minyval)) * axis.ylogscale); // really should just be yscale. |
ff022deb | 153 | } else { |
3637724f | 154 | yval = 1.0 - ((parseFloat(item[1]) - axis.minyval) * axis.yscale); |
ff022deb | 155 | } |
85b99f0b | 156 | var point = { |
ff00d3e2 | 157 | // TODO(danvk): here |
85b99f0b | 158 | x: ((parseFloat(item[0]) - this.minxval) * this.xscale), |
3637724f | 159 | y: yval, |
85b99f0b DV |
160 | xval: parseFloat(item[0]), |
161 | yval: parseFloat(item[1]), | |
162 | name: setName | |
163 | }; | |
164 | ||
1a26f3fb | 165 | this.points.push(point); |
85b99f0b | 166 | } |
efe0829a DV |
167 | } |
168 | }; | |
169 | ||
170 | DygraphLayout.prototype._evaluateLineTicks = function() { | |
171 | this.xticks = new Array(); | |
b2c9222a DV |
172 | for (var i = 0; i < this.xTicks_.length; i++) { |
173 | var tick = this.xTicks_[i]; | |
efe0829a DV |
174 | var label = tick.label; |
175 | var pos = this.xscale * (tick.v - this.minxval); | |
176 | if ((pos >= 0.0) && (pos <= 1.0)) { | |
177 | this.xticks.push([pos, label]); | |
178 | } | |
179 | } | |
180 | ||
181 | this.yticks = new Array(); | |
b2c9222a DV |
182 | for (var i = 0; i < this.yAxes_.length; i++ ) { |
183 | var axis = this.yAxes_[i]; | |
67076b22 DV |
184 | for (var j = 0; j < axis.ticks.length; j++) { |
185 | var tick = axis.ticks[j]; | |
186 | var label = tick.label; | |
ff022deb | 187 | var pos = this.dygraph_.toPercentYCoord(tick.v, i); |
67076b22 | 188 | if ((pos >= 0.0) && (pos <= 1.0)) { |
9012dd21 | 189 | this.yticks.push([i, pos, label]); |
67076b22 | 190 | } |
efe0829a DV |
191 | } |
192 | } | |
193 | }; | |
194 | ||
6a1aa64f DV |
195 | |
196 | /** | |
197 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
198 | * @private | |
199 | */ | |
285a6bda | 200 | DygraphLayout.prototype.evaluateWithError = function() { |
6a1aa64f | 201 | this.evaluate(); |
b2c9222a | 202 | if (!(this.attr_('errorBars') || this.attr_('customBars'))) return; |
6a1aa64f DV |
203 | |
204 | // Copy over the error terms | |
205 | var i = 0; // index in this.points | |
206 | for (var setName in this.datasets) { | |
85b99f0b DV |
207 | if (!this.datasets.hasOwnProperty(setName)) continue; |
208 | var j = 0; | |
209 | var dataset = this.datasets[setName]; | |
210 | for (var j = 0; j < dataset.length; j++, i++) { | |
211 | var item = dataset[j]; | |
212 | var xv = parseFloat(item[0]); | |
213 | var yv = parseFloat(item[1]); | |
214 | ||
215 | if (xv == this.points[i].xval && | |
216 | yv == this.points[i].yval) { | |
217 | this.points[i].errorMinus = parseFloat(item[2]); | |
218 | this.points[i].errorPlus = parseFloat(item[3]); | |
219 | } | |
220 | } | |
6a1aa64f DV |
221 | } |
222 | }; | |
223 | ||
ce49c2fa DV |
224 | DygraphLayout.prototype._evaluateAnnotations = function() { |
225 | // Add the annotations to the point to which they belong. | |
226 | // Make a map from (setName, xval) to annotation for quick lookups. | |
227 | var annotations = {}; | |
228 | for (var i = 0; i < this.annotations.length; i++) { | |
229 | var a = this.annotations[i]; | |
230 | annotations[a.xval + "," + a.series] = a; | |
231 | } | |
232 | ||
233 | this.annotated_points = []; | |
234 | for (var i = 0; i < this.points.length; i++) { | |
235 | var p = this.points[i]; | |
236 | var k = p.xval + "," + p.name; | |
237 | if (k in annotations) { | |
238 | p.annotation = annotations[k]; | |
239 | this.annotated_points.push(p); | |
240 | } | |
241 | } | |
242 | }; | |
243 | ||
6a1aa64f DV |
244 | /** |
245 | * Convenience function to remove all the data sets from a graph | |
246 | */ | |
285a6bda | 247 | DygraphLayout.prototype.removeAllDatasets = function() { |
6a1aa64f DV |
248 | delete this.datasets; |
249 | this.datasets = new Array(); | |
250 | }; | |
251 | ||
252 | /** | |
667d510b | 253 | * Return a copy of the point at the indicated index, with its yval unstacked. |
254 | * @param int index of point in layout_.points | |
255 | */ | |
8c03ba63 | 256 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { |
667d510b | 257 | var point = this.points[idx]; |
258 | ||
259 | // Clone the point since we modify it | |
260 | var unstackedPoint = {}; | |
261 | for (var i in point) { | |
262 | unstackedPoint[i] = point[i]; | |
263 | } | |
264 | ||
265 | if (!this.attr_("stackedGraph")) { | |
266 | return unstackedPoint; | |
267 | } | |
268 | ||
269 | // The unstacked yval is equal to the current yval minus the yval of the | |
270 | // next point at the same xval. | |
271 | for (var i = idx+1; i < this.points.length; i++) { | |
272 | if (this.points[i].xval == point.xval) { | |
273 | unstackedPoint.yval -= this.points[i].yval; | |
274 | break; | |
275 | } | |
276 | } | |
277 | ||
278 | return unstackedPoint; | |
279 | } | |
280 | ||
6a1aa64f | 281 | /** |
423f5ed3 DV |
282 | * The DygraphCanvasRenderer class does the actual rendering of the chart onto |
283 | * a canvas. It's based on PlotKit.CanvasRenderer. | |
6a1aa64f | 284 | * @param {Object} element The canvas to attach to |
2cf95fff RK |
285 | * @param {Object} elementContext The 2d context of the canvas (injected so it |
286 | * can be mocked for testing.) | |
285a6bda | 287 | * @param {Layout} layout The DygraphLayout object for this graph. |
6a1aa64f | 288 | */ |
423f5ed3 | 289 | DygraphCanvasRenderer = function(dygraph, element, elementContext, layout) { |
9317362d | 290 | this.dygraph_ = dygraph; |
fbe31dc8 | 291 | |
fbe31dc8 | 292 | this.layout = layout; |
b0c3b730 | 293 | this.element = element; |
2cf95fff | 294 | this.elementContext = elementContext; |
fbe31dc8 DV |
295 | this.container = this.element.parentNode; |
296 | ||
fbe31dc8 DV |
297 | this.height = this.element.height; |
298 | this.width = this.element.width; | |
299 | ||
300 | // --- check whether everything is ok before we return | |
301 | if (!this.isIE && !(DygraphCanvasRenderer.isSupported(this.element))) | |
302 | throw "Canvas is not supported."; | |
303 | ||
304 | // internal state | |
305 | this.xlabels = new Array(); | |
306 | this.ylabels = new Array(); | |
ce49c2fa | 307 | this.annotations = new Array(); |
ad1798c2 | 308 | this.chartLabels = {}; |
fbe31dc8 | 309 | |
423f5ed3 DV |
310 | this.area = this.computeArea_(); |
311 | this.container.style.position = "relative"; | |
312 | this.container.style.width = this.width + "px"; | |
313 | ||
314 | // Set up a clipping area for the canvas (and the interaction canvas). | |
315 | // This ensures that we don't overdraw. | |
316 | var ctx = this.dygraph_.canvas_ctx_; | |
317 | ctx.beginPath(); | |
318 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
319 | ctx.clip(); | |
320 | ||
321 | ctx = this.dygraph_.hidden_ctx_; | |
322 | ctx.beginPath(); | |
323 | ctx.rect(this.area.x, this.area.y, this.area.w, this.area.h); | |
324 | ctx.clip(); | |
325 | }; | |
326 | ||
327 | DygraphCanvasRenderer.prototype.attr_ = function(x) { | |
328 | return this.dygraph_.attr_(x); | |
329 | }; | |
330 | ||
331 | // Compute the box which the chart should be drawn in. This is the canvas's | |
332 | // box, less space needed for axis and chart labels. | |
0e23cfc6 | 333 | // TODO(danvk): this belongs in DygraphLayout. |
423f5ed3 DV |
334 | DygraphCanvasRenderer.prototype.computeArea_ = function() { |
335 | var area = { | |
9012dd21 | 336 | // TODO(danvk): per-axis setting. |
520c8080 | 337 | x: 0, |
fbe31dc8 DV |
338 | y: 0 |
339 | }; | |
520c8080 DV |
340 | if (this.attr_('drawYAxis')) { |
341 | area.x = this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'); | |
342 | } | |
343 | ||
423f5ed3 | 344 | area.w = this.width - area.x - this.attr_('rightGap'); |
520c8080 DV |
345 | area.h = this.height; |
346 | if (this.attr_('drawXAxis')) { | |
347 | area.h -= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize'); | |
348 | } | |
fbe31dc8 | 349 | |
26ca7938 DV |
350 | // Shrink the drawing area to accomodate additional y-axes. |
351 | if (this.dygraph_.numAxes() == 2) { | |
352 | // TODO(danvk): per-axis setting. | |
423f5ed3 | 353 | area.w -= (this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize')); |
26ca7938 DV |
354 | } else if (this.dygraph_.numAxes() > 2) { |
355 | this.dygraph_.error("Only two y-axes are supported at this time. (Trying " + | |
34de8b76 | 356 | "to use " + this.dygraph_.numAxes() + ")"); |
26ca7938 DV |
357 | } |
358 | ||
ad1798c2 DV |
359 | // Add space for chart labels: title, xlabel and ylabel. |
360 | if (this.attr_('title')) { | |
423f5ed3 DV |
361 | area.h -= this.attr_('titleHeight'); |
362 | area.y += this.attr_('titleHeight'); | |
ad1798c2 DV |
363 | } |
364 | if (this.attr_('xlabel')) { | |
423f5ed3 | 365 | area.h -= this.attr_('xLabelHeight'); |
ad1798c2 DV |
366 | } |
367 | if (this.attr_('ylabel')) { | |
ca49434a DV |
368 | // It would make sense to shift the chart here to make room for the y-axis |
369 | // label, but the default yAxisLabelWidth is large enough that this results | |
370 | // in overly-padded charts. The y-axis label should fit fine. If it | |
371 | // doesn't, the yAxisLabelWidth option can be increased. | |
ad1798c2 DV |
372 | } |
373 | ||
423f5ed3 | 374 | return area; |
44c6bc29 DV |
375 | }; |
376 | ||
fbe31dc8 DV |
377 | DygraphCanvasRenderer.prototype.clear = function() { |
378 | if (this.isIE) { | |
379 | // VML takes a while to start up, so we just poll every this.IEDelay | |
380 | try { | |
381 | if (this.clearDelay) { | |
382 | this.clearDelay.cancel(); | |
383 | this.clearDelay = null; | |
384 | } | |
2cf95fff | 385 | var context = this.elementContext; |
fbe31dc8 DV |
386 | } |
387 | catch (e) { | |
76171648 | 388 | // TODO(danvk): this is broken, since MochiKit.Async is gone. |
fbe31dc8 DV |
389 | this.clearDelay = MochiKit.Async.wait(this.IEDelay); |
390 | this.clearDelay.addCallback(bind(this.clear, this)); | |
391 | return; | |
392 | } | |
393 | } | |
394 | ||
2cf95fff | 395 | var context = this.elementContext; |
fbe31dc8 DV |
396 | context.clearRect(0, 0, this.width, this.height); |
397 | ||
2160ed4a | 398 | for (var i = 0; i < this.xlabels.length; i++) { |
b0c3b730 | 399 | var el = this.xlabels[i]; |
b304aaec | 400 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a DV |
401 | } |
402 | for (var i = 0; i < this.ylabels.length; i++) { | |
b0c3b730 | 403 | var el = this.ylabels[i]; |
b304aaec | 404 | if (el.parentNode) el.parentNode.removeChild(el); |
2160ed4a | 405 | } |
ce49c2fa DV |
406 | for (var i = 0; i < this.annotations.length; i++) { |
407 | var el = this.annotations[i]; | |
b304aaec | 408 | if (el.parentNode) el.parentNode.removeChild(el); |
ce49c2fa | 409 | } |
ad1798c2 DV |
410 | for (var k in this.chartLabels) { |
411 | if (!this.chartLabels.hasOwnProperty(k)) continue; | |
412 | var el = this.chartLabels[k]; | |
413 | if (el.parentNode) el.parentNode.removeChild(el); | |
414 | } | |
fbe31dc8 DV |
415 | this.xlabels = new Array(); |
416 | this.ylabels = new Array(); | |
ce49c2fa | 417 | this.annotations = new Array(); |
ad1798c2 | 418 | this.chartLabels = {}; |
fbe31dc8 DV |
419 | }; |
420 | ||
421 | ||
422 | DygraphCanvasRenderer.isSupported = function(canvasName) { | |
423 | var canvas = null; | |
424 | try { | |
21d3323f | 425 | if (typeof(canvasName) == 'undefined' || canvasName == null) |
b0c3b730 | 426 | canvas = document.createElement("canvas"); |
fbe31dc8 | 427 | else |
b0c3b730 | 428 | canvas = canvasName; |
fbe31dc8 DV |
429 | var context = canvas.getContext("2d"); |
430 | } | |
431 | catch (e) { | |
432 | var ie = navigator.appVersion.match(/MSIE (\d\.\d)/); | |
433 | var opera = (navigator.userAgent.toLowerCase().indexOf("opera") != -1); | |
434 | if ((!ie) || (ie[1] < 6) || (opera)) | |
435 | return false; | |
436 | return true; | |
437 | } | |
438 | return true; | |
6a1aa64f | 439 | }; |
6a1aa64f DV |
440 | |
441 | /** | |
600d841a DV |
442 | * @param { [String] } colors Array of color strings. Should have one entry for |
443 | * each series to be rendered. | |
444 | */ | |
445 | DygraphCanvasRenderer.prototype.setColors = function(colors) { | |
446 | this.colorScheme_ = colors; | |
447 | }; | |
448 | ||
449 | /** | |
6a1aa64f DV |
450 | * Draw an X/Y grid on top of the existing plot |
451 | */ | |
285a6bda | 452 | DygraphCanvasRenderer.prototype.render = function() { |
528ce7e5 DV |
453 | // Draw the new X/Y grid. Lines appear crisper when pixels are rounded to |
454 | // half-integers. This prevents them from drawing in two rows/cols. | |
2cf95fff | 455 | var ctx = this.elementContext; |
528ce7e5 DV |
456 | function halfUp(x){return Math.round(x)+0.5}; |
457 | function halfDown(y){return Math.round(y)-0.5}; | |
e7746234 | 458 | |
423f5ed3 | 459 | if (this.attr_('underlayCallback')) { |
1e41bd2d DV |
460 | // NOTE: we pass the dygraph object to this callback twice to avoid breaking |
461 | // users who expect a deprecated form of this callback. | |
423f5ed3 | 462 | this.attr_('underlayCallback')(ctx, this.area, this.dygraph_, this.dygraph_); |
e7746234 EC |
463 | } |
464 | ||
423f5ed3 | 465 | if (this.attr_('drawYGrid')) { |
6a1aa64f DV |
466 | var ticks = this.layout.yticks; |
467 | ctx.save(); | |
423f5ed3 | 468 | ctx.strokeStyle = this.attr_('gridLineColor'); |
990d6a35 | 469 | ctx.lineWidth = this.attr_('gridLineWidth'); |
6a1aa64f | 470 | for (var i = 0; i < ticks.length; i++) { |
880a574f DV |
471 | // TODO(danvk): allow secondary axes to draw a grid, too. |
472 | if (ticks[i][0] != 0) continue; | |
528ce7e5 DV |
473 | var x = halfUp(this.area.x); |
474 | var y = halfDown(this.area.y + ticks[i][1] * this.area.h); | |
6a1aa64f DV |
475 | ctx.beginPath(); |
476 | ctx.moveTo(x, y); | |
477 | ctx.lineTo(x + this.area.w, y); | |
478 | ctx.closePath(); | |
479 | ctx.stroke(); | |
480 | } | |
481 | } | |
482 | ||
423f5ed3 | 483 | if (this.attr_('drawXGrid')) { |
6a1aa64f DV |
484 | var ticks = this.layout.xticks; |
485 | ctx.save(); | |
423f5ed3 | 486 | ctx.strokeStyle = this.attr_('gridLineColor'); |
990d6a35 | 487 | ctx.lineWidth = this.attr_('gridLineWidth'); |
6a1aa64f | 488 | for (var i=0; i<ticks.length; i++) { |
528ce7e5 DV |
489 | var x = halfUp(this.area.x + ticks[i][0] * this.area.w); |
490 | var y = halfDown(this.area.y + this.area.h); | |
6a1aa64f | 491 | ctx.beginPath(); |
880a574f | 492 | ctx.moveTo(x, y); |
6a1aa64f DV |
493 | ctx.lineTo(x, this.area.y); |
494 | ctx.closePath(); | |
495 | ctx.stroke(); | |
496 | } | |
497 | } | |
2ce09b19 DV |
498 | |
499 | // Do the ordinary rendering, as before | |
2ce09b19 | 500 | this._renderLineChart(); |
fbe31dc8 | 501 | this._renderAxis(); |
ad1798c2 | 502 | this._renderChartLabels(); |
ce49c2fa | 503 | this._renderAnnotations(); |
fbe31dc8 DV |
504 | }; |
505 | ||
506 | ||
507 | DygraphCanvasRenderer.prototype._renderAxis = function() { | |
423f5ed3 | 508 | if (!this.attr_('drawXAxis') && !this.attr_('drawYAxis')) return; |
fbe31dc8 | 509 | |
528ce7e5 DV |
510 | // Round pixels to half-integer boundaries for crisper drawing. |
511 | function halfUp(x){return Math.round(x)+0.5}; | |
512 | function halfDown(y){return Math.round(y)-0.5}; | |
513 | ||
2cf95fff | 514 | var context = this.elementContext; |
fbe31dc8 | 515 | |
34fedff8 | 516 | var labelStyle = { |
423f5ed3 DV |
517 | position: "absolute", |
518 | fontSize: this.attr_('axisLabelFontSize') + "px", | |
519 | zIndex: 10, | |
520 | color: this.attr_('axisLabelColor'), | |
521 | width: this.attr_('axisLabelWidth') + "px", | |
522 | overflow: "hidden" | |
34fedff8 DV |
523 | }; |
524 | var makeDiv = function(txt) { | |
525 | var div = document.createElement("div"); | |
526 | for (var name in labelStyle) { | |
85b99f0b DV |
527 | if (labelStyle.hasOwnProperty(name)) { |
528 | div.style[name] = labelStyle[name]; | |
529 | } | |
fbe31dc8 | 530 | } |
34fedff8 DV |
531 | div.appendChild(document.createTextNode(txt)); |
532 | return div; | |
fbe31dc8 DV |
533 | }; |
534 | ||
535 | // axis lines | |
536 | context.save(); | |
423f5ed3 DV |
537 | context.strokeStyle = this.attr_('axisLineColor'); |
538 | context.lineWidth = this.attr_('axisLineWidth'); | |
fbe31dc8 | 539 | |
423f5ed3 | 540 | if (this.attr_('drawYAxis')) { |
8b7a0cc3 | 541 | if (this.layout.yticks && this.layout.yticks.length > 0) { |
2160ed4a DV |
542 | for (var i = 0; i < this.layout.yticks.length; i++) { |
543 | var tick = this.layout.yticks[i]; | |
fbe31dc8 DV |
544 | if (typeof(tick) == "function") return; |
545 | var x = this.area.x; | |
880a574f DV |
546 | var sgn = 1; |
547 | if (tick[0] == 1) { // right-side y-axis | |
548 | x = this.area.x + this.area.w; | |
549 | sgn = -1; | |
9012dd21 DV |
550 | } |
551 | var y = this.area.y + tick[1] * this.area.h; | |
fbe31dc8 | 552 | context.beginPath(); |
528ce7e5 | 553 | context.moveTo(halfUp(x), halfDown(y)); |
0e23cfc6 | 554 | context.lineTo(halfUp(x - sgn * this.attr_('axisTickSize')), halfDown(y)); |
fbe31dc8 DV |
555 | context.closePath(); |
556 | context.stroke(); | |
557 | ||
9012dd21 | 558 | var label = makeDiv(tick[2]); |
423f5ed3 | 559 | var top = (y - this.attr_('axisLabelFontSize') / 2); |
fbe31dc8 DV |
560 | if (top < 0) top = 0; |
561 | ||
423f5ed3 | 562 | if (top + this.attr_('axisLabelFontSize') + 3 > this.height) { |
fbe31dc8 DV |
563 | label.style.bottom = "0px"; |
564 | } else { | |
565 | label.style.top = top + "px"; | |
566 | } | |
9012dd21 | 567 | if (tick[0] == 0) { |
423f5ed3 | 568 | label.style.left = (this.area.x - this.attr_('yAxisLabelWidth') - this.attr_('axisTickSize')) + "px"; |
9012dd21 DV |
569 | label.style.textAlign = "right"; |
570 | } else if (tick[0] == 1) { | |
571 | label.style.left = (this.area.x + this.area.w + | |
423f5ed3 | 572 | this.attr_('axisTickSize')) + "px"; |
9012dd21 DV |
573 | label.style.textAlign = "left"; |
574 | } | |
423f5ed3 | 575 | label.style.width = this.attr_('yAxisLabelWidth') + "px"; |
b0c3b730 | 576 | this.container.appendChild(label); |
fbe31dc8 | 577 | this.ylabels.push(label); |
2160ed4a | 578 | } |
fbe31dc8 DV |
579 | |
580 | // The lowest tick on the y-axis often overlaps with the leftmost | |
581 | // tick on the x-axis. Shift the bottom tick up a little bit to | |
582 | // compensate if necessary. | |
583 | var bottomTick = this.ylabels[0]; | |
423f5ed3 | 584 | var fontSize = this.attr_('axisLabelFontSize'); |
fbe31dc8 DV |
585 | var bottom = parseInt(bottomTick.style.top) + fontSize; |
586 | if (bottom > this.height - fontSize) { | |
587 | bottomTick.style.top = (parseInt(bottomTick.style.top) - | |
588 | fontSize / 2) + "px"; | |
589 | } | |
590 | } | |
591 | ||
528ce7e5 | 592 | // draw a vertical line on the left to separate the chart from the labels. |
fbe31dc8 | 593 | context.beginPath(); |
528ce7e5 DV |
594 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y)); |
595 | context.lineTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
596 | context.closePath(); |
597 | context.stroke(); | |
c1dbeb10 | 598 | |
528ce7e5 | 599 | // if there's a secondary y-axis, draw a vertical line for that, too. |
c1dbeb10 DV |
600 | if (this.dygraph_.numAxes() == 2) { |
601 | context.beginPath(); | |
528ce7e5 DV |
602 | context.moveTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y)); |
603 | context.lineTo(halfDown(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
c1dbeb10 DV |
604 | context.closePath(); |
605 | context.stroke(); | |
606 | } | |
fbe31dc8 DV |
607 | } |
608 | ||
423f5ed3 | 609 | if (this.attr_('drawXAxis')) { |
fbe31dc8 | 610 | if (this.layout.xticks) { |
2160ed4a DV |
611 | for (var i = 0; i < this.layout.xticks.length; i++) { |
612 | var tick = this.layout.xticks[i]; | |
fbe31dc8 DV |
613 | if (typeof(dataset) == "function") return; |
614 | ||
615 | var x = this.area.x + tick[0] * this.area.w; | |
616 | var y = this.area.y + this.area.h; | |
617 | context.beginPath(); | |
528ce7e5 | 618 | context.moveTo(halfUp(x), halfDown(y)); |
423f5ed3 | 619 | context.lineTo(halfUp(x), halfDown(y + this.attr_('axisTickSize'))); |
fbe31dc8 DV |
620 | context.closePath(); |
621 | context.stroke(); | |
622 | ||
34fedff8 | 623 | var label = makeDiv(tick[1]); |
fbe31dc8 | 624 | label.style.textAlign = "center"; |
423f5ed3 | 625 | label.style.top = (y + this.attr_('axisTickSize')) + 'px'; |
fbe31dc8 | 626 | |
423f5ed3 DV |
627 | var left = (x - this.attr_('axisLabelWidth')/2); |
628 | if (left + this.attr_('axisLabelWidth') > this.width) { | |
629 | left = this.width - this.attr_('xAxisLabelWidth'); | |
fbe31dc8 DV |
630 | label.style.textAlign = "right"; |
631 | } | |
632 | if (left < 0) { | |
633 | left = 0; | |
634 | label.style.textAlign = "left"; | |
635 | } | |
636 | ||
637 | label.style.left = left + "px"; | |
423f5ed3 | 638 | label.style.width = this.attr_('xAxisLabelWidth') + "px"; |
b0c3b730 | 639 | this.container.appendChild(label); |
fbe31dc8 | 640 | this.xlabels.push(label); |
2160ed4a | 641 | } |
fbe31dc8 DV |
642 | } |
643 | ||
644 | context.beginPath(); | |
528ce7e5 DV |
645 | context.moveTo(halfUp(this.area.x), halfDown(this.area.y + this.area.h)); |
646 | context.lineTo(halfUp(this.area.x + this.area.w), halfDown(this.area.y + this.area.h)); | |
fbe31dc8 DV |
647 | context.closePath(); |
648 | context.stroke(); | |
649 | } | |
650 | ||
651 | context.restore(); | |
6a1aa64f DV |
652 | }; |
653 | ||
fbe31dc8 | 654 | |
ad1798c2 DV |
655 | DygraphCanvasRenderer.prototype._renderChartLabels = function() { |
656 | // Generate divs for the chart title, xlabel and ylabel. | |
657 | // Space for these divs has already been taken away from the charting area in | |
658 | // the DygraphCanvasRenderer constructor. | |
659 | if (this.attr_('title')) { | |
660 | var div = document.createElement("div"); | |
661 | div.style.position = 'absolute'; | |
662 | div.style.top = '0px'; | |
663 | div.style.left = this.area.x + 'px'; | |
664 | div.style.width = this.area.w + 'px'; | |
665 | div.style.height = this.attr_('titleHeight') + 'px'; | |
666 | div.style.textAlign = 'center'; | |
b4202b3d | 667 | div.style.fontSize = (this.attr_('titleHeight') - 8) + 'px'; |
ad1798c2 | 668 | div.style.fontWeight = 'bold'; |
ca49434a DV |
669 | var class_div = document.createElement("div"); |
670 | class_div.className = 'dygraph-label dygraph-title'; | |
671 | class_div.innerHTML = this.attr_('title'); | |
672 | div.appendChild(class_div); | |
ad1798c2 DV |
673 | this.container.appendChild(div); |
674 | this.chartLabels.title = div; | |
675 | } | |
676 | ||
677 | if (this.attr_('xlabel')) { | |
678 | var div = document.createElement("div"); | |
679 | div.style.position = 'absolute'; | |
680 | div.style.bottom = 0; // TODO(danvk): this is lazy. Calculate style.top. | |
681 | div.style.left = this.area.x + 'px'; | |
682 | div.style.width = this.area.w + 'px'; | |
683 | div.style.height = this.attr_('xLabelHeight') + 'px'; | |
684 | div.style.textAlign = 'center'; | |
86cce9e8 | 685 | div.style.fontSize = (this.attr_('xLabelHeight') - 2) + 'px'; |
ca49434a DV |
686 | |
687 | var class_div = document.createElement("div"); | |
688 | class_div.className = 'dygraph-label dygraph-xlabel'; | |
689 | class_div.innerHTML = this.attr_('xlabel'); | |
690 | div.appendChild(class_div); | |
ad1798c2 DV |
691 | this.container.appendChild(div); |
692 | this.chartLabels.xlabel = div; | |
693 | } | |
694 | ||
695 | if (this.attr_('ylabel')) { | |
696 | var box = { | |
697 | left: 0, | |
698 | top: this.area.y, | |
699 | width: this.attr_('yLabelWidth'), | |
700 | height: this.area.h | |
701 | }; | |
ca49434a | 702 | // TODO(danvk): is this outer div actually necessary? |
ad1798c2 DV |
703 | var div = document.createElement("div"); |
704 | div.style.position = 'absolute'; | |
705 | div.style.left = box.left; | |
706 | div.style.top = box.top + 'px'; | |
707 | div.style.width = box.width + 'px'; | |
708 | div.style.height = box.height + 'px'; | |
86cce9e8 | 709 | div.style.fontSize = (this.attr_('yLabelWidth') - 2) + 'px'; |
ad1798c2 DV |
710 | |
711 | var inner_div = document.createElement("div"); | |
712 | inner_div.style.position = 'absolute'; | |
ad1798c2 DV |
713 | inner_div.style.width = box.height + 'px'; |
714 | inner_div.style.height = box.width + 'px'; | |
715 | inner_div.style.top = (box.height / 2 - box.width / 2) + 'px'; | |
716 | inner_div.style.left = (box.width / 2 - box.height / 2) + 'px'; | |
717 | inner_div.style.textAlign = 'center'; | |
b56b6993 DV |
718 | |
719 | // CSS rotation is an HTML5 feature which is not standardized. Hence every | |
720 | // browser has its own name for the CSS style. | |
ad1798c2 DV |
721 | inner_div.style.transform = 'rotate(-90deg)'; // HTML5 |
722 | inner_div.style.WebkitTransform = 'rotate(-90deg)'; // Safari/Chrome | |
723 | inner_div.style.MozTransform = 'rotate(-90deg)'; // Firefox | |
724 | inner_div.style.OTransform = 'rotate(-90deg)'; // Opera | |
52c47e80 | 725 | inner_div.style.msTransform = 'rotate(-90deg)'; // IE9 |
b56b6993 DV |
726 | |
727 | if (typeof(document.documentMode) !== 'undefined' && | |
728 | document.documentMode < 9) { | |
729 | // We're dealing w/ an old version of IE, so we have to rotate the text | |
730 | // using a BasicImage transform. This uses a different origin of rotation | |
731 | // than HTML5 rotation (top left of div vs. its center). | |
732 | inner_div.style.filter = | |
733 | 'progid:DXImageTransform.Microsoft.BasicImage(rotation=3)'; | |
734 | inner_div.style.left = '0px'; | |
735 | inner_div.style.top = '0px'; | |
736 | } | |
ad1798c2 | 737 | |
ca49434a DV |
738 | var class_div = document.createElement("div"); |
739 | class_div.className = 'dygraph-label dygraph-ylabel'; | |
740 | class_div.innerHTML = this.attr_('ylabel'); | |
741 | ||
742 | inner_div.appendChild(class_div); | |
ad1798c2 DV |
743 | div.appendChild(inner_div); |
744 | this.container.appendChild(div); | |
745 | this.chartLabels.ylabel = div; | |
746 | } | |
747 | }; | |
748 | ||
749 | ||
ce49c2fa DV |
750 | DygraphCanvasRenderer.prototype._renderAnnotations = function() { |
751 | var annotationStyle = { | |
752 | "position": "absolute", | |
423f5ed3 | 753 | "fontSize": this.attr_('axisLabelFontSize') + "px", |
ce49c2fa | 754 | "zIndex": 10, |
3bf2fa91 | 755 | "overflow": "hidden" |
ce49c2fa DV |
756 | }; |
757 | ||
ab5e5c75 DV |
758 | var bindEvt = function(eventName, classEventName, p, self) { |
759 | return function(e) { | |
760 | var a = p.annotation; | |
761 | if (a.hasOwnProperty(eventName)) { | |
762 | a[eventName](a, p, self.dygraph_, e); | |
763 | } else if (self.dygraph_.attr_(classEventName)) { | |
764 | self.dygraph_.attr_(classEventName)(a, p, self.dygraph_,e ); | |
765 | } | |
766 | }; | |
767 | } | |
768 | ||
ce49c2fa DV |
769 | // Get a list of point with annotations. |
770 | var points = this.layout.annotated_points; | |
771 | for (var i = 0; i < points.length; i++) { | |
772 | var p = points[i]; | |
e6d53148 DV |
773 | if (p.canvasx < this.area.x || p.canvasx > this.area.x + this.area.w) { |
774 | continue; | |
775 | } | |
776 | ||
ce5e8d36 DV |
777 | var a = p.annotation; |
778 | var tick_height = 6; | |
779 | if (a.hasOwnProperty("tickHeight")) { | |
780 | tick_height = a.tickHeight; | |
9a40897e DV |
781 | } |
782 | ||
ce49c2fa DV |
783 | var div = document.createElement("div"); |
784 | for (var name in annotationStyle) { | |
785 | if (annotationStyle.hasOwnProperty(name)) { | |
786 | div.style[name] = annotationStyle[name]; | |
787 | } | |
788 | } | |
ce5e8d36 DV |
789 | if (!a.hasOwnProperty('icon')) { |
790 | div.className = "dygraphDefaultAnnotation"; | |
791 | } | |
792 | if (a.hasOwnProperty('cssClass')) { | |
793 | div.className += " " + a.cssClass; | |
794 | } | |
795 | ||
a5ad69cc DV |
796 | var width = a.hasOwnProperty('width') ? a.width : 16; |
797 | var height = a.hasOwnProperty('height') ? a.height : 16; | |
ce5e8d36 DV |
798 | if (a.hasOwnProperty('icon')) { |
799 | var img = document.createElement("img"); | |
800 | img.src = a.icon; | |
33030f33 DV |
801 | img.width = width; |
802 | img.height = height; | |
ce5e8d36 DV |
803 | div.appendChild(img); |
804 | } else if (p.annotation.hasOwnProperty('shortText')) { | |
805 | div.appendChild(document.createTextNode(p.annotation.shortText)); | |
5c528fa2 | 806 | } |
ce5e8d36 | 807 | div.style.left = (p.canvasx - width / 2) + "px"; |
d14b9eed DV |
808 | if (a.attachAtBottom) { |
809 | div.style.top = (this.area.h - height - tick_height) + "px"; | |
810 | } else { | |
811 | div.style.top = (p.canvasy - height - tick_height) + "px"; | |
812 | } | |
ce5e8d36 DV |
813 | div.style.width = width + "px"; |
814 | div.style.height = height + "px"; | |
ce49c2fa DV |
815 | div.title = p.annotation.text; |
816 | div.style.color = this.colors[p.name]; | |
817 | div.style.borderColor = this.colors[p.name]; | |
e6d53148 | 818 | a.div = div; |
ab5e5c75 | 819 | |
9a40897e DV |
820 | Dygraph.addEvent(div, 'click', |
821 | bindEvt('clickHandler', 'annotationClickHandler', p, this)); | |
822 | Dygraph.addEvent(div, 'mouseover', | |
823 | bindEvt('mouseOverHandler', 'annotationMouseOverHandler', p, this)); | |
824 | Dygraph.addEvent(div, 'mouseout', | |
825 | bindEvt('mouseOutHandler', 'annotationMouseOutHandler', p, this)); | |
826 | Dygraph.addEvent(div, 'dblclick', | |
827 | bindEvt('dblClickHandler', 'annotationDblClickHandler', p, this)); | |
ab5e5c75 | 828 | |
ce49c2fa DV |
829 | this.container.appendChild(div); |
830 | this.annotations.push(div); | |
9a40897e | 831 | |
2cf95fff | 832 | var ctx = this.elementContext; |
9a40897e DV |
833 | ctx.strokeStyle = this.colors[p.name]; |
834 | ctx.beginPath(); | |
d14b9eed DV |
835 | if (!a.attachAtBottom) { |
836 | ctx.moveTo(p.canvasx, p.canvasy); | |
837 | ctx.lineTo(p.canvasx, p.canvasy - 2 - tick_height); | |
838 | } else { | |
839 | ctx.moveTo(p.canvasx, this.area.h); | |
840 | ctx.lineTo(p.canvasx, this.area.h - 2 - tick_height); | |
841 | } | |
9a40897e DV |
842 | ctx.closePath(); |
843 | ctx.stroke(); | |
ce49c2fa DV |
844 | } |
845 | }; | |
846 | ||
847 | ||
6a1aa64f DV |
848 | /** |
849 | * Overrides the CanvasRenderer method to draw error bars | |
850 | */ | |
285a6bda | 851 | DygraphCanvasRenderer.prototype._renderLineChart = function() { |
44c6bc29 | 852 | // TODO(danvk): use this.attr_ for many of these. |
2cf95fff | 853 | var context = this.elementContext; |
423f5ed3 | 854 | var fillAlpha = this.attr_('fillAlpha'); |
e4182459 | 855 | var errorBars = this.attr_("errorBars") || this.attr_("customBars"); |
44c6bc29 | 856 | var fillGraph = this.attr_("fillGraph"); |
b2c9222a DV |
857 | var stackedGraph = this.attr_("stackedGraph"); |
858 | var stepPlot = this.attr_("stepPlot"); | |
21d3323f DV |
859 | |
860 | var setNames = []; | |
ca43052c | 861 | for (var name in this.layout.datasets) { |
85b99f0b DV |
862 | if (this.layout.datasets.hasOwnProperty(name)) { |
863 | setNames.push(name); | |
864 | } | |
ca43052c | 865 | } |
21d3323f | 866 | var setCount = setNames.length; |
6a1aa64f | 867 | |
0e23cfc6 | 868 | // TODO(danvk): Move this mapping into Dygraph and get it out of here. |
f032c51d AV |
869 | this.colors = {} |
870 | for (var i = 0; i < setCount; i++) { | |
600d841a | 871 | this.colors[setNames[i]] = this.colorScheme_[i % this.colorScheme_.length]; |
f032c51d AV |
872 | } |
873 | ||
ff00d3e2 DV |
874 | // Update Points |
875 | // TODO(danvk): here | |
2160ed4a DV |
876 | for (var i = 0; i < this.layout.points.length; i++) { |
877 | var point = this.layout.points[i]; | |
6a1aa64f DV |
878 | point.canvasx = this.area.w * point.x + this.area.x; |
879 | point.canvasy = this.area.h * point.y + this.area.y; | |
880 | } | |
6a1aa64f DV |
881 | |
882 | // create paths | |
80aaae18 DV |
883 | var ctx = context; |
884 | if (errorBars) { | |
6a834bbb DV |
885 | if (fillGraph) { |
886 | this.dygraph_.warn("Can't use fillGraph option with error bars"); | |
887 | } | |
888 | ||
6a1aa64f DV |
889 | for (var i = 0; i < setCount; i++) { |
890 | var setName = setNames[i]; | |
b2c9222a | 891 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
f032c51d | 892 | var color = this.colors[setName]; |
6a1aa64f DV |
893 | |
894 | // setup graphics context | |
80aaae18 | 895 | ctx.save(); |
56623f3b | 896 | var prevX = NaN; |
afdc483f | 897 | var prevY = NaN; |
6a1aa64f | 898 | var prevYs = [-1, -1]; |
ea4942ed | 899 | var yscale = axis.yscale; |
f474c2a3 DV |
900 | // should be same color as the lines but only 15% opaque. |
901 | var rgb = new RGBColor(color); | |
43af96e7 NK |
902 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
903 | fillAlpha + ')'; | |
f474c2a3 | 904 | ctx.fillStyle = err_color; |
05c9d0c4 DV |
905 | ctx.beginPath(); |
906 | for (var j = 0; j < this.layout.points.length; j++) { | |
907 | var point = this.layout.points[j]; | |
6a1aa64f | 908 | if (point.name == setName) { |
e9fe4a2f | 909 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 910 | prevX = NaN; |
ae85914a | 911 | continue; |
5011e7a1 | 912 | } |
ce49c2fa | 913 | |
3637724f | 914 | // TODO(danvk): here |
afdc483f NN |
915 | if (stepPlot) { |
916 | var newYs = [ prevY - point.errorPlus * yscale, | |
47600757 | 917 | prevY + point.errorMinus * yscale ]; |
afdc483f NN |
918 | prevY = point.y; |
919 | } else { | |
920 | var newYs = [ point.y - point.errorPlus * yscale, | |
47600757 | 921 | point.y + point.errorMinus * yscale ]; |
afdc483f | 922 | } |
6a1aa64f DV |
923 | newYs[0] = this.area.h * newYs[0] + this.area.y; |
924 | newYs[1] = this.area.h * newYs[1] + this.area.y; | |
56623f3b | 925 | if (!isNaN(prevX)) { |
afdc483f | 926 | if (stepPlot) { |
47600757 | 927 | ctx.moveTo(prevX, newYs[0]); |
afdc483f | 928 | } else { |
47600757 | 929 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 930 | } |
5954ef32 DV |
931 | ctx.lineTo(point.canvasx, newYs[0]); |
932 | ctx.lineTo(point.canvasx, newYs[1]); | |
afdc483f | 933 | if (stepPlot) { |
47600757 | 934 | ctx.lineTo(prevX, newYs[1]); |
afdc483f | 935 | } else { |
47600757 | 936 | ctx.lineTo(prevX, prevYs[1]); |
afdc483f | 937 | } |
5954ef32 DV |
938 | ctx.closePath(); |
939 | } | |
354e15ab | 940 | prevYs = newYs; |
5954ef32 DV |
941 | prevX = point.canvasx; |
942 | } | |
943 | } | |
944 | ctx.fill(); | |
945 | } | |
946 | } else if (fillGraph) { | |
354e15ab DE |
947 | var baseline = [] // for stacked graphs: baseline for filling |
948 | ||
949 | // process sets in reverse order (needed for stacked graphs) | |
950 | for (var i = setCount - 1; i >= 0; i--) { | |
5954ef32 | 951 | var setName = setNames[i]; |
f032c51d | 952 | var color = this.colors[setName]; |
b2c9222a | 953 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
ea4942ed DV |
954 | var axisY = 1.0 + axis.minyval * axis.yscale; |
955 | if (axisY < 0.0) axisY = 0.0; | |
956 | else if (axisY > 1.0) axisY = 1.0; | |
957 | axisY = this.area.h * axisY + this.area.y; | |
5954ef32 DV |
958 | |
959 | // setup graphics context | |
960 | ctx.save(); | |
56623f3b | 961 | var prevX = NaN; |
5954ef32 | 962 | var prevYs = [-1, -1]; |
ea4942ed | 963 | var yscale = axis.yscale; |
5954ef32 DV |
964 | // should be same color as the lines but only 15% opaque. |
965 | var rgb = new RGBColor(color); | |
43af96e7 NK |
966 | var err_color = 'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + |
967 | fillAlpha + ')'; | |
5954ef32 DV |
968 | ctx.fillStyle = err_color; |
969 | ctx.beginPath(); | |
970 | for (var j = 0; j < this.layout.points.length; j++) { | |
971 | var point = this.layout.points[j]; | |
5954ef32 | 972 | if (point.name == setName) { |
e9fe4a2f | 973 | if (!Dygraph.isOK(point.y)) { |
56623f3b | 974 | prevX = NaN; |
5954ef32 DV |
975 | continue; |
976 | } | |
354e15ab DE |
977 | var newYs; |
978 | if (stackedGraph) { | |
979 | lastY = baseline[point.canvasx]; | |
980 | if (lastY === undefined) lastY = axisY; | |
981 | baseline[point.canvasx] = point.canvasy; | |
982 | newYs = [ point.canvasy, lastY ]; | |
983 | } else { | |
984 | newYs = [ point.canvasy, axisY ]; | |
985 | } | |
56623f3b | 986 | if (!isNaN(prevX)) { |
05c9d0c4 | 987 | ctx.moveTo(prevX, prevYs[0]); |
afdc483f | 988 | if (stepPlot) { |
47600757 | 989 | ctx.lineTo(point.canvasx, prevYs[0]); |
afdc483f | 990 | } else { |
47600757 | 991 | ctx.lineTo(point.canvasx, newYs[0]); |
afdc483f | 992 | } |
05c9d0c4 DV |
993 | ctx.lineTo(point.canvasx, newYs[1]); |
994 | ctx.lineTo(prevX, prevYs[1]); | |
995 | ctx.closePath(); | |
6a1aa64f | 996 | } |
354e15ab | 997 | prevYs = newYs; |
6a1aa64f DV |
998 | prevX = point.canvasx; |
999 | } | |
05c9d0c4 | 1000 | } |
6a1aa64f DV |
1001 | ctx.fill(); |
1002 | } | |
80aaae18 DV |
1003 | } |
1004 | ||
1005 | for (var i = 0; i < setCount; i++) { | |
1006 | var setName = setNames[i]; | |
f032c51d | 1007 | var color = this.colors[setName]; |
227b93cc | 1008 | var strokeWidth = this.dygraph_.attr_("strokeWidth", setName); |
80aaae18 DV |
1009 | |
1010 | // setup graphics context | |
1011 | context.save(); | |
1012 | var point = this.layout.points[0]; | |
227b93cc | 1013 | var pointSize = this.dygraph_.attr_("pointSize", setName); |
80aaae18 | 1014 | var prevX = null, prevY = null; |
227b93cc | 1015 | var drawPoints = this.dygraph_.attr_("drawPoints", setName); |
80aaae18 DV |
1016 | var points = this.layout.points; |
1017 | for (var j = 0; j < points.length; j++) { | |
1018 | var point = points[j]; | |
1019 | if (point.name == setName) { | |
e9fe4a2f | 1020 | if (!Dygraph.isOK(point.canvasy)) { |
5d13ef68 | 1021 | if (stepPlot && prevX != null) { |
0599d13b NN |
1022 | // Draw a horizontal line to the start of the missing data |
1023 | ctx.beginPath(); | |
1024 | ctx.strokeStyle = color; | |
423f5ed3 | 1025 | ctx.lineWidth = this.attr_('strokeWidth'); |
0599d13b NN |
1026 | ctx.moveTo(prevX, prevY); |
1027 | ctx.lineTo(point.canvasx, prevY); | |
1028 | ctx.stroke(); | |
1029 | } | |
80aaae18 DV |
1030 | // this will make us move to the next point, not draw a line to it. |
1031 | prevX = prevY = null; | |
1032 | } else { | |
1033 | // A point is "isolated" if it is non-null but both the previous | |
1034 | // and next points are null. | |
1035 | var isIsolated = (!prevX && (j == points.length - 1 || | |
e9fe4a2f | 1036 | !Dygraph.isOK(points[j+1].canvasy))); |
80aaae18 DV |
1037 | |
1038 | if (!prevX) { | |
1039 | prevX = point.canvasx; | |
1040 | prevY = point.canvasy; | |
1041 | } else { | |
46dde5f9 DV |
1042 | // TODO(danvk): figure out why this conditional is necessary. |
1043 | if (strokeWidth) { | |
1044 | ctx.beginPath(); | |
1045 | ctx.strokeStyle = color; | |
1046 | ctx.lineWidth = strokeWidth; | |
1047 | ctx.moveTo(prevX, prevY); | |
1048 | if (stepPlot) { | |
1049 | ctx.lineTo(point.canvasx, prevY); | |
1050 | } | |
1051 | prevX = point.canvasx; | |
1052 | prevY = point.canvasy; | |
1053 | ctx.lineTo(prevX, prevY); | |
1054 | ctx.stroke(); | |
afdc483f | 1055 | } |
80aaae18 DV |
1056 | } |
1057 | ||
1058 | if (drawPoints || isIsolated) { | |
1059 | ctx.beginPath(); | |
1060 | ctx.fillStyle = color; | |
7bf6a9fe DV |
1061 | ctx.arc(point.canvasx, point.canvasy, pointSize, |
1062 | 0, 2 * Math.PI, false); | |
80aaae18 DV |
1063 | ctx.fill(); |
1064 | } | |
1065 | } | |
1066 | } | |
1067 | } | |
1068 | } | |
6a1aa64f | 1069 | |
6a1aa64f DV |
1070 | context.restore(); |
1071 | }; |