labelsSeparateLines: false,
labelsKMB: false,
labelsKMG2: false,
+ showLabelsOnHighlight: true,
+
+ yValueFormatter: function(x) { return Dygraph.round_(x, 2); },
strokeWidth: 1.0,
axisLabelFontSize: 14,
xAxisLabelWidth: 50,
yAxisLabelWidth: 50,
+ xAxisLabelFormatter: Dygraph.dateAxisFormatter,
rightGap: 5,
showRoller: false,
customBars: false,
fillGraph: false,
fillAlpha: 0.15,
+ connectSeparatedPoints: false,
stackedGraph: false,
hideOverlayOnMouseOut: true
* Initializes the Dygraph. This creates a new DIV and constructs the PlotKit
* and interaction <canvas> inside of it. See the constructor for details
* on the parameters.
+ * @param {Element} div the Element to render the graph into.
* @param {String | Function} file Source data
- * @param {Array.<String>} labels Names of the data series
* @param {Object} attrs Miscellaneous other options
* @private
*/
this.attrs_ = {};
Dygraph.update(this.attrs_, Dygraph.DEFAULT_ATTRS);
-
+
this.boundaryIds_ = [];
// Make a note of whether labels will be pulled from the CSV file.
if (!colors) {
var sat = this.attr_('colorSaturation') || 1.0;
var val = this.attr_('colorValue') || 0.5;
+ var half = Math.ceil(num / 2);
for (var i = 1; i <= num; i++) {
if (!this.visibility()[i-1]) continue;
// alternate colors for high contrast.
- var idx = i - parseInt(i % 2 ? i / 2 : (i - num)/2, 10);
+ var idx = i % 2 ? Math.ceil(i / 2) : (half + i / 2);
var hue = (1.0 * idx/ (1 + num));
this.colors_.push(Dygraph.hsvToRGB(hue, sat, val));
}
Dygraph.findPosX = function(obj) {
var curleft = 0;
if(obj.offsetParent)
- while(1)
+ while(1)
{
curleft += obj.offsetLeft;
if(!obj.offsetParent)
var idx = -1;
for (var i = 0; i < points.length; i++) {
var dist = Math.abs(points[i].canvasx - canvasx);
- if (dist > minDist) break;
+ if (dist > minDist) continue;
minDist = dist;
idx = i;
}
// Extract the points we've selected
this.selPoints_ = [];
- for (var i = 0; i < points.length; i++) {
- if (points[i].xval == lastx) {
- this.selPoints_.push(points[i]);
+ var cumulative_sum = 0; // used only if we have a stackedGraph.
+ var l = points.length;
+ var isStacked = this.attr_("stackedGraph");
+ if (!this.attr_("stackedGraph")) {
+ for (var i = 0; i < l; i++) {
+ if (points[i].xval == lastx) {
+ this.selPoints_.push(points[i]);
+ }
+ }
+ } else {
+ // Stacked points need to be examined in reverse order.
+ for (var i = l - 1; i >= 0; i--) {
+ if (points[i].xval == lastx) {
+ // Clone the point, since we need to 'unstack' it below.
+ var p = {};
+ for (var k in points[i]) {
+ p[k] = points[i][k];
+ }
+ p.yval -= cumulative_sum;
+ cumulative_sum += p.yval;
+ this.selPoints_.push(p);
+ }
}
}
if (this.attr_("highlightCallback")) {
- var px = this.lastHighlightCallbackX;
+ var px = this.lastx_;
if (px !== null && lastx != px) {
// only fire if the selected point has changed.
- this.lastHighlightCallbackX = lastx;
- if (!this.attr_("stackedGraph")) {
- this.attr_("highlightCallback")(event, lastx, this.selPoints_);
- } else {
- // "unstack" the points.
- var callbackPoints = this.selPoints_.map(
- function(p) { return {xval: p.xval, yval: p.yval, name: p.name} });
- var cumulative_sum = 0;
- for (var j = callbackPoints.length - 1; j >= 0; j--) {
- callbackPoints[j].yval -= cumulative_sum;
- cumulative_sum += callbackPoints[j].yval;
- }
- this.attr_("highlightCallback")(event, lastx, callbackPoints);
- }
+ this.attr_("highlightCallback")(event, lastx, this.selPoints_);
}
}
// Save last x position for callbacks.
this.lastx_ = lastx;
-
+
this.updateSelection_();
};
// Set the status message to indicate the selected point(s)
var replace = this.attr_('xValueFormatter')(this.lastx_, this) + ":";
+ var fmtFunc = this.attr_('yValueFormatter');
var clen = this.colors_.length;
- for (var i = 0; i < this.selPoints_.length; i++) {
- if (!isOK(this.selPoints_[i].canvasy)) continue;
- if (this.attr_("labelsSeparateLines")) {
- replace += "<br/>";
+
+ if (this.attr_('showLabelsOnHighlight')) {
+ // Set the status message to indicate the selected point(s)
+ for (var i = 0; i < this.selPoints_.length; i++) {
+ if (!isOK(this.selPoints_[i].canvasy)) continue;
+ if (this.attr_("labelsSeparateLines")) {
+ replace += "<br/>";
+ }
+ var point = this.selPoints_[i];
+ var c = new RGBColor(this.colors_[i%clen]);
+ var yval = fmtFunc(point.yval);
+ replace += " <b><font color='" + c.toHex() + "'>"
+ + point.name + "</font></b>:"
+ + yval;
}
- var point = this.selPoints_[i];
- var c = new RGBColor(this.colors_[i%clen]);
- replace += " <b><font color='" + c.toHex() + "'>"
- + point.name + "</font></b>:"
- + this.round_(point.yval, 2);
+
+ this.attr_("labelsDiv").innerHTML = replace;
}
- this.attr_("labelsDiv").innerHTML = replace;
// Draw colored circles over the center of each selected point
ctx.save();
for (var i = 0; i < this.selPoints_.length; i++) {
- if (!isOK(this.selPoints_[i%clen].canvasy)) continue;
+ if (!isOK(this.selPoints_[i].canvasy)) continue;
ctx.beginPath();
- ctx.fillStyle = this.colors_[i%clen];
- ctx.arc(canvasx, this.selPoints_[i%clen].canvasy, circleSize,
+ ctx.fillStyle = this.plotter_.colors[this.selPoints_[i].name];
+ ctx.arc(canvasx, this.selPoints_[i].canvasy, circleSize,
0, 2 * Math.PI, false);
ctx.fill();
}
// Extract the points we've selected
this.selPoints_ = [];
var pos = 0;
-
+
if (row !== false) {
row = row-this.boundaryIds_[0][0];
}
-
+
if (row !== false && row >= 0) {
for (var i in this.layout_.datasets) {
if (row < this.layout_.datasets[i].length) {
pos += this.layout_.datasets[i].length;
}
}
-
+
if (this.selPoints_.length) {
this.lastx_ = this.selPoints_[0].xval;
this.updateSelection_();
* @private
*/
Dygraph.prototype.mouseOut_ = function(event) {
+ if (this.attr_("unhighlightCallback")) {
+ this.attr_("unhighlightCallback")(event);
+ }
+
if (this.attr_("hideOverlayOnMouseOut")) {
this.clearSelection();
}
if (!this.selPoints_ || this.selPoints_.length < 1) {
return -1;
}
-
+
for (var row=0; row<this.layout_.points.length; row++ ) {
if (this.layout_.points[row].x == this.selPoints_[0].x) {
return row + this.boundaryIds_[0][0];
* @return {String} A time of the form "HH:MM:SS"
* @private
*/
-Dygraph.prototype.hmsString_ = function(date) {
+Dygraph.hmsString_ = function(date) {
var zeropad = Dygraph.zeropad;
var d = new Date(date);
if (d.getSeconds()) {
}
/**
+ * Convert a JS date to a string appropriate to display on an axis that
+ * is displaying values at the stated granularity.
+ * @param {Date} date The date to format
+ * @param {Number} granularity One of the Dygraph granularity constants
+ * @return {String} The formatted date
+ * @private
+ */
+Dygraph.dateAxisFormatter = function(date, granularity) {
+ if (granularity >= Dygraph.MONTHLY) {
+ return date.strftime('%b %y');
+ } else {
+ var frac = date.getHours() * 3600 + date.getMinutes() * 60 + date.getSeconds() + date.getMilliseconds();
+ if (frac == 0 || granularity >= Dygraph.DAILY) {
+ return new Date(date.getTime() + 3600*1000).strftime('%d%b');
+ } else {
+ return Dygraph.hmsString_(date.getTime());
+ }
+ }
+}
+
+/**
* Convert a JS date (millis since epoch) to YYYY/MM/DD
* @param {Number} date The JavaScript date (ms since epoch)
* @return {String} A date of the form "YYYY/MM/DD"
* @private
- * TODO(danvk): why is this part of the prototype?
*/
Dygraph.dateString_ = function(date, self) {
var zeropad = Dygraph.zeropad;
var ret = "";
var frac = d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds();
- if (frac) ret = " " + self.hmsString_(date);
+ if (frac) ret = " " + Dygraph.hmsString_(date);
return year + "/" + month + "/" + day + ret;
};
* @return {Number} The rounded number
* @private
*/
-Dygraph.prototype.round_ = function(num, places) {
+Dygraph.round_ = function(num, places) {
var shift = Math.pow(10, places);
return Math.round(num * shift)/shift;
};
// Returns an array containing {v: millis, label: label} dictionaries.
//
Dygraph.prototype.GetXAxis = function(start_time, end_time, granularity) {
+ var formatter = this.attr_("xAxisLabelFormatter");
var ticks = [];
if (granularity < Dygraph.MONTHLY) {
// Generate one tick mark for every fixed interval of time.
start_time = d.getTime();
for (var t = start_time; t <= end_time; t += spacing) {
- var d = new Date(t);
- var frac = d.getHours() * 3600 + d.getMinutes() * 60 + d.getSeconds();
- if (frac == 0 || granularity >= Dygraph.DAILY) {
- // the extra hour covers DST problems.
- ticks.push({ v:t, label: new Date(t + 3600*1000).strftime(format) });
- } else {
- ticks.push({ v:t, label: this.hmsString_(t) });
- }
+ ticks.push({ v:t, label: formatter(new Date(t), granularity) });
}
} else {
// Display a tick mark on the first of a set of months of each year.
var date_str = i + "/" + zeropad(1 + months[j]) + "/01";
var t = Date.parse(date_str);
if (t < start_time || t > end_time) continue;
- ticks.push({ v:t, label: new Date(t).strftime('%b %y') });
+ ticks.push({ v:t, label: formatter(new Date(t), granularity) });
}
}
}
for (var i = 0; i < nTicks; i++) {
var tickV = low_val + i * scale;
var absTickV = Math.abs(tickV);
- var label = self.round_(tickV, 2);
+ var label = Dygraph.round_(tickV, 2);
if (k_labels.length) {
// Round up to an appropriate unit.
var n = k*k*k*k;
for (var j = 3; j >= 0; j--, n /= k) {
if (absTickV >= n) {
- label = self.round_(tickV / n, 1) + k_labels[j];
+ label = Dygraph.round_(tickV / n, 1) + k_labels[j];
break;
}
}
this.setColors_();
this.attrs_['pointSize'] = 0.5 * this.attr_('highlightCircleSize');
+ var connectSeparatedPoints = this.attr_('connectSeparatedPoints');
+
// For stacked series.
var cumulative_y = [];
var stacked_datasets = [];
var series = [];
for (var j = 0; j < data.length; j++) {
- var date = data[j][0];
- series[j] = [date, data[j][i]];
+ if (data[j][i] || !connectSeparatedPoints) {
+ var date = data[j][0];
+ series.push([date, data[j][i]]);
+ }
}
series = this.rollingAverage(series, this.rollPeriod_);
this.attrs_.xValueFormatter = Dygraph.dateString_;
this.attrs_.xValueParser = Dygraph.dateParser;
this.attrs_.xTicker = Dygraph.dateTicker;
+ this.attrs_.xAxisLabelFormatter = Dygraph.dateAxisFormatter;
} else {
this.attrs_.xValueFormatter = function(x) { return x; };
this.attrs_.xValueParser = function(x) { return parseFloat(x); };
this.attrs_.xTicker = Dygraph.numericTicks;
+ this.attrs_.xAxisLabelFormatter = this.attrs_.xValueFormatter;
}
};
if (Dygraph.isDateLike(data[0][0])) {
// Some intelligent defaults for a date x-axis.
this.attrs_.xValueFormatter = Dygraph.dateString_;
+ this.attrs_.xAxisLabelFormatter = Dygraph.dateAxisFormatter;
this.attrs_.xTicker = Dygraph.dateTicker;
// Assume they're all dates.
this.attrs_.xValueFormatter = Dygraph.dateString_;
this.attrs_.xValueParser = Dygraph.dateParser;
this.attrs_.xTicker = Dygraph.dateTicker;
+ this.attrs_.xAxisLabelFormatter = Dygraph.dateAxisFormatter;
} else if (indepType == 'number') {
this.attrs_.xValueFormatter = function(x) { return x; };
this.attrs_.xValueParser = function(x) { return parseFloat(x); };
this.attrs_.xTicker = Dygraph.numericTicks;
+ this.attrs_.xAxisLabelFormatter = this.attrs_.xValueFormatter;
} else {
this.error("only 'date', 'datetime' and 'number' types are supported for " +
"column 1 of DataTable input (Got '" + indepType + "')");
/**
* Google charts compatible setSelection
- * Only row selection is supported, all points in the
- * row will be highlighted
+ * Only row selection is supported, all points in the row will be highlighted
* @param {Array} array of the selected cells
* @public
*/
*/
Dygraph.GVizChart.prototype.getSelection = function() {
var selection = [];
-
+
var row = this.date_graph.getSelection();
-
+
if (row < 0) return selection;
-
+
col = 1;
for (var i in this.date_graph.layout_.datasets) {
selection.push({row: row, column: col});