3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
8 * @fileoverview Based on PlotKitLayout, but modified to meet the needs of
13 * Creates a new DygraphLayout object.
15 * This class contains all the data to be charted.
16 * It uses data coordinates, but also records the chart range (in data
17 * coordinates) and hence is able to calculate percentage positions ('In this
18 * view, Point A lies 25% down the x-axis.')
20 * Two things that it does not do are:
21 * 1. Record pixel coordinates for anything.
22 * 2. (oddly) determine anything about the layout of chart elements.
24 * The naming is a vestige of Dygraph's original PlotKit roots.
28 DygraphLayout
= function(dygraph
) {
29 this.dygraph_
= dygraph
;
30 this.datasets
= new Array();
31 this.annotations
= new Array();
34 // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and
35 // yticks are outputs. Clean this up.
40 DygraphLayout
.prototype.attr_
= function(name
) {
41 return this.dygraph_
.attr_(name
);
44 DygraphLayout
.prototype.addDataset
= function(setname
, set_xy
) {
45 this.datasets
[setname
] = set_xy
;
48 DygraphLayout
.prototype.getPlotArea
= function() {
49 return this.computePlotArea_();
52 // Compute the box which the chart should be drawn in. This is the canvas's
53 // box, less space needed for axis and chart labels.
54 DygraphLayout
.prototype.computePlotArea_
= function() {
56 // TODO(danvk): per-axis setting.
60 if (this.attr_('drawYAxis')) {
61 area
.x
= this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize');
64 area
.w
= this.dygraph_
.width_
- area
.x
- this.attr_('rightGap');
65 area
.h
= this.dygraph_
.height_
;
66 if (this.attr_('drawXAxis')) {
67 if (this.attr_('xAxisHeight')) {
68 area
.h
-= this.attr_('xAxisHeight');
70 area
.h
-= this.attr_('axisLabelFontSize') + 2 * this.attr_('axisTickSize');
74 // Shrink the drawing area to accomodate additional y-axes.
75 if (this.dygraph_
.numAxes() == 2) {
76 // TODO(danvk): per-axis setting.
77 area
.w
-= (this.attr_('yAxisLabelWidth') + 2 * this.attr_('axisTickSize'));
78 } else if (this.dygraph_
.numAxes() > 2) {
79 this.dygraph_
.error("Only two y-axes are supported at this time. (Trying " +
80 "to use " + this.dygraph_
.numAxes() + ")");
83 // Add space for chart labels: title, xlabel and ylabel.
84 if (this.attr_('title')) {
85 area
.h
-= this.attr_('titleHeight');
86 area
.y
+= this.attr_('titleHeight');
88 if (this.attr_('xlabel')) {
89 area
.h
-= this.attr_('xLabelHeight');
91 if (this.attr_('ylabel')) {
92 // It would make sense to shift the chart here to make room for the y-axis
93 // label, but the default yAxisLabelWidth is large enough that this results
94 // in overly-padded charts. The y-axis label should fit fine. If it
95 // doesn't, the yAxisLabelWidth option can be increased.
98 // Add space for range selector, if needed.
99 if (this.attr_('showRangeSelector')) {
100 area
.h
-= this.attr_('rangeSelectorHeight') + 4;
106 DygraphLayout
.prototype.setAnnotations
= function(ann
) {
107 // The Dygraph object's annotations aren't parsed. We parse them here and
108 // save a copy. If there is no parser, then the user must be using raw format.
109 this.annotations
= [];
110 var parse
= this.attr_('xValueParser') || function(x
) { return x
; };
111 for (var i
= 0; i
< ann
.length
; i
++) {
113 if (!ann
[i
].xval
&& !ann
[i
].x
) {
114 this.dygraph_
.error("Annotations must have an 'x' property");
118 !(ann
[i
].hasOwnProperty('width') &&
119 ann
[i
].hasOwnProperty('height'))) {
120 this.dygraph_
.error("Must set width and height when setting " +
121 "annotation.icon property");
124 Dygraph
.update(a
, ann
[i
]);
125 if (!a
.xval
) a
.xval
= parse(a
.x
);
126 this.annotations
.push(a
);
130 DygraphLayout
.prototype.setXTicks
= function(xTicks
) {
131 this.xTicks_
= xTicks
;
134 // TODO(danvk): add this to the Dygraph object's API or move it into Layout.
135 DygraphLayout
.prototype.setYAxes
= function (yAxes
) {
139 DygraphLayout
.prototype.setDateWindow
= function(dateWindow
) {
140 this.dateWindow_
= dateWindow
;
143 DygraphLayout
.prototype.evaluate
= function() {
144 this._evaluateLimits();
145 this._evaluateLineCharts();
146 this._evaluateLineTicks();
147 this._evaluateAnnotations();
150 DygraphLayout
.prototype._evaluateLimits
= function() {
151 this.minxval
= this.maxxval
= null;
152 if (this.dateWindow_
) {
153 this.minxval
= this.dateWindow_
[0];
154 this.maxxval
= this.dateWindow_
[1];
156 for (var name
in this.datasets
) {
157 if (!this.datasets
.hasOwnProperty(name
)) continue;
158 var series
= this.datasets
[name
];
159 if (series
.length
> 1) {
160 var x1
= series
[0][0];
161 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
163 var x2
= series
[series
.length
- 1][0];
164 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
168 this.xrange
= this.maxxval
- this.minxval
;
169 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
171 for (var i
= 0; i
< this.yAxes_
.length
; i
++) {
172 var axis
= this.yAxes_
[i
];
173 axis
.minyval
= axis
.computedValueRange
[0];
174 axis
.maxyval
= axis
.computedValueRange
[1];
175 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
176 axis
.yscale
= (axis
.yrange
!= 0 ? 1.0 / axis
.yrange
: 1.0);
178 if (axis
.g
.attr_("logscale")) {
179 axis
.ylogrange
= Dygraph
.log10(axis
.maxyval
) - Dygraph
.log10(axis
.minyval
);
180 axis
.ylogscale
= (axis
.ylogrange
!= 0 ? 1.0 / axis
.ylogrange
: 1.0);
181 if (!isFinite(axis
.ylogrange
) || isNaN(axis
.ylogrange
)) {
182 axis
.g
.error('axis ' + i
+ ' of graph at ' + axis
.g
+
183 ' can\'t be displayed in log scale for range [' +
184 axis
.minyval
+ ' - ' + axis
.maxyval
+ ']');
190 DygraphLayout
._calcYNormal
= function(axis
, value
) {
192 return 1.0 - ((Dygraph
.log10(value
) - Dygraph
.log10(axis
.minyval
)) * axis
.ylogscale
);
194 return 1.0 - ((value
- axis
.minyval
) * axis
.yscale
);
198 DygraphLayout
.prototype._evaluateLineCharts
= function() {
200 this.points
= new Array();
201 // An array to keep track of how many points will be drawn for each set.
202 // This will allow for the canvas renderer to not have to check every point
203 // for every data set since the points are added in order of the sets in
205 this.setPointsLengths
= new Array();
207 for (var setName
in this.datasets
) {
208 if (!this.datasets
.hasOwnProperty(setName
)) continue;
210 var dataset
= this.datasets
[setName
];
211 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
213 var setPointsLength
= 0;
215 for (var j
= 0; j
< dataset
.length
; j
++) {
216 var item
= dataset
[j
];
217 var xValue
= parseFloat(dataset
[j
][0]);
218 var yValue
= parseFloat(dataset
[j
][1]);
220 // Range from 0-1 where 0 represents left and 1 represents right.
221 var xNormal
= (xValue
- this.minxval
) * this.xscale
;
222 // Range from 0-1 where 0 represents top and 1 represents bottom
223 var yNormal
= DygraphLayout
._calcYNormal(axis
, yValue
);
233 this.points
.push(point
);
234 setPointsLength
+= 1;
236 this.setPointsLengths
.push(setPointsLength
);
240 DygraphLayout
.prototype._evaluateLineTicks
= function() {
241 this.xticks
= new Array();
242 for (var i
= 0; i
< this.xTicks_
.length
; i
++) {
243 var tick
= this.xTicks_
[i
];
244 var label
= tick
.label
;
245 var pos
= this.xscale
* (tick
.v
- this.minxval
);
246 if ((pos
>= 0.0) && (pos
<= 1.0)) {
247 this.xticks
.push([pos
, label
]);
251 this.yticks
= new Array();
252 for (var i
= 0; i
< this.yAxes_
.length
; i
++ ) {
253 var axis
= this.yAxes_
[i
];
254 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
255 var tick
= axis
.ticks
[j
];
256 var label
= tick
.label
;
257 var pos
= this.dygraph_
.toPercentYCoord(tick
.v
, i
);
258 if ((pos
>= 0.0) && (pos
<= 1.0)) {
259 this.yticks
.push([i
, pos
, label
]);
267 * Behaves the same way as PlotKit.Layout, but also copies the errors
270 DygraphLayout
.prototype.evaluateWithError
= function() {
272 if (!(this.attr_('errorBars') || this.attr_('customBars'))) return;
274 // Copy over the error terms
275 var i
= 0; // index in this.points
276 for (var setName
in this.datasets
) {
277 if (!this.datasets
.hasOwnProperty(setName
)) continue;
279 var dataset
= this.datasets
[setName
];
280 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
281 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
282 var item
= dataset
[j
];
283 var xv
= parseFloat(item
[0]);
284 var yv
= parseFloat(item
[1]);
286 if (xv
== this.points
[i
].xval
&&
287 yv
== this.points
[i
].yval
) {
288 var errorMinus
= parseFloat(item
[2]);
289 var errorPlus
= parseFloat(item
[3]);
291 var yv_minus
= yv
- errorMinus
;
292 var yv_plus
= yv
+ errorPlus
;
293 this.points
[i
].y_top
= DygraphLayout
._calcYNormal(axis
, yv_minus
);
294 this.points
[i
].y_bottom
= DygraphLayout
._calcYNormal(axis
, yv_plus
);
300 DygraphLayout
.prototype._evaluateAnnotations
= function() {
301 // Add the annotations to the point to which they belong.
302 // Make a map from (setName, xval) to annotation for quick lookups.
303 var annotations
= {};
304 for (var i
= 0; i
< this.annotations
.length
; i
++) {
305 var a
= this.annotations
[i
];
306 annotations
[a
.xval
+ "," + a
.series
] = a
;
309 this.annotated_points
= [];
311 // Exit the function early if there are no annotations.
312 if (!this.annotations
|| !this.annotations
.length
) {
316 // TODO(antrob): loop through annotations not points.
317 for (var i
= 0; i
< this.points
.length
; i
++) {
318 var p
= this.points
[i
];
319 var k
= p
.xval
+ "," + p
.name
;
320 if (k
in annotations
) {
321 p
.annotation
= annotations
[k
];
322 this.annotated_points
.push(p
);
328 * Convenience function to remove all the data sets from a graph
330 DygraphLayout
.prototype.removeAllDatasets
= function() {
331 delete this.datasets
;
332 this.datasets
= new Array();
336 * Return a copy of the point at the indicated index, with its yval unstacked.
337 * @param int index of point in layout_.points
339 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
340 var point
= this.points
[idx
];
342 // Clone the point since we modify it
343 var unstackedPoint
= {};
344 for (var i
in point
) {
345 unstackedPoint
[i
] = point
[i
];
348 if (!this.attr_("stackedGraph")) {
349 return unstackedPoint
;
352 // The unstacked yval is equal to the current yval minus the yval of the
353 // next point at the same xval.
354 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
355 if (this.points
[i
].xval
== point
.xval
) {
356 unstackedPoint
.yval
-= this.points
[i
].yval
;
361 return unstackedPoint
;