dingle quotes.
[dygraphs.git] / dygraph-layout.js
CommitLineData
88e95c46
DV
1/**
2 * @license
3 * Copyright 2011 Dan Vanderkam (danvdk@gmail.com)
4 * MIT-licensed (http://opensource.org/licenses/MIT)
5 */
74a5af31
DV
6
7/**
8 * @fileoverview Based on PlotKitLayout, but modified to meet the needs of
9 * dygraphs.
10 */
11
758a629f
DV
12/*jshint globalstrict: true */
13/*global Dygraph:false */
c0f54d4f
DV
14"use strict";
15
74a5af31
DV
16/**
17 * Creates a new DygraphLayout object.
18 *
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.')
23 *
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.
27 *
28 * The naming is a vestige of Dygraph's original PlotKit roots.
29 *
30 * @constructor
31 */
c0f54d4f 32var DygraphLayout = function(dygraph) {
74a5af31 33 this.dygraph_ = dygraph;
30a5cfc6
KW
34 /**
35 * Array of points for each series.
36 *
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.
42 *
43 * @type {Array.<Array.<Dygraph.PointType>>}
44 */
45 this.points = [];
82c6fe4d 46 this.setNames = [];
758a629f 47 this.annotations = [];
74a5af31
DV
48 this.yAxes_ = null;
49
50 // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and
51 // yticks are outputs. Clean this up.
52 this.xTicks_ = null;
53 this.yTicks_ = null;
54};
55
30a5cfc6
KW
56/**
57 * Add points for a single series.
58 *
59 * @param {string} setname Name of the series.
60 * @param {Array.<Dygraph.PointType>} set_xy Points for the series.
61 */
74a5af31 62DygraphLayout.prototype.addDataset = function(setname, set_xy) {
30a5cfc6 63 this.points.push(set_xy);
82c6fe4d 64 this.setNames.push(setname);
74a5af31
DV
65};
66
141064ff
DE
67/**
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.
70 *
71 * @return {{x: number, y: number, w: number, h: number}}
72 */
70be5ed1 73DygraphLayout.prototype.getPlotArea = function() {
0d216a60 74 return this.area_;
758a629f 75};
70be5ed1 76
ccd9d7c2 77// Compute the box which the chart should be drawn in. This is the canvas's
ad617f17 78// box, less space needed for axis, chart labels, and other plug-ins.
ec475f44 79// NOTE: This should only be called by Dygraph.predraw_().
0d216a60 80DygraphLayout.prototype.computePlotArea = function() {
ccd9d7c2
PF
81 var area = {
82 // TODO(danvk): per-axis setting.
83 x: 0,
84 y: 0
85 };
f8540c66 86
7e64db42 87 area.w = this.dygraph_.width_ - area.x - this.dygraph_.getOption('rightGap');
ccd9d7c2 88 area.h = this.dygraph_.height_;
6dca682f
DV
89
90 // Let plugins reserve space.
91 var e = {
92 chart_div: this.dygraph_.graphDiv,
93 reserveSpaceLeft: function(px) {
94 var r = {
95 x: area.x,
96 y: area.y,
97 w: px,
98 h: area.h
99 };
100 area.x += px;
101 area.w -= px;
102 return r;
103 },
1c177b6a
DV
104 reserveSpaceRight: function(px) {
105 var r = {
106 x: area.x + area.w - px,
107 y: area.y,
108 w: px,
109 h: area.h
110 };
111 area.w -= px;
112 return r;
113 },
6dca682f
DV
114 reserveSpaceTop: function(px) {
115 var r = {
116 x: area.x,
117 y: area.y,
118 w: area.w,
119 h: px
120 };
121 area.y += px;
122 area.h -= px;
123 return r;
124 },
1c177b6a
DV
125 reserveSpaceBottom: function(px) {
126 var r = {
127 x: area.x,
128 y: area.y + area.h - px,
129 w: area.w,
130 h: px
131 };
132 area.h -= px;
133 return r;
134 },
6dca682f
DV
135 chartRect: function() {
136 return {x:area.x, y:area.y, w:area.w, h:area.h};
137 }
138 };
139 this.dygraph_.cascadeEvents_('layout', e);
140
0d216a60 141 this.area_ = area;
ccd9d7c2
PF
142};
143
74a5af31
DV
144DygraphLayout.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 = [];
7e64db42 148 var parse = this.dygraph_.getOption('xValueParser') || function(x) { return x; };
74a5af31
DV
149 for (var i = 0; i < ann.length; i++) {
150 var a = {};
b5481aea 151 if (!ann[i].xval && ann[i].x === undefined) {
464b5f50 152 Dygraph.error("Annotations must have an 'x' property");
74a5af31
DV
153 return;
154 }
155 if (ann[i].icon &&
156 !(ann[i].hasOwnProperty('width') &&
157 ann[i].hasOwnProperty('height'))) {
464b5f50
DV
158 Dygraph.error("Must set width and height when setting " +
159 "annotation.icon property");
74a5af31
DV
160 return;
161 }
162 Dygraph.update(a, ann[i]);
163 if (!a.xval) a.xval = parse(a.x);
164 this.annotations.push(a);
165 }
166};
167
168DygraphLayout.prototype.setXTicks = function(xTicks) {
169 this.xTicks_ = xTicks;
170};
171
172// TODO(danvk): add this to the Dygraph object's API or move it into Layout.
173DygraphLayout.prototype.setYAxes = function (yAxes) {
174 this.yAxes_ = yAxes;
175};
176
74a5af31 177DygraphLayout.prototype.evaluate = function() {
5b9b2142 178 this._xAxis = {};
74a5af31
DV
179 this._evaluateLimits();
180 this._evaluateLineCharts();
181 this._evaluateLineTicks();
182 this._evaluateAnnotations();
183};
184
185DygraphLayout.prototype._evaluateLimits = function() {
fa460473 186 var xlimits = this.dygraph_.xAxisRange();
5b9b2142
RK
187 this._xAxis.minxval = xlimits[0];
188 this._xAxis.maxxval = xlimits[1];
fa460473 189 var xrange = xlimits[1] - xlimits[0];
5b9b2142 190 this._xAxis.xscale = (xrange !== 0 ? 1 / xrange : 1.0);
74a5af31 191
5b9b2142
RK
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);
195 }
74a5af31
DV
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;
758a629f 201 axis.yscale = (axis.yrange !== 0 ? 1.0 / axis.yrange : 1.0);
74a5af31 202
5b9b2142 203 if (this.dygraph_.getOption("logscale")) {
74a5af31 204 axis.ylogrange = Dygraph.log10(axis.maxyval) - Dygraph.log10(axis.minyval);
758a629f 205 axis.ylogscale = (axis.ylogrange !== 0 ? 1.0 / axis.ylogrange : 1.0);
74a5af31 206 if (!isFinite(axis.ylogrange) || isNaN(axis.ylogrange)) {
464b5f50
DV
207 Dygraph.error('axis ' + i + ' of graph at ' + axis.g +
208 ' can\'t be displayed in log scale for range [' +
209 axis.minyval + ' - ' + axis.maxyval + ']');
74a5af31
DV
210 }
211 }
212 }
213};
214
335011fd 215DygraphLayout.calcXNormal_ = function(value, axis, logscale) {
5b9b2142
RK
216 if (logscale) {
217 return ((Dygraph.log10(value) - Dygraph.log10(axis.minxval)) * axis.xlogscale);
218 } else {
219 return (value - axis.minxval) * axis.xscale;
220 }
221};
222
e956b69b
DV
223/**
224 * @param {DygraphAxisType} axis
225 * @param {number} value
226 * @param {boolean} logscale
227 * @return {number}
228 */
229DygraphLayout.calcYNormal_ = function(axis, value, logscale) {
23db4e07 230 if (logscale) {
b7ec6c55 231 var x = 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale);
40a54d29 232 return isFinite(x) ? x : NaN; // shim for v8 issue; see pull request 276
7f028980
DV
233 } else {
234 return 1.0 - ((value - axis.minyval) * axis.yscale);
235 }
236};
237
74a5af31 238DygraphLayout.prototype._evaluateLineCharts = function() {
7e64db42 239 var isStacked = this.dygraph_.getOption("stackedGraph");
5b9b2142 240 var isLogscaleForX = this.dygraph_.getOptionForAxis("logscale", 'x');
a12a78ae 241
30a5cfc6
KW
242 for (var setIdx = 0; setIdx < this.points.length; setIdx++) {
243 var points = this.points[setIdx];
82c6fe4d 244 var setName = this.setNames[setIdx];
7e64db42 245 var connectSeparated = this.dygraph_.getOption('connectSeparatedPoints', setName);
74a5af31 246 var axis = this.dygraph_.axisPropertiesForSeries(setName);
23db4e07 247 // TODO (konigsberg): use optionsForAxis instead.
8e19509a 248 var logscale = this.dygraph_.attributes_.getForSeries("logscale", setName);
74a5af31 249
30a5cfc6
KW
250 for (var j = 0; j < points.length; j++) {
251 var point = points[j];
ecf9b464 252
4ab8db0c 253 // Range from 0-1 where 0 represents left and 1 represents right.
335011fd 254 point.x = DygraphLayout.calcXNormal_(point.xval, this._xAxis, isLogscaleForX);
7f028980 255 // Range from 0-1 where 0 represents top and 1 represents bottom
30a5cfc6
KW
256 var yval = point.yval;
257 if (isStacked) {
e956b69b 258 point.y_stacked = DygraphLayout.calcYNormal_(
30a5cfc6
KW
259 axis, point.yval_stacked, logscale);
260 if (yval !== null && !isNaN(yval)) {
261 yval = point.yval_stacked;
262 }
263 }
264 if (yval === null) {
265 yval = NaN;
266 if (!connectSeparated) {
267 point.yval = NaN;
268 }
269 }
e956b69b 270 point.y = DygraphLayout.calcYNormal_(axis, yval, logscale);
74a5af31 271 }
a49c164a
DE
272
273 this.dygraph_.dataHandler_.onLineEvaluated(points, axis, logscale);
74a5af31
DV
274 }
275};
276
277DygraphLayout.prototype._evaluateLineTicks = function() {
758a629f
DV
278 var i, tick, label, pos;
279 this.xticks = [];
280 for (i = 0; i < this.xTicks_.length; i++) {
281 tick = this.xTicks_[i];
282 label = tick.label;
5b9b2142 283 pos = this.dygraph_.toPercentXCoord(tick.v);
9146b6c0 284 if ((pos >= 0.0) && (pos < 1.0)) {
74a5af31
DV
285 this.xticks.push([pos, label]);
286 }
287 }
288
758a629f
DV
289 this.yticks = [];
290 for (i = 0; i < this.yAxes_.length; i++ ) {
74a5af31
DV
291 var axis = this.yAxes_[i];
292 for (var j = 0; j < axis.ticks.length; j++) {
758a629f
DV
293 tick = axis.ticks[j];
294 label = tick.label;
295 pos = this.dygraph_.toPercentYCoord(tick.v, i);
9146b6c0 296 if ((pos > 0.0) && (pos <= 1.0)) {
74a5af31
DV
297 this.yticks.push([i, pos, label]);
298 }
299 }
300 }
301};
302
74a5af31
DV
303DygraphLayout.prototype._evaluateAnnotations = function() {
304 // Add the annotations to the point to which they belong.
305 // Make a map from (setName, xval) to annotation for quick lookups.
758a629f 306 var i;
74a5af31 307 var annotations = {};
758a629f 308 for (i = 0; i < this.annotations.length; i++) {
74a5af31
DV
309 var a = this.annotations[i];
310 annotations[a.xval + "," + a.series] = a;
311 }
312
313 this.annotated_points = [];
d570a072
AR
314
315 // Exit the function early if there are no annotations.
316 if (!this.annotations || !this.annotations.length) {
317 return;
318 }
ccd9d7c2 319
d570a072 320 // TODO(antrob): loop through annotations not points.
a12a78ae
DV
321 for (var setIdx = 0; setIdx < this.points.length; setIdx++) {
322 var points = this.points[setIdx];
323 for (i = 0; i < points.length; i++) {
324 var p = points[i];
325 var k = p.xval + "," + p.name;
326 if (k in annotations) {
327 p.annotation = annotations[k];
328 this.annotated_points.push(p);
329 }
74a5af31
DV
330 }
331 }
332};
333
334/**
335 * Convenience function to remove all the data sets from a graph
336 */
337DygraphLayout.prototype.removeAllDatasets = function() {
30a5cfc6 338 delete this.points;
82c6fe4d
KW
339 delete this.setNames;
340 delete this.setPointsLengths;
341 delete this.setPointsOffsets;
30a5cfc6 342 this.points = [];
82c6fe4d
KW
343 this.setNames = [];
344 this.setPointsLengths = [];
345 this.setPointsOffsets = [];
74a5af31 346};