g.setSelection(9);
assertEquals("vf9: y:vf18", getLegend());
};
+
+AxisLabelsTestCase.prototype.testSeriesOrder = function() {
+ var opts = {
+ width: 480,
+ height: 320
+ };
+ var data = "x,00,01,10,11\n" +
+ "0,101,201,301,401\n" +
+ "1,102,202,302,402\n" +
+ "2,103,203,303,403\n" +
+ "3,104,204,304,404\n"
+ ;
+
+ var graph = document.getElementById("graph");
+ var g = new Dygraph(graph, data, opts);
+
+ g.setSelection(2);
+ assertEquals('2: 00:103 01:203 10:303 11:403', getLegend());
+
+ // Sanity checks for indexFromSetName
+ assertEquals(0, g.indexFromSetName("x"));
+ assertEquals(1, g.indexFromSetName("00"));
+ assertEquals(null, g.indexFromSetName("abcde"));
+
+ // Verify that we get the label list back in the right order
+ assertEquals(["x", "00", "01", "10", "11"], g.getLabels());
+};
var pointsLength = points.length;
var point, i, j, prevX, prevY, prevYs, color, setName, newYs, err_color, rgb, yscale, axis;
- var setNames = [];
- for (var name in this.layout.datasets) {
- if (this.layout.datasets.hasOwnProperty(name)) {
- setNames.push(name);
- }
- }
+ var setNames = this.layout.setNames;
var setCount = setNames.length;
// TODO(danvk): Move this mapping into Dygraph and get it out of here.
var afterLastIndexInSet = 0;
var setLength = 0;
for (i = 0; i < setCount; i += 1) {
+ firstIndexInSet = this.layout.setPointsOffsets[i];
setLength = this.layout.setPointsLengths[i];
- afterLastIndexInSet += setLength;
+ afterLastIndexInSet = firstIndexInSet + setLength;
setName = setNames[i];
color = this.colors[setName];
var strokeWidth = this.dygraph_.attr_("strokeWidth", setName);
}
}
}
- firstIndexInSet = afterLastIndexInSet;
}
context.restore();
if (row < 0) return selection;
- var col = 1;
var datasets = this.date_graph.layout_.datasets;
- for (var k in datasets) {
- if (!datasets.hasOwnProperty(k)) continue;
- selection.push({row: row, column: col});
- col++;
+ for (var setIdx = 0; setIdx < datasets.length; ++setIdx) {
+ selection.push({row: row, column: setIdx + 1});
}
return selection;
};
-
var DygraphLayout = function(dygraph) {
this.dygraph_ = dygraph;
this.datasets = [];
+ this.setNames = [];
this.annotations = [];
this.yAxes_ = null;
};
DygraphLayout.prototype.addDataset = function(setname, set_xy) {
- this.datasets[setname] = set_xy;
+ this.datasets.push(set_xy);
+ this.setNames.push(setname);
};
DygraphLayout.prototype.getPlotArea = function() {
this.minxval = this.dateWindow_[0];
this.maxxval = this.dateWindow_[1];
} else {
- for (var name in this.datasets) {
- if (!this.datasets.hasOwnProperty(name)) continue;
- var series = this.datasets[name];
+ for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) {
+ var series = this.datasets[setIdx];
if (series.length > 1) {
var x1 = series[0][0];
if (!this.minxval || x1 < this.minxval) this.minxval = x1;
// for every data set since the points are added in order of the sets in
// datasets.
this.setPointsLengths = [];
+ this.setPointsOffsets = [];
- for (var setName in this.datasets) {
- if (!this.datasets.hasOwnProperty(setName)) continue;
-
- var dataset = this.datasets[setName];
+ for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) {
+ var dataset = this.datasets[setIdx];
+ var setName = this.setNames[setIdx];
var axis = this.dygraph_.axisPropertiesForSeries(setName);
+ this.setPointsOffsets.push(this.points.length);
var setPointsLength = 0;
for (var j = 0; j < dataset.length; j++) {
// Copy over the error terms
var i = 0; // index in this.points
- for (var setName in this.datasets) {
- if (!this.datasets.hasOwnProperty(setName)) continue;
+ for (var setIdx = 0; setIdx < this.datasets.length; ++setIdx) {
var j = 0;
- var dataset = this.datasets[setName];
+ var dataset = this.datasets[setIdx];
+ var setName = this.setNames[setIdx];
var axis = this.dygraph_.axisPropertiesForSeries(setName);
for (j = 0; j < dataset.length; j++, i++) {
var item = dataset[j];
*/
DygraphLayout.prototype.removeAllDatasets = function() {
delete this.datasets;
+ delete this.setNames;
+ delete this.setPointsLengths;
+ delete this.setPointsOffsets;
this.datasets = [];
+ this.setNames = [];
+ this.setPointsLengths = [];
+ this.setPointsOffsets = [];
};
/**
Dygraph.updateDeep(this.attrs_, Dygraph.DEFAULT_ATTRS);
this.boundaryIds_ = [];
+ this.setIndexByName_ = {};
// Create the containing DIV and other interactive elements
this.createInterface_();
}
}
if (boundaryIdx < 0) return -1;
- for (var name in this.layout_.datasets) {
- if (idx < this.layout_.datasets[name].length) {
+ for (var setIdx = 0; setIdx < this.layout_.datasets.length; ++setIdx) {
+ var set = this.layout_.datasets[setIdx];
+ if (idx < set.length) {
return this.boundaryIds_[boundaryIdx][0] + idx;
}
- idx -= this.layout_.datasets[name].length;
+ idx -= set.length;
}
return -1;
};
}
if (row !== false && row >= 0) {
- for (var i in this.layout_.datasets) {
- if (row < this.layout_.datasets[i].length) {
+ for (var setIdx = 0; setIdx < this.layout_.datasets.length; ++setIdx) {
+ var set = this.layout_.datasets[setIdx];
+ if (row < set.length) {
var point = this.layout_.points[pos+row];
if (this.attr_("stackedGraph")) {
this.selPoints_.push(point);
}
- pos += this.layout_.datasets[i].length;
+ pos += set.length;
}
}
var extremes = packed[1];
this.boundaryIds_ = packed[2];
+ this.setIndexByName_ = {};
+ var labels = this.attr_("labels");
+ if (labels.length > 0) {
+ this.setIndexByName_[labels[0]] = 0;
+ }
for (var i = 1; i < datasets.length; i++) {
+ this.setIndexByName_[labels[i]] = i;
if (!this.visibility()[i - 1]) continue;
- this.layout_.addDataset(this.attr_("labels")[i], datasets[i]);
+ this.layout_.addDataset(labels[i], datasets[i]);
}
this.computeYAxisRanges_(extremes);
};
/**
+ * Get the list of label names for this graph. The first column is the
+ * x-axis, so the data series names start at index 1.
+ */
+Dygraph.prototype.getLabels = function(name) {
+ return this.attr_("labels").slice();
+};
+
+/**
* Get the index of a series (column) given its name. The first column is the
* x-axis, so the data series start with index 1.
*/
Dygraph.prototype.indexFromSetName = function(name) {
- var labels = this.attr_("labels");
- for (var i = 0; i < labels.length; i++) {
- if (labels[i] == name) return i;
- }
- return null;
+ return this.setIndexByName_[name];
};
/**