- * Creates a new DygraphLayout object.
- * @param {Object} options Options for PlotKit.Layout
- * @return {Object} The DygraphLayout object
- */
-DygraphLayout = function(dygraph, options) {
- this.dygraph_ = dygraph;
- this.options = {}; // TODO(danvk): remove, use attr_ instead.
- Dygraph.update(this.options, options ? options : {});
- this.datasets = new Array();
- this.annotations = new Array()
-};
-
-DygraphLayout.prototype.attr_ = function(name) {
- return this.dygraph_.attr_(name);
-};
-
-DygraphLayout.prototype.addDataset = function(setname, set_xy) {
- this.datasets[setname] = set_xy;
-};
-
-DygraphLayout.prototype.setAnnotations = function(ann) {
- // The Dygraph object's annotations aren't parsed. We parse them here and
- // save a copy.
- var parse = this.attr_('xValueParser');
- for (var i = 0; i < ann.length; i++) {
- var a = {};
- if (!ann[i].xval && !ann[i].x) {
- this.dygraph_.error("Annotations must have an 'x' property");
- return;
- }
- if (ann[i].icon &&
- !(ann[i].hasOwnProperty('width') &&
- ann[i].hasOwnProperty('height'))) {
- this.dygraph_.error("Must set width and height when setting " +
- "annotation.icon property");
- return;
- }
- Dygraph.update(a, ann[i]);
- if (!a.xval) a.xval = parse(a.x);
- this.annotations.push(a);
- }
-};
-
-DygraphLayout.prototype.evaluate = function() {
- this._evaluateLimits();
- this._evaluateLineCharts();
- this._evaluateLineTicks();
- this._evaluateAnnotations();
-};
-
-DygraphLayout.prototype._evaluateLimits = function() {
- this.minxval = this.maxxval = null;
- if (this.options.dateWindow) {
- this.minxval = this.options.dateWindow[0];
- this.maxxval = this.options.dateWindow[1];
- } else {
- for (var name in this.datasets) {
- if (!this.datasets.hasOwnProperty(name)) continue;
- var series = this.datasets[name];
- var x1 = series[0][0];
- if (!this.minxval || x1 < this.minxval) this.minxval = x1;
-
- var x2 = series[series.length - 1][0];
- if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2;
- }
- }
- this.xrange = this.maxxval - this.minxval;
- this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0);
-
- this.minyval = this.options.yAxis[0];
- this.maxyval = this.options.yAxis[1];
- this.yrange = this.maxyval - this.minyval;
- this.yscale = (this.yrange != 0 ? 1/this.yrange : 1.0);
-};
-
-DygraphLayout.prototype._evaluateLineCharts = function() {
- // add all the rects
- this.points = new Array();
- for (var setName in this.datasets) {
- if (!this.datasets.hasOwnProperty(setName)) continue;
-
- var dataset = this.datasets[setName];
- for (var j = 0; j < dataset.length; j++) {
- var item = dataset[j];
- var point = {
- // TODO(danvk): here
- x: ((parseFloat(item[0]) - this.minxval) * this.xscale),
- y: 1.0 - ((parseFloat(item[1]) - this.minyval) * this.yscale),
- xval: parseFloat(item[0]),
- yval: parseFloat(item[1]),
- name: setName
- };
-
- // limit the x, y values so they do not overdraw
- if (point.y <= 0.0) {
- point.y = 0.0;
- }
- if (point.y >= 1.0) {
- point.y = 1.0;
- }
- this.points.push(point);
- }
- }
-};
-
-DygraphLayout.prototype._evaluateLineTicks = function() {
- this.xticks = new Array();
- for (var i = 0; i < this.options.xTicks.length; i++) {
- var tick = this.options.xTicks[i];
- var label = tick.label;
- var pos = this.xscale * (tick.v - this.minxval);
- if ((pos >= 0.0) && (pos <= 1.0)) {
- this.xticks.push([pos, label]);
- }
- }
-
- this.yticks = new Array();
- for (var i = 0; i < this.options.yTicks.length; i++) {
- var tick = this.options.yTicks[i];
- var label = tick.label;
- var pos = 1.0 - (this.yscale * (tick.v - this.minyval));
- if ((pos >= 0.0) && (pos <= 1.0)) {
- this.yticks.push([pos, label]);
- }
- }
-};
-
-
-/**
- * Behaves the same way as PlotKit.Layout, but also copies the errors
- * @private
- */
-DygraphLayout.prototype.evaluateWithError = function() {
- this.evaluate();
- if (!this.options.errorBars) return;
-
- // Copy over the error terms
- var i = 0; // index in this.points
- for (var setName in this.datasets) {
- if (!this.datasets.hasOwnProperty(setName)) continue;
- var j = 0;
- var dataset = this.datasets[setName];
- for (var j = 0; j < dataset.length; j++, i++) {
- var item = dataset[j];
- var xv = parseFloat(item[0]);
- var yv = parseFloat(item[1]);
-
- if (xv == this.points[i].xval &&
- yv == this.points[i].yval) {
- this.points[i].errorMinus = parseFloat(item[2]);
- this.points[i].errorPlus = parseFloat(item[3]);
- }
- }
- }
-};
-
-DygraphLayout.prototype._evaluateAnnotations = function() {
- // Add the annotations to the point to which they belong.
- // Make a map from (setName, xval) to annotation for quick lookups.
- var annotations = {};
- for (var i = 0; i < this.annotations.length; i++) {
- var a = this.annotations[i];
- annotations[a.xval + "," + a.series] = a;
- }
-
- this.annotated_points = [];
- for (var i = 0; i < this.points.length; i++) {
- var p = this.points[i];
- var k = p.xval + "," + p.name;
- if (k in annotations) {
- p.annotation = annotations[k];
- this.annotated_points.push(p);
- }
- }
-};
-
-/**
- * Convenience function to remove all the data sets from a graph
- */
-DygraphLayout.prototype.removeAllDatasets = function() {
- delete this.datasets;
- this.datasets = new Array();
-};
-
-/**
- * Change the values of various layout options
- * @param {Object} new_options an associative array of new properties
- */
-DygraphLayout.prototype.updateOptions = function(new_options) {
- Dygraph.update(this.options, new_options ? new_options : {});
-};
-
-/**
- * Return a copy of the point at the indicated index, with its yval unstacked.
- * @param int index of point in layout_.points
- */
-DygraphLayout.prototype.unstackPointAtIndex_ = function(idx) {
- var point = this.points[idx];
-
- // Clone the point since we modify it
- var unstackedPoint = {};
- for (var i in point) {
- unstackedPoint[i] = point[i];
- }
-
- if (!this.attr_("stackedGraph")) {
- return unstackedPoint;
- }
-
- // The unstacked yval is equal to the current yval minus the yval of the
- // next point at the same xval.
- for (var i = idx+1; i < this.points.length; i++) {
- if (this.points[i].xval == point.xval) {
- unstackedPoint.yval -= this.points[i].yval;
- break;
- }
- }
-
- return unstackedPoint;
-}
-
-// Subclass PlotKit.CanvasRenderer to add:
-// 1. X/Y grid overlay
-// 2. Ability to draw error bars (if required)
-
-/**
- * Sets some PlotKit.CanvasRenderer options