From 46dde5f90a668f55e48b798804fe40a3d9f16b20 Mon Sep 17 00:00:00 2001 From: Dan Vanderkam Date: Wed, 29 Sep 2010 18:28:39 +0200 Subject: [PATCH] functioning demo --- dygraph-canvas.js | 29 ++++++++++++++++----------- dygraph.js | 30 ++++++++++++++++++++++++++-- tests/per-series.html | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 100 insertions(+), 14 deletions(-) create mode 100644 tests/per-series.html diff --git a/dygraph-canvas.js b/dygraph-canvas.js index 8d3bfe2..20e1f8b 100644 --- a/dygraph-canvas.js +++ b/dygraph-canvas.js @@ -754,14 +754,16 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { for (var i = 0; i < setCount; i++) { var setName = setNames[i]; + var setIdx = this.dygraph_.indexFromSetName(setName); var color = this.colors[setName]; + var strokeWidth = this.dygraph_.attr_("strokeWidth", setIdx); // setup graphics context context.save(); var point = this.layout.points[0]; - var pointSize = this.dygraph_.attr_("pointSize"); + var pointSize = this.dygraph_.attr_("pointSize", setIdx); var prevX = null, prevY = null; - var drawPoints = this.dygraph_.attr_("drawPoints"); + var drawPoints = this.dygraph_.attr_("drawPoints", setIdx); var points = this.layout.points; for (var j = 0; j < points.length; j++) { var point = points[j]; @@ -779,17 +781,20 @@ DygraphCanvasRenderer.prototype._renderLineChart = function() { prevX = point.canvasx; prevY = point.canvasy; } else { - ctx.beginPath(); - ctx.strokeStyle = color; - ctx.lineWidth = this.options.strokeWidth; - ctx.moveTo(prevX, prevY); - if (stepPlot) { - ctx.lineTo(point.canvasx, prevY); + // TODO(danvk): figure out why this conditional is necessary. + if (strokeWidth) { + ctx.beginPath(); + ctx.strokeStyle = color; + ctx.lineWidth = strokeWidth; + ctx.moveTo(prevX, prevY); + if (stepPlot) { + ctx.lineTo(point.canvasx, prevY); + } + prevX = point.canvasx; + prevY = point.canvasy; + ctx.lineTo(prevX, prevY); + ctx.stroke(); } - prevX = point.canvasx; - prevY = point.canvasy; - ctx.lineTo(prevX, prevY); - ctx.stroke(); } if (drawPoints || isIsolated) { diff --git a/dygraph.js b/dygraph.js index 517f0c6..56f9319 100644 --- a/dygraph.js +++ b/dygraph.js @@ -1021,11 +1021,18 @@ Dygraph.prototype.mouseMove_ = function(event) { */ Dygraph.prototype.updateSelection_ = function() { // Clear the previously drawn vertical, if there is one - var circleSize = this.attr_('highlightCircleSize'); var ctx = this.canvas_.getContext("2d"); if (this.previousVerticalX_ >= 0) { + // Determine the maximum highlight circle size. + var maxCircleSize = 0; + var num_series = this.attr_('labels').length; + for (var i = 1; i < num_series; i++) { + var r = this.attr_('highlightCircleSize', i); + if (r > maxCircleSize) maxCircleSize = r; + } var px = this.previousVerticalX_; - ctx.clearRect(px - circleSize - 1, 0, 2 * circleSize + 2, this.height_); + ctx.clearRect(px - maxCircleSize - 1, 0, + 2 * maxCircleSize + 2, this.height_); } var isOK = function(x) { return x && !isNaN(x); }; @@ -1061,6 +1068,8 @@ Dygraph.prototype.updateSelection_ = function() { ctx.save(); for (var i = 0; i < this.selPoints_.length; i++) { if (!isOK(this.selPoints_[i].canvasy)) continue; + var setIdx = this.indexFromSetName(this.selPoints_[i].name); + var circleSize = this.attr_('highlightCircleSize', setIdx); ctx.beginPath(); ctx.fillStyle = this.plotter_.colors[this.selPoints_[i].name]; ctx.arc(canvasx, this.selPoints_[i].canvasy, circleSize, @@ -2312,6 +2321,11 @@ Dygraph.prototype.updateOptions = function(attrs) { } // TODO(danvk): validate per-series options. + // Supported: + // strokeWidth + // pointSize + // drawPoints + // highlightCircleSize Dygraph.update(this.user_attrs_, attrs); Dygraph.update(this.renderOptions_, attrs); @@ -2427,6 +2441,18 @@ Dygraph.prototype.annotations = function() { return this.annotations_; }; +/** + * Get the index of a series (column) given its name. The first column is the + * x-axis, so the data series start with index 1. + */ +Dygraph.prototype.indexFromSetName = function(name) { + var labels = this.attr_("labels"); + for (var i = 0; i < labels.length; i++) { + if (labels[i] == name) return i; + } + return null; +}; + Dygraph.addAnnotationRule = function() { if (Dygraph.addedAnnotationCSS) return; diff --git a/tests/per-series.html b/tests/per-series.html new file mode 100644 index 0000000..8ac90df --- /dev/null +++ b/tests/per-series.html @@ -0,0 +1,55 @@ + + + Per-Series Properties + + + + + + + +

Chart with per-series properties

+
+

+
+ + + + -- 2.7.4