- * 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.
- this.annotations = [];
- 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];
- if (series.length > 1) {
- 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
- };
-
- 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