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