div.style.width = Dygraph.DEFAULT_WIDTH + "px";
}
}
- // these will be zero if the dygraph's div is hidden.
- this.width_ = div.clientWidth;
- this.height_ = div.clientHeight;
+ // These will be zero if the dygraph's div is hidden. In that case,
+ // use the user-specified attributes if present. If not, use zero
+ // and assume the user will call resize to fix things later.
+ this.width_ = div.clientWidth || attrs.width || 0;
+ this.height_ = div.clientHeight || attrs.height || 0;
// TODO(danvk): set fillGraph to be part of attrs_ here, not user_attrs_.
if (attrs.stackedGraph) {
this.canvas_ = Dygraph.createCanvas();
this.canvas_.style.position = "absolute";
+ // ... and for static parts of the chart.
+ this.hidden_ = this.createPlotKitCanvas_(this.canvas_);
+
this.resizeElements_();
this.canvas_ctx_ = Dygraph.getContext(this.canvas_);
-
- // ... and for static parts of the chart.
- this.hidden_ = this.createPlotKitCanvas_(this.canvas_);
this.hidden_ctx_ = Dygraph.getContext(this.hidden_);
// The interactive parts of the graph are drawn on top of the chart.
this.canvas_.height = this.height_;
this.canvas_.style.width = this.width_ + "px"; // for IE
this.canvas_.style.height = this.height_ + "px"; // for IE
+ this.hidden_.width = this.width_;
+ this.hidden_.height = this.height_;
+ this.hidden_.style.width = this.width_ + "px"; // for IE
+ this.hidden_.style.height = this.height_ + "px"; // for IE
};
/**
if (prevDirection == Dygraph.HORIZONTAL) {
ctx.clearRect(Math.min(startX, prevEndX), this.layout_.getPlotArea().y,
Math.abs(startX - prevEndX), this.layout_.getPlotArea().h);
- } else if (prevDirection == Dygraph.VERTICAL){
+ } else if (prevDirection == Dygraph.VERTICAL) {
ctx.clearRect(this.layout_.getPlotArea().x, Math.min(startY, prevEndY),
this.layout_.getPlotArea().w, Math.abs(startY - prevEndY));
}
*/
Dygraph.prototype.findClosestRow = function(domX) {
var minDistX = Infinity;
- var pointIdx = -1, setIdx = -1;
+ var closestRow = -1;
var sets = this.layout_.points;
for (var i = 0; i < sets.length; i++) {
var points = sets[i];
var dist = Math.abs(point.canvasx - domX);
if (dist < minDistX) {
minDistX = dist;
- setIdx = i;
- pointIdx = j;
+ closestRow = point.idx;
}
}
}
- // TODO(danvk): remove this function; it's trivial and has only one use.
- return this.idxToRow_(setIdx, pointIdx);
+ return closestRow;
};
/**
*/
Dygraph.prototype.findClosestPoint = function(domX, domY) {
var minDist = Infinity;
- var idx = -1;
- var dist, dx, dy, point, closestPoint, closestSeries;
+ var dist, dx, dy, point, closestPoint, closestSeries, closestRow;
for ( var setIdx = this.layout_.points.length - 1 ; setIdx >= 0 ; --setIdx ) {
var points = this.layout_.points[setIdx];
for (var i = 0; i < points.length; ++i) {
- var point = points[i];
+ point = points[i];
if (!Dygraph.isValidPoint(point)) continue;
dx = point.canvasx - domX;
dy = point.canvasy - domY;
minDist = dist;
closestPoint = point;
closestSeries = setIdx;
- idx = i;
+ closestRow = point.idx;
}
}
}
var name = this.layout_.setNames[closestSeries];
return {
- row: idx + this.getLeftBoundary_(),
+ row: closestRow,
seriesName: name,
point: closestPoint
};
*/
Dygraph.prototype.findStackedPoint = function(domX, domY) {
var row = this.findClosestRow(domX);
- var boundary = this.getLeftBoundary_();
- var rowIdx = row - boundary;
var closestPoint, closestSeries;
for (var setIdx = 0; setIdx < this.layout_.points.length; ++setIdx) {
+ var boundary = this.getLeftBoundary_(setIdx);
+ var rowIdx = row - boundary;
var points = this.layout_.points[setIdx];
if (rowIdx >= points.length) continue;
var p1 = points[rowIdx];
callback(event,
this.lastx_,
this.selPoints_,
- this.lastRow_ + this.getLeftBoundary_(),
+ this.lastRow_,
this.highlightSet_);
}
};
/**
- * Fetch left offset from first defined boundaryIds record (see bug #236).
+ * Fetch left offset from the specified set index or if not passed, the
+ * first defined boundaryIds record (see bug #236).
* @private
*/
-Dygraph.prototype.getLeftBoundary_ = function() {
- for (var i = 0; i < this.boundaryIds_.length; i++) {
- if (this.boundaryIds_[i] !== undefined) {
- return this.boundaryIds_[i][0];
+Dygraph.prototype.getLeftBoundary_ = function(setIdx) {
+ if (this.boundaryIds_[setIdx]) {
+ return this.boundaryIds_[setIdx][0];
+ } else {
+ for (var i = 0; i < this.boundaryIds_.length; i++) {
+ if (this.boundaryIds_[i] !== undefined) {
+ return this.boundaryIds_[i][0];
+ }
}
+ return 0;
}
- return 0;
-};
-
-/**
- * Transforms layout_.points index into data row number.
- * @param int layout_.points index
- * @return int row number, or -1 if none could be found.
- * @private
- */
-Dygraph.prototype.idxToRow_ = function(setIdx, rowIdx) {
- if (rowIdx < 0) return -1;
-
- var boundary = this.getLeftBoundary_();
- return boundary + rowIdx;
};
Dygraph.prototype.animateSelection_ = function(direction) {
// Extract the points we've selected
this.selPoints_ = [];
- if (row !== false) {
- row -= this.getLeftBoundary_();
- }
-
var changed = false;
if (row !== false && row >= 0) {
if (row != this.lastRow_) changed = true;
this.lastRow_ = row;
for (var setIdx = 0; setIdx < this.layout_.points.length; ++setIdx) {
var points = this.layout_.points[setIdx];
- if (row < points.length) {
- var point = points[row];
+ var setRow = row - this.getLeftBoundary_(setIdx);
+ if (setRow < points.length) {
+ var point = points[setRow];
if (point.yval !== null) this.selPoints_.push(point);
}
}
var points = this.layout_.points[setIdx];
for (var row = 0; row < points.length; row++) {
if (points[row].x == this.selPoints_[0].x) {
- return row + this.getLeftBoundary_();
+ return points[row].idx;
}
}
}
this.plotter_.clear();
}
- if(!this.is_initial_draw_) {
+ if (!this.is_initial_draw_) {
this.canvas_ctx_.restore();
this.hidden_ctx_.restore();
}
/**
* Converts a series to a Point array.
*
+ * @private
* @param {Array.<Array.<(?number|Array<?number>)>} series Array where
* series[row] = [x,y] or [x, [y, err]] or [x, [y, yplus, yminus]].
* @param {boolean} bars True if error bars or custom bars are being drawn.
* to reflect the stacked values.
* @param {string} fillMethod Interpolation method, one of 'all', 'inside', or
* 'none'.
+ * @private
*/
Dygraph.stackPoints_ = function(
points, cumulativeYval, seriesExtremes, fillMethod) {
isInvalidValue = isValueNull(series[correctedLastIdx]);
}
- boundaryIds[i-1] = [(firstIdx > 0) ? firstIdx - 1 : firstIdx,
- (lastIdx < series.length - 1) ? lastIdx + 1 : lastIdx];
if (correctedFirstIdx!==firstIdx) {
firstIdx = correctedFirstIdx;
if (correctedLastIdx !== lastIdx) {
lastIdx = correctedLastIdx;
}
+
+ boundaryIds[i-1] = [firstIdx, lastIdx];
+
// .slice's end is exclusive, we want to include lastIdx.
series = series.slice(firstIdx, lastIdx + 1);
} else {
}
- if(independentTicks) {
+ if (independentTicks) {
axis.independentTicks = independentTicks;
var opts = this.optionsViewForAxis_('y' + (i ? '2' : ''));
var ticker = opts('ticker');
} else {
// Calculate the rolling average for the first rollPeriod - 1 points where
// there is not enough data to roll over the full number of points
- if (!this.attr_("errorBars")){
+ if (!this.attr_("errorBars")) {
if (rollPeriod == 1) {
return originalData;
}
this.height_ = this.maindiv_.clientHeight;
}
- this.resizeElements_();
-
if (old_width != this.width_ || old_height != this.height_) {
+ // Resizing a canvas erases it, even when the size doesn't change, so
+ // any resize needs to be followed by a redraw.
+ this.resizeElements_();
this.predraw_();
}