My question is similar to Horizontal line to show average in dygraph, but not the same. I understand using underlayCallback
but how do I get the underlying data to calculate maximum and minimum of the data. I was to highlight these point in the data, which is being read from a csv file like so: gplot.updateOptions({'file': filename});
and I do not know the filename in advance, so have to calculate the max,min on the fly. I feel it is somehow related to gplot.rawData_
but cannot figure out how.
Thanks.
You can query a Dygraph's data set to get the min and max using its getValue
method, like so:
// Calculate the min/max y value in the Dygraph's data set.
function calcMinMax(g) {
var ymin = g.getValue(0, 1), ymax = g.getValue(0, 1), v;
for (var i = 0; i < g.numRows(); i++) {
for (var j = 1; j < g.numColumns(); j++) {
y = g.getValue(i, j);
if (y < ymin) {
ymin = y;
} else if (y > ymax) {
ymax = y;
}
}
}
return [ymin, ymax];
}
To draw the lines, you'll need to use an underlayCallback
, e.g.
underlayCallback: function(ctx, area) {
var mm = calcMinMax(this),
ymin = mm[0],
ymax = mm[1],
canvasYmin = this.toDomYCoord(ymin),
canvasYmax = this.toDomYCoord(ymax);
ctx.strokeStyle= 'red';
ctx.beginPath();
ctx.moveTo(area.x, canvasYmin);
ctx.lineTo(area.x + area.w, canvasYmin);
ctx.moveTo(area.x, canvasYmax);
ctx.lineTo(area.x + area.w, canvasYmax);
ctx.closePath();
ctx.stroke();
}
See this jsbin for a working example.