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 var DygraphLayout
= (function() {
14 /*global Dygraph:false */
18 * Creates a new DygraphLayout object.
20 * This class contains all the data to be charted.
21 * It uses data coordinates, but also records the chart range (in data
22 * coordinates) and hence is able to calculate percentage positions ('In this
23 * view, Point A lies 25% down the x-axis.')
25 * Two things that it does not do are:
26 * 1. Record pixel coordinates for anything.
27 * 2. (oddly) determine anything about the layout of chart elements.
29 * The naming is a vestige of Dygraph's original PlotKit roots.
33 var DygraphLayout
= function(dygraph
) {
34 this.dygraph_
= dygraph
;
36 * Array of points for each series.
38 * [series index][row index in series] = |Point| structure,
39 * where series index refers to visible series only, and the
40 * point index is for the reduced set of points for the current
41 * zoom region (including one point just outside the window).
42 * All points in the same row index share the same X value.
44 * @type {Array.<Array.<Dygraph.PointType>>}
48 this.annotations
= [];
51 // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and
52 // yticks are outputs. Clean this up.
58 * Add points for a single series.
60 * @param {string} setname Name of the series.
61 * @param {Array.<Dygraph.PointType>} set_xy Points for the series.
63 DygraphLayout
.prototype.addDataset
= function(setname
, set_xy
) {
64 this.points
.push(set_xy
);
65 this.setNames
.push(setname
);
69 * Returns the box which the chart should be drawn in. This is the canvas's
70 * box, less space needed for the axis and chart labels.
72 * @return {{x: number, y: number, w: number, h: number}}
74 DygraphLayout
.prototype.getPlotArea
= function() {
78 // Compute the box which the chart should be drawn in. This is the canvas's
79 // box, less space needed for axis, chart labels, and other plug-ins.
80 // NOTE: This should only be called by Dygraph.predraw_().
81 DygraphLayout
.prototype.computePlotArea
= function() {
83 // TODO(danvk): per-axis setting.
88 area
.w
= this.dygraph_
.width_
- area
.x
- this.dygraph_
.getOption('rightGap');
89 area
.h
= this.dygraph_
.height_
;
91 // Let plugins reserve space.
93 chart_div
: this.dygraph_
.graphDiv
,
94 reserveSpaceLeft
: function(px
) {
105 reserveSpaceRight
: function(px
) {
107 x
: area
.x
+ area
.w
- px
,
115 reserveSpaceTop
: function(px
) {
126 reserveSpaceBottom
: function(px
) {
129 y
: area
.y
+ area
.h
- px
,
136 chartRect
: function() {
137 return {x
:area
.x
, y
:area
.y
, w
:area
.w
, h
:area
.h
};
140 this.dygraph_
.cascadeEvents_('layout', e
);
145 DygraphLayout
.prototype.setAnnotations
= function(ann
) {
146 // The Dygraph object's annotations aren't parsed. We parse them here and
147 // save a copy. If there is no parser, then the user must be using raw format.
148 this.annotations
= [];
149 var parse
= this.dygraph_
.getOption('xValueParser') || function(x
) { return x
; };
150 for (var i
= 0; i
< ann
.length
; i
++) {
152 if (!ann
[i
].xval
&& ann
[i
].x
=== undefined
) {
153 console
.error("Annotations must have an 'x' property");
157 !(ann
[i
].hasOwnProperty('width') &&
158 ann
[i
].hasOwnProperty('height'))) {
159 console
.error("Must set width and height when setting " +
160 "annotation.icon property");
163 Dygraph
.update(a
, ann
[i
]);
164 if (!a
.xval
) a
.xval
= parse(a
.x
);
165 this.annotations
.push(a
);
169 DygraphLayout
.prototype.setXTicks
= function(xTicks
) {
170 this.xTicks_
= xTicks
;
173 // TODO(danvk): add this to the Dygraph object's API or move it into Layout.
174 DygraphLayout
.prototype.setYAxes
= function (yAxes
) {
178 DygraphLayout
.prototype.evaluate
= function() {
180 this._evaluateLimits();
181 this._evaluateLineCharts();
182 this._evaluateLineTicks();
183 this._evaluateAnnotations();
186 DygraphLayout
.prototype._evaluateLimits
= function() {
187 var xlimits
= this.dygraph_
.xAxisRange();
188 this._xAxis
.minval
= xlimits
[0];
189 this._xAxis
.maxval
= xlimits
[1];
190 var xrange
= xlimits
[1] - xlimits
[0];
191 this._xAxis
.scale
= (xrange
!== 0 ? 1 / xrange
: 1.0);
193 if (this.dygraph_
.getOptionForAxis("logscale", 'x')) {
194 this._xAxis
.xlogrange
= Dygraph
.log10(this._xAxis
.maxval
) - Dygraph
.log10(this._xAxis
.minval
);
195 this._xAxis
.xlogscale
= (this._xAxis
.xlogrange
!== 0 ? 1.0 / this._xAxis
.xlogrange
: 1.0);
197 for (var i
= 0; i
< this.yAxes_
.length
; i
++) {
198 var axis
= this.yAxes_
[i
];
199 axis
.minyval
= axis
.computedValueRange
[0];
200 axis
.maxyval
= axis
.computedValueRange
[1];
201 axis
.yrange
= axis
.maxyval
- axis
.minyval
;
202 axis
.yscale
= (axis
.yrange
!== 0 ? 1.0 / axis
.yrange
: 1.0);
204 if (this.dygraph_
.getOption("logscale")) {
205 axis
.ylogrange
= Dygraph
.log10(axis
.maxyval
) - Dygraph
.log10(axis
.minyval
);
206 axis
.ylogscale
= (axis
.ylogrange
!== 0 ? 1.0 / axis
.ylogrange
: 1.0);
207 if (!isFinite(axis
.ylogrange
) || isNaN(axis
.ylogrange
)) {
208 console
.error('axis ' + i
+ ' of graph at ' + axis
.g
+
209 ' can\'t be displayed in log scale for range [' +
210 axis
.minyval
+ ' - ' + axis
.maxyval
+ ']');
216 DygraphLayout
.calcXNormal_
= function(value
, xAxis
, logscale
) {
218 return ((Dygraph
.log10(value
) - Dygraph
.log10(xAxis
.minval
)) * xAxis
.xlogscale
);
220 return (value
- xAxis
.minval
) * xAxis
.scale
;
225 * @param {DygraphAxisType} axis
226 * @param {number} value
227 * @param {boolean} logscale
230 DygraphLayout
.calcYNormal_
= function(axis
, value
, logscale
) {
232 var x
= 1.0 - ((Dygraph
.log10(value
) - Dygraph
.log10(axis
.minyval
)) * axis
.ylogscale
);
233 return isFinite(x
) ? x
: NaN
; // shim for v8 issue; see pull request 276
235 return 1.0 - ((value
- axis
.minyval
) * axis
.yscale
);
239 DygraphLayout
.prototype._evaluateLineCharts
= function() {
240 var isStacked
= this.dygraph_
.getOption("stackedGraph");
241 var isLogscaleForX
= this.dygraph_
.getOptionForAxis("logscale", 'x');
243 for (var setIdx
= 0; setIdx
< this.points
.length
; setIdx
++) {
244 var points
= this.points
[setIdx
];
245 var setName
= this.setNames
[setIdx
];
246 var connectSeparated
= this.dygraph_
.getOption('connectSeparatedPoints', setName
);
247 var axis
= this.dygraph_
.axisPropertiesForSeries(setName
);
248 // TODO (konigsberg): use optionsForAxis instead.
249 var logscale
= this.dygraph_
.attributes_
.getForSeries("logscale", setName
);
251 for (var j
= 0; j
< points
.length
; j
++) {
252 var point
= points
[j
];
254 // Range from 0-1 where 0 represents left and 1 represents right.
255 point
.x
= DygraphLayout
.calcXNormal_(point
.xval
, this._xAxis
, isLogscaleForX
);
256 // Range from 0-1 where 0 represents top and 1 represents bottom
257 var yval
= point
.yval
;
259 point
.y_stacked
= DygraphLayout
.calcYNormal_(
260 axis
, point
.yval_stacked
, logscale
);
261 if (yval
!== null && !isNaN(yval
)) {
262 yval
= point
.yval_stacked
;
267 if (!connectSeparated
) {
271 point
.y
= DygraphLayout
.calcYNormal_(axis
, yval
, logscale
);
274 this.dygraph_
.dataHandler_
.onLineEvaluated(points
, axis
, logscale
);
278 DygraphLayout
.prototype._evaluateLineTicks
= function() {
279 var i
, tick
, label
, pos
;
281 for (i
= 0; i
< this.xTicks_
.length
; i
++) {
282 tick
= this.xTicks_
[i
];
284 pos
= this.dygraph_
.toPercentXCoord(tick
.v
);
285 if ((pos
>= 0.0) && (pos
< 1.0)) {
286 this.xticks
.push([pos
, label
]);
291 for (i
= 0; i
< this.yAxes_
.length
; i
++ ) {
292 var axis
= this.yAxes_
[i
];
293 for (var j
= 0; j
< axis
.ticks
.length
; j
++) {
294 tick
= axis
.ticks
[j
];
296 pos
= this.dygraph_
.toPercentYCoord(tick
.v
, i
);
297 if ((pos
> 0.0) && (pos
<= 1.0)) {
298 this.yticks
.push([i
, pos
, label
]);
304 DygraphLayout
.prototype._evaluateAnnotations
= function() {
305 // Add the annotations to the point to which they belong.
306 // Make a map from (setName, xval) to annotation for quick lookups.
308 var annotations
= {};
309 for (i
= 0; i
< this.annotations
.length
; i
++) {
310 var a
= this.annotations
[i
];
311 annotations
[a
.xval
+ "," + a
.series
] = a
;
314 this.annotated_points
= [];
316 // Exit the function early if there are no annotations.
317 if (!this.annotations
|| !this.annotations
.length
) {
321 // TODO(antrob): loop through annotations not points.
322 for (var setIdx
= 0; setIdx
< this.points
.length
; setIdx
++) {
323 var points
= this.points
[setIdx
];
324 for (i
= 0; i
< points
.length
; i
++) {
326 var k
= p
.xval
+ "," + p
.name
;
327 if (k
in annotations
) {
328 p
.annotation
= annotations
[k
];
329 this.annotated_points
.push(p
);
336 * Convenience function to remove all the data sets from a graph
338 DygraphLayout
.prototype.removeAllDatasets
= function() {
340 delete this.setNames
;
341 delete this.setPointsLengths
;
342 delete this.setPointsOffsets
;
345 this.setPointsLengths
= [];
346 this.setPointsOffsets
= [];
349 return DygraphLayout
;