}
return ret;
};
+
+
+/**
+ * Sample a pixel from the canvas.
+ * Returns an [r, g, b, a] tuple where each values is in [0, 255].
+ */
+Util.samplePixel = function(canvas, x, y) {
+ var ctx = canvas.getContext("2d"); // bypasses Proxy if applied.
+
+ // TODO(danvk): Any performance issues with this?
+ var imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
+
+ var i = 4 * (x + imageData.width * y);
+ var d = imageData.data;
+ return [d[i], d[i+1], d[i+2], d[i+3]];
+};
// 249-299: empty (white)
// TODO(danvk): test the edges of these regions.
- var ctx = g.hidden_.getContext("2d"); // bypass Proxy
- var imageData = ctx.getImageData(0, 0, 400, 300);
-
- assertEquals(400, imageData.width);
- assertEquals(300, imageData.height);
-
- // returns an (r, g, b, alpha) tuple for the pixel.
- // values are in [0, 255].
- var getPixel = function(imageData, x, y) {
- var i = 4 * (x + imageData.width * y);
- var d = imageData.data;
- return [d[i], d[i+1], d[i+2], d[i+3]];
- };
-
- assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 75));
- assertEquals([0, 0, 255, 38], getPixel(imageData, 200, 125));
- assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 175));
- assertEquals([0, 255, 0, 38], getPixel(imageData, 200, 225));
+ assertEquals([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 75));
+ assertEquals([0, 0, 255, 38], Util.samplePixel(g.hidden_, 200, 125));
+ assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 175));
+ assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 225));
}
g.setSelection(0, 'Y2');
assertEquals("0: Y1: 1 Y2: 1", Util.getLegend());
};
+
+// Regression test for http://code.google.com/p/dygraphs/issues/detail?id=176
+stackedTestCase.prototype.testDuplicatedXValue = function() {
+ var opts = {
+ stackedGraph: true,
+ fillAlpha: 0.15,
+ colors: ['#00ff00'],
+ width: 400,
+ height: 300
+ };
+ var data = "X,Y1\n" +
+ "0,1\n" +
+ "1,1\n" +
+ "2,1\n" +
+ "2,1\n" + // duplicate x-value!
+ "3,1\n"
+ ;
+
+ var graph = document.getElementById("graph");
+ g = new Dygraph(graph, data, opts);
+
+ assert(g.yAxisRange()[1] < 2);
+
+ assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 200, 250));
+ assertEquals([0, 255, 0, 38], Util.samplePixel(g.hidden_, 317, 250));
+}
'rgba(' + rgb.r + ',' + rgb.g + ',' + rgb.b + ',' + fillAlpha + ')';
ctx.fillStyle = err_color;
ctx.beginPath();
- while(iter.hasNext) {
+ var last_x, is_first = true;
+ while (iter.hasNext) {
var point = iter.next();
if (!Dygraph.isOK(point.y)) {
prevX = NaN;
continue;
}
if (stackedGraph) {
+ if (!is_first && last_x == point.xval) {
+ continue;
+ } else {
+ is_first = false;
+ last_x = point.xval;
+ }
+
currBaseline = baseline[point.canvasx];
var lastY;
if (currBaseline === undefined) {
series[j][1][2]];
}
} else if (this.attr_("stackedGraph")) {
- var l = series.length;
- var actual_y;
- for (j = 0; j < l; j++) {
+ var actual_y, last_x;
+ for (j = 0; j < series.length; j++) {
// If one data set has a NaN, let all subsequent stacked
// sets inherit the NaN -- only start at 0 for the first set.
var x = series[j][0];
continue;
}
- cumulative_y[x] += actual_y;
+ if (j === 0 || last_x != x) {
+ cumulative_y[x] += actual_y;
+ // If an x-value is repeated, we ignore the duplicates.
+ }
+ last_x = x;
series[j] = [x, cumulative_y[x]];