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.setAnnotations
= function(ann
) {
49 // The Dygraph object's annotations aren't parsed. We parse them here and
50 // save a copy. If there is no parser, then the user must be using raw format.
51 this.annotations
= [];
52 var parse
= this.attr_('xValueParser') || function(x
) { return x
; };
53 for (var i
= 0; i
< ann
.length
; i
++) {
55 if (!ann
[i
].xval
&& !ann
[i
].x
) {
56 this.dygraph_
.error("Annotations must have an 'x' property");
60 !(ann
[i
].hasOwnProperty('width') &&
61 ann
[i
].hasOwnProperty('height'))) {
62 this.dygraph_
.error("Must set width and height when setting " +
63 "annotation.icon property");
66 Dygraph
.update(a
, ann
[i
]);
67 if (!a
.xval
) a
.xval
= parse(a
.x
);
68 this.annotations
.push(a
);
72 DygraphLayout
.prototype.setXTicks
= function(xTicks
) {
73 this.xTicks_
= xTicks
;
76 // TODO(danvk): add this to the Dygraph object's API or move it into Layout.
77 DygraphLayout
.prototype.setYAxes
= function (yAxes
) {
81 DygraphLayout
.prototype.setDateWindow
= function(dateWindow
) {
82 this.dateWindow_
= dateWindow
;
85 DygraphLayout
.prototype.evaluate
= function() {
86 this._evaluateLimits();
87 this._evaluateLineCharts();
88 this._evaluateLineTicks();
89 this._evaluateAnnotations();
92 DygraphLayout
.prototype._evaluateLimits
= function() {
93 this.minxval
= this.maxxval
= null;
94 if (this.dateWindow_
) {
95 this.minxval
= this.dateWindow_
[0];
96 this.maxxval
= this.dateWindow_
[1];
98 for (var name
in this.datasets
) {
99 if (!this.datasets
.hasOwnProperty(name
)) continue;
100 var series
= this.datasets
[name
];
101 if (series
.length
> 1) {
102 var x1
= series
[0][0];
103 if (!this.minxval
|| x1
< this.minxval
) this.minxval
= x1
;
105 var x2
= series
[series
.length
- 1][0];
106 if (!this.maxxval
|| x2
> this.maxxval
) this.maxxval
= x2
;
110 this.xrange
= this.maxxval
- this.minxval
;
111 this.xscale
= (this.xrange
!= 0 ? 1/this.xrange
: 1.0);
113 for (var i
= 0; i
< this.yAxes_
.length
; i
++) {
114 var axis
= this.yAxes_
[i
];
115 axis
.minyval
= axis
.computedValueRange
[0];
116 axis
.maxyval
= axis
.computedValueRange
[1];
117 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
118 axis
.yscale
= (axis
.yrange
!= 0 ? 1.0 / axis
.yrange
: 1.0);
120 if (axis
.g
.attr_("logscale")) {
121 axis
.ylogrange
= Dygraph
.log10(axis
.maxyval
) - Dygraph
.log10(axis
.minyval
);
122 axis
.ylogscale
= (axis
.ylogrange
!= 0 ? 1.0 / axis
.ylogrange
: 1.0);
123 if (!isFinite(axis
.ylogrange
) || isNaN(axis
.ylogrange
)) {
124 axis
.g
.error('axis ' + i
+ ' of graph at ' + axis
.g
+
125 ' can\'t be displayed in log scale for range [' +
126 axis
.minyval
+ ' - ' + axis
.maxyval
+ ']');
132 DygraphLayout
._calcYNormal
= function(axis
, value
) {
134 return 1.0 - ((Dygraph
.log10(value
) - Dygraph
.log10(axis
.minyval
)) * axis
.ylogscale
);
136 return 1.0 - ((value
- axis
.minyval
) * axis
.yscale
);
140 DygraphLayout
.prototype._evaluateLineCharts
= function() {
142 this.points
= new Array();
143 // An array to keep track of how many points will be drawn for each set.
144 // This will allow for the canvas renderer to not have to check every point
145 // for every data set since the points are added in order of the sets in
147 this.setPointsLengths
= new Array();
149 for (var setName
in this.datasets
) {
150 if (!this.datasets
.hasOwnProperty(setName
)) continue;
152 var dataset
= this.datasets
[setName
];
153 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
155 var setPointsLength
= 0;
157 for (var j
= 0; j
< dataset
.length
; j
++) {
158 var item
= dataset
[j
];
159 var xValue
= parseFloat(dataset
[j
][0]);
160 var yValue
= parseFloat(dataset
[j
][1]);
162 // Range from 0-1 where 0 represents left and 1 represents right.
163 var xNormal
= (xValue
- this.minxval
) * this.xscale
;
164 // Range from 0-1 where 0 represents top and 1 represents bottom
165 var yNormal
= DygraphLayout
._calcYNormal(axis
, yValue
);
175 this.points
.push(point
);
176 setPointsLength
+= 1;
178 this.setPointsLengths
.push(setPointsLength
);
182 DygraphLayout
.prototype._evaluateLineTicks
= function() {
183 this.xticks
= new Array();
184 for (var i
= 0; i
< this.xTicks_
.length
; i
++) {
185 var tick
= this.xTicks_
[i
];
186 var label
= tick
.label
;
187 var pos
= this.xscale
* (tick
.v
- this.minxval
);
188 if ((pos
>= 0.0) && (pos
<= 1.0)) {
189 this.xticks
.push([pos
, label
]);
193 this.yticks
= new Array();
194 for (var i
= 0; i
< this.yAxes_
.length
; i
++ ) {
195 var axis
= this.yAxes_
[i
];
196 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
197 var tick
= axis
.ticks
[j
];
198 var label
= tick
.label
;
199 var pos
= this.dygraph_
.toPercentYCoord(tick
.v
, i
);
200 if ((pos
>= 0.0) && (pos
<= 1.0)) {
201 this.yticks
.push([i
, pos
, label
]);
209 * Behaves the same way as PlotKit.Layout, but also copies the errors
212 DygraphLayout
.prototype.evaluateWithError
= function() {
214 if (!(this.attr_('errorBars') || this.attr_('customBars'))) return;
216 // Copy over the error terms
217 var i
= 0; // index in this.points
218 for (var setName
in this.datasets
) {
219 if (!this.datasets
.hasOwnProperty(setName
)) continue;
221 var dataset
= this.datasets
[setName
];
222 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
223 for (var j
= 0; j
< dataset
.length
; j
++, i
++) {
224 var item
= dataset
[j
];
225 var xv
= parseFloat(item
[0]);
226 var yv
= parseFloat(item
[1]);
228 if (xv
== this.points
[i
].xval
&&
229 yv
== this.points
[i
].yval
) {
230 var errorMinus
= parseFloat(item
[2]);
231 var errorPlus
= parseFloat(item
[3]);
233 var yv_minus
= yv
- errorMinus
;
234 var yv_plus
= yv
+ errorPlus
;
235 this.points
[i
].y_top
= DygraphLayout
._calcYNormal(axis
, yv_minus
);
236 this.points
[i
].y_bottom
= DygraphLayout
._calcYNormal(axis
, yv_plus
);
242 DygraphLayout
.prototype._evaluateAnnotations
= function() {
243 // Add the annotations to the point to which they belong.
244 // Make a map from (setName, xval) to annotation for quick lookups.
245 var annotations
= {};
246 for (var i
= 0; i
< this.annotations
.length
; i
++) {
247 var a
= this.annotations
[i
];
248 annotations
[a
.xval
+ "," + a
.series
] = a
;
251 this.annotated_points
= [];
253 // Exit the function early if there are no annotations.
254 if (!this.annotations
|| !this.annotations
.length
) {
258 // TODO(antrob): loop through annotations not points.
259 for (var i
= 0; i
< this.points
.length
; i
++) {
260 var p
= this.points
[i
];
261 var k
= p
.xval
+ "," + p
.name
;
262 if (k
in annotations
) {
263 p
.annotation
= annotations
[k
];
264 this.annotated_points
.push(p
);
270 * Convenience function to remove all the data sets from a graph
272 DygraphLayout
.prototype.removeAllDatasets
= function() {
273 delete this.datasets
;
274 this.datasets
= new Array();
278 * Return a copy of the point at the indicated index, with its yval unstacked.
279 * @param int index of point in layout_.points
281 DygraphLayout
.prototype.unstackPointAtIndex
= function(idx
) {
282 var point
= this.points
[idx
];
284 // Clone the point since we modify it
285 var unstackedPoint
= {};
286 for (var i
in point
) {
287 unstackedPoint
[i
] = point
[i
];
290 if (!this.attr_("stackedGraph")) {
291 return unstackedPoint
;
294 // The unstacked yval is equal to the current yval minus the yval of the
295 // next point at the same xval.
296 for (var i
= idx
+1; i
< this.points
.length
; i
++) {
297 if (this.points
[i
].xval
== point
.xval
) {
298 unstackedPoint
.yval
-= this.points
[i
].yval
;
303 return unstackedPoint
;