Commit | Line | Data |
---|---|---|
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 | ||
12 | /** | |
13 | * Creates a new DygraphLayout object. | |
14 | * | |
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.') | |
19 | * | |
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. | |
23 | * | |
24 | * The naming is a vestige of Dygraph's original PlotKit roots. | |
25 | * | |
26 | * @constructor | |
27 | */ | |
28 | DygraphLayout = function(dygraph) { | |
29 | this.dygraph_ = dygraph; | |
30 | this.datasets = new Array(); | |
31 | this.annotations = new Array(); | |
32 | this.yAxes_ = null; | |
33 | ||
34 | // TODO(danvk): it's odd that xTicks_ and yTicks_ are inputs, but xticks and | |
35 | // yticks are outputs. Clean this up. | |
36 | this.xTicks_ = null; | |
37 | this.yTicks_ = null; | |
38 | }; | |
39 | ||
40 | DygraphLayout.prototype.attr_ = function(name) { | |
41 | return this.dygraph_.attr_(name); | |
42 | }; | |
43 | ||
44 | DygraphLayout.prototype.addDataset = function(setname, set_xy) { | |
45 | this.datasets[setname] = set_xy; | |
46 | }; | |
47 | ||
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++) { | |
54 | var a = {}; | |
55 | if (!ann[i].xval && !ann[i].x) { | |
56 | this.dygraph_.error("Annotations must have an 'x' property"); | |
57 | return; | |
58 | } | |
59 | if (ann[i].icon && | |
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"); | |
64 | return; | |
65 | } | |
66 | Dygraph.update(a, ann[i]); | |
67 | if (!a.xval) a.xval = parse(a.x); | |
68 | this.annotations.push(a); | |
69 | } | |
70 | }; | |
71 | ||
72 | DygraphLayout.prototype.setXTicks = function(xTicks) { | |
73 | this.xTicks_ = xTicks; | |
74 | }; | |
75 | ||
76 | // TODO(danvk): add this to the Dygraph object's API or move it into Layout. | |
77 | DygraphLayout.prototype.setYAxes = function (yAxes) { | |
78 | this.yAxes_ = yAxes; | |
79 | }; | |
80 | ||
81 | DygraphLayout.prototype.setDateWindow = function(dateWindow) { | |
82 | this.dateWindow_ = dateWindow; | |
83 | }; | |
84 | ||
85 | DygraphLayout.prototype.evaluate = function() { | |
86 | this._evaluateLimits(); | |
87 | this._evaluateLineCharts(); | |
88 | this._evaluateLineTicks(); | |
89 | this._evaluateAnnotations(); | |
90 | }; | |
91 | ||
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]; | |
97 | } else { | |
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; | |
104 | ||
105 | var x2 = series[series.length - 1][0]; | |
106 | if (!this.maxxval || x2 > this.maxxval) this.maxxval = x2; | |
107 | } | |
108 | } | |
109 | } | |
110 | this.xrange = this.maxxval - this.minxval; | |
111 | this.xscale = (this.xrange != 0 ? 1/this.xrange : 1.0); | |
112 | ||
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); | |
119 | ||
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 + ']'); | |
127 | } | |
128 | } | |
129 | } | |
130 | }; | |
131 | ||
7f028980 DV |
132 | DygraphLayout._calcYNormal = function(axis, value) { |
133 | if (axis.logscale) { | |
134 | return 1.0 - ((Dygraph.log10(value) - Dygraph.log10(axis.minyval)) * axis.ylogscale); | |
135 | } else { | |
136 | return 1.0 - ((value - axis.minyval) * axis.yscale); | |
137 | } | |
138 | }; | |
139 | ||
74a5af31 DV |
140 | DygraphLayout.prototype._evaluateLineCharts = function() { |
141 | // add all the rects | |
142 | this.points = new Array(); | |
c3e1495b AR |
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 | |
fc77253b DV |
145 | // for every data set since the points are added in order of the sets in |
146 | // datasets. | |
c3e1495b AR |
147 | this.setPointsLengths = new Array(); |
148 | ||
74a5af31 DV |
149 | for (var setName in this.datasets) { |
150 | if (!this.datasets.hasOwnProperty(setName)) continue; | |
151 | ||
152 | var dataset = this.datasets[setName]; | |
153 | var axis = this.dygraph_.axisPropertiesForSeries(setName); | |
154 | ||
c3e1495b | 155 | var setPointsLength = 0; |
ecf9b464 | 156 | |
74a5af31 DV |
157 | for (var j = 0; j < dataset.length; j++) { |
158 | var item = dataset[j]; | |
4ab8db0c AR |
159 | var xValue = parseFloat(dataset[j][0]); |
160 | var yValue = parseFloat(dataset[j][1]); | |
ecf9b464 | 161 | |
4ab8db0c | 162 | // Range from 0-1 where 0 represents left and 1 represents right. |
7f028980 DV |
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); | |
166 | ||
f9414b11 DV |
167 | var point = { |
168 | // TODO(danvk): here | |
169 | x: xNormal, | |
170 | y: yNormal, | |
171 | xval: xValue, | |
172 | yval: yValue, | |
173 | name: setName | |
174 | }; | |
175 | this.points.push(point); | |
176 | setPointsLength += 1; | |
74a5af31 | 177 | } |
c3e1495b | 178 | this.setPointsLengths.push(setPointsLength); |
74a5af31 DV |
179 | } |
180 | }; | |
181 | ||
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]); | |
190 | } | |
191 | } | |
192 | ||
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]); | |
202 | } | |
203 | } | |
204 | } | |
205 | }; | |
206 | ||
207 | ||
208 | /** | |
209 | * Behaves the same way as PlotKit.Layout, but also copies the errors | |
210 | * @private | |
211 | */ | |
212 | DygraphLayout.prototype.evaluateWithError = function() { | |
213 | this.evaluate(); | |
214 | if (!(this.attr_('errorBars') || this.attr_('customBars'))) return; | |
215 | ||
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; | |
220 | var j = 0; | |
221 | var dataset = this.datasets[setName]; | |
7f028980 | 222 | var axis = this.dygraph_.axisPropertiesForSeries(setName); |
74a5af31 DV |
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]); | |
227 | ||
228 | if (xv == this.points[i].xval && | |
229 | yv == this.points[i].yval) { | |
7f028980 DV |
230 | var errorMinus = parseFloat(item[2]); |
231 | var errorPlus = parseFloat(item[3]); | |
232 | ||
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); | |
74a5af31 DV |
237 | } |
238 | } | |
239 | } | |
240 | }; | |
241 | ||
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; | |
249 | } | |
250 | ||
251 | this.annotated_points = []; | |
d570a072 AR |
252 | |
253 | // Exit the function early if there are no annotations. | |
254 | if (!this.annotations || !this.annotations.length) { | |
255 | return; | |
256 | } | |
257 | ||
258 | // TODO(antrob): loop through annotations not points. | |
74a5af31 DV |
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); | |
265 | } | |
266 | } | |
267 | }; | |
268 | ||
269 | /** | |
270 | * Convenience function to remove all the data sets from a graph | |
271 | */ | |
272 | DygraphLayout.prototype.removeAllDatasets = function() { | |
273 | delete this.datasets; | |
274 | this.datasets = new Array(); | |
275 | }; | |
276 | ||
277 | /** | |
278 | * Return a copy of the point at the indicated index, with its yval unstacked. | |
279 | * @param int index of point in layout_.points | |
280 | */ | |
281 | DygraphLayout.prototype.unstackPointAtIndex = function(idx) { | |
282 | var point = this.points[idx]; | |
283 | ||
284 | // Clone the point since we modify it | |
285 | var unstackedPoint = {}; | |
286 | for (var i in point) { | |
287 | unstackedPoint[i] = point[i]; | |
288 | } | |
289 | ||
290 | if (!this.attr_("stackedGraph")) { | |
291 | return unstackedPoint; | |
292 | } | |
293 | ||
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; | |
299 | break; | |
300 | } | |
301 | } | |
302 | ||
303 | return unstackedPoint; | |
304 | } |