2e81359305952ebe51a0d316bfc024dfebf06f58
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
12 /*jshint globalstrict: true */
13 /*global Dygraph:false */
17 * Creates a new DygraphLayout object.
19 * This class contains all the data to be charted.
20 * It uses data coordinates, but also records the chart range (in data
21 * coordinates) and hence is able to calculate percentage positions ('In this
22 * view, Point A lies 25% down the x-axis.')
24 * Two things that it does not do are:
25 * 1. Record pixel coordinates for anything.
26 * 2. (oddly) determine anything about the layout of chart elements.
28 * The naming is a vestige of Dygraph's original PlotKit roots.
32 var DygraphLayout
= function(dygraph
) {
33 this.dygraph_
= dygraph
;
35 * Array of points for each series.
37 * [series index][row index in series] = |Point| structure,
38 * where series index refers to visible series only, and the
39 * point index is for the reduced set of points for the current
40 * zoom region (including one point just outside the window).
41 * All points in the same row index share the same X value.
43 * @type {Array.<Array.<Dygraph.PointType>>}
47 this.annotations
= [];
50 // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and
51 // yticks are outputs. Clean this up.
57 * Add points for a single series.
59 * @param {string} setname Name of the series.
60 * @param {Array.<Dygraph.PointType>} set_xy Points for the series.
62 DygraphLayout
.prototype.addDataset
= function(setname
, set_xy
) {
63 this.points
.push(set_xy
);
64 this.setNames
.push(setname
);
68 * Returns the box which the chart should be drawn in. This is the canvas's
69 * box, less space needed for the axis and chart labels.
71 * @return {{x: number, y: number, w: number, h: number}}
73 DygraphLayout
.prototype.getPlotArea
= function() {
77 // Compute the box which the chart should be drawn in. This is the canvas's
78 // box, less space needed for axis, chart labels, and other plug-ins.
79 // NOTE: This should only be called by Dygraph.predraw_().
80 DygraphLayout
.prototype.computePlotArea
= function() {
82 // TODO(danvk): per-axis setting.
87 area
.w
= this.dygraph_
.width_
- area
.x
- this.dygraph_
.getOption('rightGap');
88 area
.h
= this.dygraph_
.height_
;
90 // Let plugins reserve space.
92 chart_div
: this.dygraph_
.graphDiv
,
93 reserveSpaceLeft
: function(px
) {
104 reserveSpaceRight
: function(px
) {
106 x
: area
.x
+ area
.w
- px
,
114 reserveSpaceTop
: function(px
) {
125 reserveSpaceBottom
: function(px
) {
128 y
: area
.y
+ area
.h
- px
,
135 chartRect
: function() {
136 return {x
:area
.x
, y
:area
.y
, w
:area
.w
, h
:area
.h
};
139 this.dygraph_
.cascadeEvents_('layout', e
);
144 DygraphLayout
.prototype.setAnnotations
= function(ann
) {
145 // The Dygraph object's annotations aren't parsed. We parse them here and
146 // save a copy. If there is no parser, then the user must be using raw format.
147 this.annotations
= [];
148 var parse
= this.dygraph_
.getOption('xValueParser') || function(x
) { return x
; };
149 for (var i
= 0; i
< ann
.length
; i
++) {
151 if (!ann
[i
].xval
&& ann
[i
].x
=== undefined
) {
152 this.dygraph_
.error("Annotations must have an 'x' property");
156 !(ann
[i
].hasOwnProperty('width') &&
157 ann
[i
].hasOwnProperty('height'))) {
158 this.dygraph_
.error("Must set width and height when setting " +
159 "annotation.icon property");
162 Dygraph
.update(a
, ann
[i
]);
163 if (!a
.xval
) a
.xval
= parse(a
.x
);
164 this.annotations
.push(a
);
168 DygraphLayout
.prototype.setXTicks
= function(xTicks
) {
169 this.xTicks_
= xTicks
;
172 // TODO(danvk): add this to the Dygraph object's API or move it into Layout.
173 DygraphLayout
.prototype.setYAxes
= function (yAxes
) {
177 DygraphLayout
.prototype.evaluate
= function() {
179 this._evaluateLimits();
180 this._evaluateLineCharts();
181 this._evaluateLineTicks();
182 this._evaluateAnnotations();
185 DygraphLayout
.prototype._evaluateLimits
= function() {
186 var xlimits
= this.dygraph_
.xAxisRange();
187 this._xAxis
.minxval
= xlimits
[0];
188 this._xAxis
.maxxval
= xlimits
[1];
189 var xrange
= xlimits
[1] - xlimits
[0];
190 this._xAxis
.xscale
= (xrange
!== 0 ? 1 / xrange
: 1.0);
192 if (this.dygraph_
.getOptionForAxis("logscale", 'x')) {
193 this._xAxis
.xlogrange
= Dygraph
.log10(this._xAxis
.maxxval
) - Dygraph
.log10(this._xAxis
.minxval
);
194 this._xAxis
.xlogscale
= (this._xAxis
.xlogrange
!== 0 ? 1.0 / this._xAxis
.xlogrange
: 1.0);
196 for (var i
= 0; i
< this.yAxes_
.length
; i
++) {
197 var axis
= this.yAxes_
[i
];
198 axis
.minyval
= axis
.computedValueRange
[0];
199 axis
.maxyval
= axis
.computedValueRange
[1];
200 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
201 axis
.yscale
= (axis
.yrange
!== 0 ? 1.0 / axis
.yrange
: 1.0);
203 if (this.dygraph_
.getOption("logscale")) {
204 axis
.ylogrange
= Dygraph
.log10(axis
.maxyval
) - Dygraph
.log10(axis
.minyval
);
205 axis
.ylogscale
= (axis
.ylogrange
!== 0 ? 1.0 / axis
.ylogrange
: 1.0);
206 if (!isFinite(axis
.ylogrange
) || isNaN(axis
.ylogrange
)) {
207 axis
.g
.error('axis ' + i
+ ' of graph at ' + axis
.g
+
208 ' can\'t be displayed in log scale for range [' +
209 axis
.minyval
+ ' - ' + axis
.maxyval
+ ']');
215 DygraphLayout
._calcXNormal
= function(value
, axis
, logscale
) {
217 return ((Dygraph
.log10(value
) - Dygraph
.log10(axis
.minxval
)) * axis
.xlogscale
);
219 return (value
- axis
.minxval
) * axis
.xscale
;
223 DygraphLayout
._calcYNormal
= function(axis
, value
, logscale
) {
225 return 1.0 - ((Dygraph
.log10(value
) - Dygraph
.log10(axis
.minyval
)) * axis
.ylogscale
);
227 return 1.0 - ((value
- axis
.minyval
) * axis
.yscale
);
231 DygraphLayout
.prototype._evaluateLineCharts
= function() {
232 var isStacked
= this.dygraph_
.getOption("stackedGraph");
233 var isLogscaleForX
= this.dygraph_
.getOptionForAxis("logscale", 'x');
235 for (var setIdx
= 0; setIdx
< this.points
.length
; setIdx
++) {
236 var points
= this.points
[setIdx
];
237 var setName
= this.setNames
[setIdx
];
238 var connectSeparated
= this.dygraph_
.getOption('connectSeparatedPoints', setName
);
239 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
240 // TODO (konigsberg): use optionsForAxis instead.
241 var logscale
= this.dygraph_
.attributes_
.getForSeries("logscale", setName
);
243 for (var j
= 0; j
< points
.length
; j
++) {
244 var point
= points
[j
];
246 // Range from 0-1 where 0 represents left and 1 represents right.
247 point
.x
= DygraphLayout
._calcXNormal(point
.xval
, this._xAxis
, isLogscaleForX
);
248 // Range from 0-1 where 0 represents top and 1 represents bottom
249 var yval
= point
.yval
;
251 point
.y_stacked
= DygraphLayout
._calcYNormal(
252 axis
, point
.yval_stacked
, logscale
);
253 if (yval
!== null && !isNaN(yval
)) {
254 yval
= point
.yval_stacked
;
259 if (!connectSeparated
) {
263 point
.y
= DygraphLayout
._calcYNormal(axis
, yval
, logscale
);
266 this.dygraph_
.dataHandler_
.onLineEvaluated(points
, axis
, logscale
);
271 * Optimized replacement for parseFloat, which was way too slow when almost
272 * all values were type number, with few edge cases, none of which were strings.
274 DygraphLayout
.parseFloat_
= function(val
) {
275 // parseFloat(null) is NaN
280 // Assume it's a number or NaN. If it's something else, I'll be shocked.
284 DygraphLayout
.prototype._evaluateLineTicks
= function() {
285 var i
, tick
, label
, pos
;
287 for (i
= 0; i
< this.xTicks_
.length
; i
++) {
288 tick
= this.xTicks_
[i
];
290 pos
= this.dygraph_
.toPercentXCoord(tick
.v
);
291 if ((pos
>= 0.0) && (pos
<= 1.0)) {
292 this.xticks
.push([pos
, label
]);
297 for (i
= 0; i
< this.yAxes_
.length
; i
++ ) {
298 var axis
= this.yAxes_
[i
];
299 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
300 tick
= axis
.ticks
[j
];
302 pos
= this.dygraph_
.toPercentYCoord(tick
.v
, i
);
303 if ((pos
>= 0.0) && (pos
<= 1.0)) {
304 this.yticks
.push([i
, pos
, label
]);
310 DygraphLayout
.prototype._evaluateAnnotations
= function() {
311 // Add the annotations to the point to which they belong.
312 // Make a map from (setName, xval) to annotation for quick lookups.
314 var annotations
= {};
315 for (i
= 0; i
< this.annotations
.length
; i
++) {
316 var a
= this.annotations
[i
];
317 annotations
[a
.xval
+ "," + a
.series
] = a
;
320 this.annotated_points
= [];
322 // Exit the function early if there are no annotations.
323 if (!this.annotations
|| !this.annotations
.length
) {
327 // TODO(antrob): loop through annotations not points.
328 for (var setIdx
= 0; setIdx
< this.points
.length
; setIdx
++) {
329 var points
= this.points
[setIdx
];
330 for (i
= 0; i
< points
.length
; i
++) {
332 var k
= p
.xval
+ "," + p
.name
;
333 if (k
in annotations
) {
334 p
.annotation
= annotations
[k
];
335 this.annotated_points
.push(p
);
342 * Convenience function to remove all the data sets from a graph
344 DygraphLayout
.prototype.removeAllDatasets
= function() {
346 delete this.setNames
;
347 delete this.setPointsLengths
;
348 delete this.setPointsOffsets
;
351 this.setPointsLengths
= [];
352 this.setPointsOffsets
= [];