Debian Codegraph @

dojox/charting/Chart.js

raw symbols SCIP
1 define(["../main", "dojo/_base/lang", "dojo/_base/array","dojo/_base/declare", "dojo/dom-style",
2 "dojo/dom", "dojo/dom-geometry", "dojo/dom-construct","dojo/_base/Color", "dojo/sniff",
3 "./Element", "./SimpleTheme", "./Series", "./axis2d/common", "./plot2d/common", "dojox/gfx/shape",
4 "dojox/gfx", "dojo/has!dojo-bidi?./bidi/Chart", "dojox/lang/functional", "dojox/lang/functional/fold", "dojox/lang/functional/reversed"],
5 function(dojox, lang, arr, declare, domStyle,
6 dom, domGeom, domConstruct, Color, has,
7 Element, SimpleTheme, Series, common, plot2dCommon, shape,
8 g, BidiChart, func){
9 /*=====
10 var __ChartCtorArgs = {
11 // summary:
12 // The keyword arguments that can be passed in a Chart constructor.
13 // margins: Object?
14 // Optional margins for the chart, in the form of { l, t, r, b}.
15 // stroke: dojox.gfx.Stroke?
16 // An optional outline/stroke for the chart.
17 // fill: dojox.gfx.Fill?
18 // An optional fill for the chart.
19 // delayInMs: Number
20 // Delay in ms for delayedRender(). Default: 200.
21 };
22 =====*/
23
24 /*=====
25 var __SeriesCtorArgs = {
26 // summary:
27 // An optional arguments object that can be used in the Series constructor.
28 // plot: String?
29 // The plot (by name) that this series belongs to.
30 };
31 =====*/
32
33 /*=====
34 var __BaseAxisCtorArgs = {
35 // summary:
36 // Optional arguments used in the definition of an invisible axis.
37 // vertical: Boolean?
38 // A flag that says whether an axis is vertical (i.e. y axis) or horizontal. Default is false (horizontal).
39 // min: Number?
40 // The smallest value on an axis. Default is 0.
41 // max: Number?
42 // The largest value on an axis. Default is 1.
43 };
44 =====*/
45
46 var dc = lang.getObject("charting", true, dojox),
47 defaultMargins = {l: 10, t: 10, r: 10, b: 10};
48
49 function clear (item) {
50 return item.clear();
51 }
52
53 function destroy (item) {
54 return item.destroy();
55 }
56
57 function makeClean (item) {
58 item.dirty = false;
59 return false;
60 }
61
62 function makeDirty (item) {
63 item.dirty = true;
64 return true;
65 }
66
67 function getName (item) {
68 return item.name;
69 }
70
71 var Chart = declare(has("dojo-bidi")? "dojox.charting.NonBidiChart" : "dojox.charting.Chart", null, {
72 // summary:
73 // The main chart object in dojox.charting. This will create a two dimensional
74 // chart based on dojox.gfx.
75 //
76 // description:
77 // dojox.charting.Chart is the primary object used for any kind of charts. It
78 // is simple to create--just pass it a node reference, which is used as the
79 // container for the chart--and a set of optional keyword arguments and go.
80 //
81 // Note that like most of dojox.gfx, most of dojox.charting.Chart's methods are
82 // designed to return a reference to the chart itself, to allow for functional
83 // chaining. This makes defining everything on a Chart very easy to do.
84 //
85 // example:
86 // Create an area chart, with smoothing.
87 // | require(["dojox/charting/Chart", "dojox/charting/themes/Shrooms", "dojox/charting/plot2d/Areas", ...],
88 // | function(Chart, Shrooms, Areas, ...){
89 // | new Chart(node)
90 // | .addPlot("default", { type: Areas, tension: "X" })
91 // | .setTheme(Shrooms)
92 // | .addSeries("Series A", [1, 2, 0.5, 1.5, 1, 2.8, 0.4])
93 // | .addSeries("Series B", [2.6, 1.8, 2, 1, 1.4, 0.7, 2])
94 // | .addSeries("Series C", [6.3, 1.8, 3, 0.5, 4.4, 2.7, 2])
95 // | .render();
96 // | });
97 //
98 // example:
99 // The form of data in a data series can take a number of forms: a simple array,
100 // an array of objects {x,y}, or something custom (as determined by the plot).
101 // Here's an example of a Candlestick chart, which expects an object of
102 // { open, high, low, close }.
103 // | require(["dojox/charting/Chart", "dojox/charting/plot2d/Candlesticks", ...],
104 // | function(Chart, Candlesticks, ...){
105 // | new Chart(node)
106 // | .addPlot("default", {type: Candlesticks, gap: 1})
107 // | .addAxis("x", {fixLower: "major", fixUpper: "major", includeZero: true})
108 // | .addAxis("y", {vertical: true, fixLower: "major", fixUpper: "major", natural: true})
109 // | .addSeries("Series A", [
110 // | { open: 20, close: 16, high: 22, low: 8 },
111 // | { open: 16, close: 22, high: 26, low: 6, mid: 18 },
112 // | { open: 22, close: 18, high: 22, low: 11, mid: 21 },
113 // | { open: 18, close: 29, high: 32, low: 14, mid: 27 },
114 // | { open: 29, close: 24, high: 29, low: 13, mid: 27 },
115 // | { open: 24, close: 8, high: 24, low: 5 },
116 // | { open: 8, close: 16, high: 22, low: 2 },
117 // | { open: 16, close: 12, high: 19, low: 7 },
118 // | { open: 12, close: 20, high: 22, low: 8 },
119 // | { open: 20, close: 16, high: 22, low: 8 },
120 // | { open: 16, close: 22, high: 26, low: 6, mid: 18 },
121 // | { open: 22, close: 18, high: 22, low: 11, mid: 21 },
122 // | { open: 18, close: 29, high: 32, low: 14, mid: 27 },
123 // | { open: 29, close: 24, high: 29, low: 13, mid: 27 },
124 // | { open: 24, close: 8, high: 24, low: 5 },
125 // | { open: 8, close: 16, high: 22, low: 2 },
126 // | { open: 16, close: 12, high: 19, low: 7 },
127 // | { open: 12, close: 20, high: 22, low: 8 },
128 // | { open: 20, close: 16, high: 22, low: 8 },
129 // | { open: 16, close: 22, high: 26, low: 6 },
130 // | { open: 22, close: 18, high: 22, low: 11 },
131 // | { open: 18, close: 29, high: 32, low: 14 },
132 // | { open: 29, close: 24, high: 29, low: 13 },
133 // | { open: 24, close: 8, high: 24, low: 5 },
134 // | { open: 8, close: 16, high: 22, low: 2 },
135 // | { open: 16, close: 12, high: 19, low: 7 },
136 // | { open: 12, close: 20, high: 22, low: 8 },
137 // | { open: 20, close: 16, high: 22, low: 8 }
138 // | ],
139 // | { stroke: { color: "green" }, fill: "lightgreen" }
140 // | )
141 // | .render();
142 // | });
143
144 // theme: dojox/charting/SimpleTheme?
145 // An optional theme to use for styling the chart.
146 // axes: dojox/charting/axis2d/Base{}?
147 // A map of axes for use in plotting a chart.
148 // stack: dojox/charting/plot2d/Base[]
149 // A stack of plotters.
150 // plots: dojox/charting/plot2d/Base{}
151 // A map of plotter indices
152 // series: dojox/charting/Series[]
153 // The stack of data runs used to create plots.
154 // runs: dojox/charting/Series{}
155 // A map of series indices
156 // margins: Object?
157 // The margins around the chart. Default is { l:10, t:10, r:10, b:10 }.
158 // stroke: dojox.gfx.Stroke?
159 // The outline of the chart (stroke in vector graphics terms).
160 // fill: dojox.gfx.Fill?
161 // The color for the chart.
162 // node: DOMNode
163 // The container node passed to the constructor.
164 // surface: dojox/gfx/shape.Surface
165 // The main graphics surface upon which a chart is drawn.
166 // dirty: Boolean
167 // A boolean flag indicating whether or not the chart needs to be updated/re-rendered.
168 // htmlLabels: Boolean
169 // A boolean flag indicating whether or not it should try to use HTML-based labels for the title or not.
170 // The default is true. The only caveat is IE and Opera browsers will always use GFX-based labels.
171
172 constructor: function(/* DOMNode */node, /* __ChartCtorArgs? */kwArgs){
173 // summary:
174 // The constructor for a new Chart. Initializes all parameters used for a chart.
175 // returns: dojox/charting/Chart
176 // The newly created chart.
177
178 // initialize parameters
179 if(!kwArgs){ kwArgs = {}; }
180 this.margins = kwArgs.margins || defaultMargins;
181 this._customMargins = !!kwArgs.margins;
182 this.stroke = kwArgs.stroke;
183 this.fill = kwArgs.fill;
184 this.delayInMs = kwArgs.delayInMs || 200;
185 this.title = kwArgs.title;
186 this.titleGap = kwArgs.titleGap;
187 this.titlePos = kwArgs.titlePos;
188 this.titleFont = kwArgs.titleFont;
189 this.titleFontColor = kwArgs.titleFontColor;
190 this.titleAlign = kwArgs.titleAlign; // This can be middle, left, right, or edge
191 // edge is left or right aligned with chart plot edge depending on bidi.
192 this.chartTitle = null;
193 this.htmlLabels = true;
194 if("htmlLabels" in kwArgs){
195 this.htmlLabels = kwArgs.htmlLabels;
196 }
197
198 // default initialization
199 this.theme = null;
200 this.axes = {}; // map of axes
201 this.stack = []; // stack of plotters
202 this.plots = {}; // map of plotter indices
203 this.series = []; // stack of data runs
204 this.runs = {}; // map of data run indices
205 this.dirty = true;
206
207 // create a surface
208 this.node = dom.byId(node);
209 var box = domGeom.getMarginBox(node);
210 this.surface = g.createSurface(this.node, box.w || 400, box.h || 300);
211 if(this.surface.declaredClass.indexOf("vml") == -1){
212 // except if vml use native clipping
213 this._nativeClip = true;
214 }
215 },
216 destroy: function(){
217 // summary:
218 // Cleanup when a chart is to be destroyed.
219 // returns: void
220 arr.forEach(this.series, destroy);
221 arr.forEach(this.stack, destroy);
222 func.forIn(this.axes, destroy);
223 this.surface.destroy();
224 if(this.chartTitle && this.chartTitle.tagName){
225 // destroy title if it is a DOM node
226 domConstruct.destroy(this.chartTitle);
227 }
228 },
229 getCoords: function(){
230 // summary:
231 // Get the coordinates and dimensions of the containing DOMNode, as
232 // returned by dojo.coords.
233 // returns: Object
234 // The resulting coordinates of the chart. See dojo.coords for details.
235 var node = this.node;
236 var s = domStyle.getComputedStyle(node), coords = domGeom.getMarginBox(node, s);
237 var abs = domGeom.position(node, true);
238 coords.x = abs.x;
239 coords.y = abs.y;
240 return coords; // Object
241 },
242 setTheme: function(theme){
243 // summary:
244 // Set a theme of the chart.
245 // theme: dojox/charting/SimpleTheme
246 // The theme to be used for visual rendering.
247 // returns: dojox/charting/Chart
248 // A reference to the current chart for functional chaining.
249 this.theme = theme.clone();
250 if(!this._customMargins){
251 this.margins = this.theme.chart.margins || defaultMargins;
252 }
253 this.dirty = true;
254 return this; // dojox/charting/Chart
255 },
256 addAxis: function(name, kwArgs){
257 // summary:
258 // Add an axis to the chart, for rendering.
259 // name: String
260 // The name of the axis.
261 // kwArgs: __BaseAxisCtorArgs?
262 // An optional keyword arguments object for use in defining details of an axis.
263 // returns: dojox/charting/Chart
264 // A reference to the current chart for functional chaining.
265 var axis, axisType = kwArgs && kwArgs.type || "Default";
266 if(typeof axisType == "string"){
267 if(!dc.axis2d || !dc.axis2d[axisType]){
268 throw Error("Can't find axis: " + axisType + " - Check " + "require() dependencies.");
269 }
270 axis = new dc.axis2d[axisType](this, kwArgs);
271 }else{
272 axis = new axisType(this, kwArgs);
273 }
274 axis.name = name;
275 axis.dirty = true;
276 if(name in this.axes){
277 this.axes[name].destroy();
278 }
279 this.axes[name] = axis;
280 this.dirty = true;
281 return this; // dojox/charting/Chart
282 },
283 getAxis: function(name){
284 // summary:
285 // Get the given axis, by name.
286 // name: String
287 // The name the axis was defined by.
288 // returns: dojox/charting/axis2d/Default
289 // The axis as stored in the chart's axis map.
290 return this.axes[name]; // dojox/charting/axis2d/Default
291 },
292 removeAxis: function(name){
293 // summary:
294 // Remove the axis that was defined using name.
295 // name: String
296 // The axis name, as defined in addAxis.
297 // returns: dojox/charting/Chart
298 // A reference to the current chart for functional chaining.
299 if(name in this.axes){
300 // destroy the axis
301 this.axes[name].destroy();
302 delete this.axes[name];
303 // mark the chart as dirty
304 this.dirty = true;
305 }
306 return this; // dojox/charting/Chart
307 },
308 addPlot: function(name, kwArgs){
309 // summary:
310 // Add a new plot to the chart, defined by name and using the optional keyword arguments object.
311 // Note that dojox.charting assumes the main plot to be called "default"; if you do not have
312 // a plot called "default" and attempt to add data series to the chart without specifying the
313 // plot to be rendered on, you WILL get errors.
314 // name: String
315 // The name of the plot to be added to the chart. If you only plan on using one plot, call it "default".
316 // kwArgs: dojox.charting.plot2d.__PlotCtorArgs
317 // An object with optional parameters for the plot in question.
318 // returns: dojox/charting/Chart
319 // A reference to the current chart for functional chaining.
320 var plot, plotType = kwArgs && kwArgs.type || "Default";
321 if(typeof plotType == "string"){
322 if(!dc.plot2d || !dc.plot2d[plotType]){
323 throw Error("Can't find plot: " + plotType + " - didn't you forget to dojo" + ".require() it?");
324 }
325 plot = new dc.plot2d[plotType](this, kwArgs);
326 }else{
327 plot = new plotType(this, kwArgs);
328 }
329 plot.name = name;
330 plot.dirty = true;
331 if(name in this.plots){
332 this.stack[this.plots[name]].destroy();
333 this.stack[this.plots[name]] = plot;
334 }else{
335 this.plots[name] = this.stack.length;
336 this.stack.push(plot);
337 }
338 this.dirty = true;
339 return this; // dojox/charting/Chart
340 },
341 getPlot: function(name){
342 // summary:
343 // Get the given plot, by name.
344 // name: String
345 // The name the plot was defined by.
346 // returns: dojox/charting/plot2d/Base
347 // The plot.
348 return this.stack[this.plots[name]];
349 },
350 removePlot: function(name){
351 // summary:
352 // Remove the plot defined using name from the chart's plot stack.
353 // name: String
354 // The name of the plot as defined using addPlot.
355 // returns: dojox/charting/Chart
356 // A reference to the current chart for functional chaining.
357 if(name in this.plots){
358 // get the index and remove the name
359 var index = this.plots[name];
360 delete this.plots[name];
361 // destroy the plot
362 this.stack[index].destroy();
363 // remove the plot from the stack
364 this.stack.splice(index, 1);
365 // update indices to reflect the shift
366 func.forIn(this.plots, function(idx, name, plots){
367 if(idx > index){
368 plots[name] = idx - 1;
369 }
370 });
371 // remove all related series
372 var ns = arr.filter(this.series, function(run){ return run.plot != name; });
373 if(ns.length < this.series.length){
374 // kill all removed series
375 arr.forEach(this.series, function(run){
376 if(run.plot == name){
377 run.destroy();
378 }
379 });
380 // rebuild all necessary data structures
381 this.runs = {};
382 arr.forEach(ns, function(run, index){
383 this.runs[run.plot] = index;
384 }, this);
385 this.series = ns;
386 }
387 // mark the chart as dirty
388 this.dirty = true;
389 }
390 return this; // dojox/charting/Chart
391 },
392 getPlotOrder: function(){
393 // summary:
394 // Returns an array of plot names in the current order
395 // (the top-most plot is the first).
396 // returns: Array
397 return func.map(this.stack, getName); // Array
398 },
399 setPlotOrder: function(newOrder){
400 // summary:
401 // Sets new order of plots. newOrder cannot add or remove
402 // plots. Wrong names, or dups are ignored.
403 // newOrder: Array
404 // Array of plot names compatible with getPlotOrder().
405 // returns: dojox/charting/Chart
406 // A reference to the current chart for functional chaining.
407 var names = {},
408 order = func.filter(newOrder, function(name){
409 if(!(name in this.plots) || (name in names)){
410 return false;
411 }
412 names[name] = 1;
413 return true;
414 }, this);
415 if(order.length < this.stack.length){
416 func.forEach(this.stack, function(plot){
417 var name = plot.name;
418 if(!(name in names)){
419 order.push(name);
420 }
421 });
422 }
423 var newStack = func.map(order, function(name){
424 return this.stack[this.plots[name]];
425 }, this);
426 func.forEach(newStack, function(plot, i){
427 this.plots[plot.name] = i;
428 }, this);
429 this.stack = newStack;
430 this.dirty = true;
431 return this; // dojox/charting/Chart
432 },
433 movePlotToFront: function(name){
434 // summary:
435 // Moves a given plot to front.
436 // name: String
437 // Plot's name to move.
438 // returns: dojox/charting/Chart
439 // A reference to the current chart for functional chaining.
440 if(name in this.plots){
441 var index = this.plots[name];
442 if(index){
443 var newOrder = this.getPlotOrder();
444 newOrder.splice(index, 1);
445 newOrder.unshift(name);
446 return this.setPlotOrder(newOrder); // dojox/charting/Chart
447 }
448 }
449 return this; // dojox/charting/Chart
450 },
451 movePlotToBack: function(name){
452 // summary:
453 // Moves a given plot to back.
454 // name: String
455 // Plot's name to move.
456 // returns: dojox/charting/Chart
457 // A reference to the current chart for functional chaining.
458 if(name in this.plots){
459 var index = this.plots[name];
460 if(index < this.stack.length - 1){
461 var newOrder = this.getPlotOrder();
462 newOrder.splice(index, 1);
463 newOrder.push(name);
464 return this.setPlotOrder(newOrder); // dojox/charting/Chart
465 }
466 }
467 return this; // dojox/charting/Chart
468 },
469 addSeries: function(name, data, kwArgs){
470 // summary:
471 // Add a data series to the chart for rendering.
472 // name: String
473 // The name of the data series to be plotted.
474 // data: Array|Object
475 // The array of data points (either numbers or objects) that
476 // represents the data to be drawn. Or it can be an object. In
477 // the latter case, it should have a property "data" (an array),
478 // destroy(), and setSeriesObject().
479 // kwArgs: __SeriesCtorArgs?
480 // An optional keyword arguments object that will be mixed into
481 // the resultant series object.
482 // returns: dojox/charting/Chart
483 // A reference to the current chart for functional chaining.
484 var run = new Series(this, data, kwArgs);
485 run.name = name;
486 if(name in this.runs){
487 this.series[this.runs[name]].destroy();
488 this.series[this.runs[name]] = run;
489 }else{
490 this.runs[name] = this.series.length;
491 this.series.push(run);
492 }
493 this.dirty = true;
494 // fix min/max
495 if(!("ymin" in run) && "min" in run){ run.ymin = run.min; }
496 if(!("ymax" in run) && "max" in run){ run.ymax = run.max; }
497 return this; // dojox/charting/Chart
498 },
499 getSeries: function(name){
500 // summary:
501 // Get the given series, by name.
502 // name: String
503 // The name the series was defined by.
504 // returns: dojox/charting/Series
505 // The series.
506 return this.series[this.runs[name]];
507 },
508 removeSeries: function(name){
509 // summary:
510 // Remove the series defined by name from the chart.
511 // name: String
512 // The name of the series as defined by addSeries.
513 // returns: dojox/charting/Chart
514 // A reference to the current chart for functional chaining.
515 if(name in this.runs){
516 // get the index and remove the name
517 var index = this.runs[name];
518 delete this.runs[name];
519 // destroy the run
520 this.series[index].destroy();
521 // remove the run from the stack of series
522 this.series.splice(index, 1);
523 // update indices to reflect the shift
524 func.forIn(this.runs, function(idx, name, runs){
525 if(idx > index){
526 runs[name] = idx - 1;
527 }
528 });
529 this.dirty = true;
530 }
531 return this; // dojox/charting/Chart
532 },
533 updateSeries: function(name, data, offsets){
534 // summary:
535 // Update the given series with a new set of data points.
536 // name: String
537 // The name of the series as defined in addSeries.
538 // data: Array|Object
539 // The array of data points (either numbers or objects) that
540 // represents the data to be drawn. Or it can be an object. In
541 // the latter case, it should have a property "data" (an array),
542 // destroy(), and setSeriesObject().
543 // offsets: Boolean?
544 // If true recomputes the offsets of the chart based on the new
545 // data. This is useful if the range of data is drastically changing
546 // and offsets need to be recomputed.
547 // returns: dojox/charting/Chart
548 // A reference to the current chart for functional chaining.
549 if(name in this.runs){
550 var run = this.series[this.runs[name]];
551 run.update(data);
552 if(offsets){
553 this.dirty = true;
554 }else{
555 this._invalidateDependentPlots(run.plot, false);
556 this._invalidateDependentPlots(run.plot, true);
557 }
558 }
559 return this; // dojox/charting/Chart
560 },
561 getSeriesOrder: function(plotName){
562 // summary:
563 // Returns an array of series names in the current order
564 // (the top-most series is the first) within a plot.
565 // plotName: String
566 // Plot's name.
567 // returns: Array
568 return func.map(func.filter(this.series, function(run){
569 return run.plot == plotName;
570 }), getName);
571 },
572 setSeriesOrder: function(newOrder){
573 // summary:
574 // Sets new order of series within a plot. newOrder cannot add
575 // or remove series. Wrong names, or dups are ignored.
576 // newOrder: Array
577 // Array of series names compatible with getPlotOrder(). All
578 // series should belong to the same plot.
579 // returns: dojox/charting/Chart
580 // A reference to the current chart for functional chaining.
581 var plotName, names = {},
582 order = func.filter(newOrder, function(name){
583 if(!(name in this.runs) || (name in names)){
584 return false;
585 }
586 var run = this.series[this.runs[name]];
587 if(plotName){
588 if(run.plot != plotName){
589 return false;
590 }
591 }else{
592 plotName = run.plot;
593 }
594 names[name] = 1;
595 return true;
596 }, this);
597 func.forEach(this.series, function(run){
598 var name = run.name;
599 if(!(name in names) && run.plot == plotName){
600 order.push(name);
601 }
602 });
603 var newSeries = func.map(order, function(name){
604 return this.series[this.runs[name]];
605 }, this);
606 this.series = newSeries.concat(func.filter(this.series, function(run){
607 return run.plot != plotName;
608 }));
609 func.forEach(this.series, function(run, i){
610 this.runs[run.name] = i;
611 }, this);
612 this.dirty = true;
613 return this; // dojox/charting/Chart
614 },
615 moveSeriesToFront: function(name){
616 // summary:
617 // Moves a given series to front of a plot.
618 // name: String
619 // Series' name to move.
620 // returns: dojox/charting/Chart
621 // A reference to the current chart for functional chaining.
622 if(name in this.runs){
623 var index = this.runs[name],
624 newOrder = this.getSeriesOrder(this.series[index].plot);
625 if(name != newOrder[0]){
626 newOrder.splice(index, 1);
627 newOrder.unshift(name);
628 return this.setSeriesOrder(newOrder); // dojox/charting/Chart
629 }
630 }
631 return this; // dojox/charting/Chart
632 },
633 moveSeriesToBack: function(name){
634 // summary:
635 // Moves a given series to back of a plot.
636 // name: String
637 // Series' name to move.
638 // returns: dojox/charting/Chart
639 // A reference to the current chart for functional chaining.
640 if(name in this.runs){
641 var index = this.runs[name],
642 newOrder = this.getSeriesOrder(this.series[index].plot);
643 if(name != newOrder[newOrder.length - 1]){
644 newOrder.splice(index, 1);
645 newOrder.push(name);
646 return this.setSeriesOrder(newOrder); // dojox/charting/Chart
647 }
648 }
649 return this; // dojox/charting/Chart
650 },
651 resize: function(width, height){
652 // summary:
653 // Resize the chart to the dimensions of width and height.
654 // description:
655 // Resize the chart and its surface to the width and height dimensions.
656 // If a single argument of the form {w: value1, h: value2} is provided take that argument as the dimensions to use.
657 // Finally if no argument is provided, resize the surface to the marginBox of the chart.
658 // width: Number|Object?
659 // The new width of the chart or the box definition.
660 // height: Number?
661 // The new height of the chart.
662 // returns: dojox/charting/Chart
663 // A reference to the current chart for functional chaining.
664 switch(arguments.length){
665 // case 0, do not resize the div, just the surface
666 case 1:
667 // argument, override node box
668 domGeom.setMarginBox(this.node, width);
669 break;
670 case 2:
671 // argument, override node box
672 domGeom.setMarginBox(this.node, {w: width, h: height});
673 break;
674 }
675 // in all cases take back the computed box
676 var box = domGeom.getMarginBox(this.node);
677 var d = this.surface.getDimensions();
678 if(d.width != box.w || d.height != box.h){
679 // and set it on the surface
680 this.surface.setDimensions(box.w, box.h);
681 this.dirty = true;
682 return this.render(); // dojox/charting/Chart
683 }else{
684 return this;
685 }
686 },
687 getGeometry: function(){
688 // summary:
689 // Returns a map of information about all axes in a chart and what they represent
690 // in terms of scaling (see dojox.charting.axis2d.Default.getScaler).
691 // returns: Object
692 // An map of geometry objects, a one-to-one mapping of axes.
693 var ret = {};
694 func.forIn(this.axes, function(axis){
695 if(axis.initialized()){
696 ret[axis.name] = {
697 name: axis.name,
698 vertical: axis.vertical,
699 scaler: axis.scaler,
700 ticks: axis.ticks
701 };
702 }
703 });
704 return ret; // Object
705 },
706 setAxisWindow: function(name, scale, offset, zoom){
707 // summary:
708 // Zooms an axis and all dependent plots. Can be used to zoom in 1D.
709 // name: String
710 // The name of the axis as defined by addAxis.
711 // scale: Number
712 // The scale on the target axis.
713 // offset: Number
714 // Any offest, as measured by axis tick
715 // zoom: Boolean|Object?
716 // The chart zooming animation trigger. This is null by default,
717 // e.g. {duration: 1200}, or just set true.
718 // returns: dojox/charting/Chart
719 // A reference to the current chart for functional chaining.
720 var axis = this.axes[name];
721 if(axis){
722 axis.setWindow(scale, offset);
723 arr.forEach(this.stack,function(plot){
724 if(plot.hAxis == name || plot.vAxis == name){
725 plot.zoom = zoom;
726 }
727 });
728 }
729 return this; // dojox/charting/Chart
730 },
731 setWindow: function(sx, sy, dx, dy, zoom){
732 // summary:
733 // Zooms in or out any plots in two dimensions.
734 // sx: Number
735 // The scale for the x axis.
736 // sy: Number
737 // The scale for the y axis.
738 // dx: Number
739 // The pixel offset on the x axis.
740 // dy: Number
741 // The pixel offset on the y axis.
742 // zoom: Boolean|Object?
743 // The chart zooming animation trigger. This is null by default,
744 // e.g. {duration: 1200}, or just set true.
745 // returns: dojox/charting/Chart
746 // A reference to the current chart for functional chaining.
747 if(!("plotArea" in this)){
748 this.calculateGeometry();
749 }
750 func.forIn(this.axes, function(axis){
751 var scale, offset, bounds = axis.getScaler().bounds,
752 s = bounds.span / (bounds.upper - bounds.lower);
753 if(axis.vertical){
754 scale = sy;
755 offset = dy / s / scale;
756 }else{
757 scale = sx;
758 offset = dx / s / scale;
759 }
760 axis.setWindow(scale, offset);
761 });
762 arr.forEach(this.stack, function(plot){ plot.zoom = zoom; });
763 return this; // dojox/charting/Chart
764 },
765 zoomIn: function(name, range, delayed){
766 // summary:
767 // Zoom the chart to a specific range on one axis. This calls render()
768 // directly as a convenience method.
769 // name: String
770 // The name of the axis as defined by addAxis.
771 // range: Array
772 // The end points of the zoom range, measured in axis ticks.
773 var axis = this.axes[name];
774 if(axis){
775 var scale, offset, bounds = axis.getScaler().bounds;
776 var lower = Math.min(range[0],range[1]);
777 var upper = Math.max(range[0],range[1]);
778 lower = range[0] < bounds.lower ? bounds.lower : lower;
779 upper = range[1] > bounds.upper ? bounds.upper : upper;
780 scale = (bounds.upper - bounds.lower) / (upper - lower);
781 offset = lower - bounds.lower;
782 this.setAxisWindow(name, scale, offset);
783 if(delayed){
784 this.delayedRender();
785 }else{
786 this.render();
787 }
788 }
789 },
790 calculateGeometry: function(){
791 // summary:
792 // Calculate the geometry of the chart based on the defined axes of
793 // a chart.
794 // returns: dojox/charting/Chart
795 // A reference to the current chart for functional chaining.
796 if(this.dirty){
797 return this.fullGeometry();
798 }
799
800 // calculate geometry
801 var dirty = arr.filter(this.stack, function(plot){
802 return plot.dirty ||
803 (plot.hAxis && this.axes[plot.hAxis].dirty) ||
804 (plot.vAxis && this.axes[plot.vAxis].dirty);
805 }, this);
806 calculateAxes(dirty, this.plotArea);
807
808 return this; // dojox/charting/Chart
809 },
810 fullGeometry: function(){
811 // summary:
812 // Calculate the full geometry of the chart. This includes passing
813 // over all major elements of a chart (plots, axes, series, container)
814 // in order to ensure proper rendering.
815 // returns: dojox/charting/Chart
816 // A reference to the current chart for functional chaining.
817 this._makeDirty();
818
819 // clear old values
820 arr.forEach(this.stack, clear);
821
822 // rebuild new connections, and add defaults
823
824 // set up a theme
825 if(!this.theme){
826 this.setTheme(new SimpleTheme());
827 }
828
829 // assign series
830 arr.forEach(this.series, function(run){
831 if(!(run.plot in this.plots)){
832 // TODO remove auto-assignment
833 if(!dc.plot2d || !dc.plot2d.Default){
834 throw Error("Can't find plot: Default - didn't you forget to dojo" + ".require() it?");
835 }
836 var plot = new dc.plot2d.Default(this, {});
837 plot.name = run.plot;
838 this.plots[run.plot] = this.stack.length;
839 this.stack.push(plot);
840 }
841 this.stack[this.plots[run.plot]].addSeries(run);
842 }, this);
843 // assign axes
844 arr.forEach(this.stack, function(plot){
845 if(plot.assignAxes){
846 plot.assignAxes(this.axes);
847 }
848 }, this);
849
850 // calculate geometry
851
852 // 1st pass
853 var dim = this.dim = this.surface.getDimensions();
854 dim.width = g.normalizedLength(dim.width);
855 dim.height = g.normalizedLength(dim.height);
856 func.forIn(this.axes, clear);
857 calculateAxes(this.stack, dim);
858
859 // assumption: we don't have stacked axes yet
860 var offsets = this.offsets = {l: 0, r: 0, t: 0, b: 0};
861 // chart mirroring starts
862 var self = this;
863 func.forIn(this.axes, function(axis){
864 if(has("dojo-bidi")){
865 self._resetLeftBottom(axis);
866 }
867 func.forIn(axis.getOffsets(), function(o, i){ offsets[i] = Math.max(o, offsets[i]); });
868 });
869 // chart mirroring ends
870 // add title area
871 if(this.title){
872 this.titleGap = (this.titleGap==0) ? 0 : this.titleGap || this.theme.chart.titleGap || 20;
873 this.titlePos = this.titlePos || this.theme.chart.titlePos || "top";
874 this.titleFont = this.titleFont || this.theme.chart.titleFont;
875 this.titleFontColor = this.titleFontColor || this.theme.chart.titleFontColor || "black";
876 this.titleAlign = this.titleAlign || this.theme && this.theme.chart && this.theme.chart.titleAlign || "middle";
877 var tsize = g.normalizedLength(g.splitFontString(this.titleFont).size);
878 offsets[this.titlePos == "top" ? "t" : "b"] += (tsize + this.titleGap);
879 }
880 // add margins
881 func.forIn(this.margins, function(o, i){ offsets[i] += o; });
882
883 // 2nd pass with realistic dimensions
884 this.plotArea = {
885 width: dim.width - offsets.l - offsets.r,
886 height: dim.height - offsets.t - offsets.b
887 };
888 func.forIn(this.axes, clear);
889 calculateAxes(this.stack, this.plotArea);
890
891 return this; // dojox/charting/Chart
892 },
893 render: function(){
894 // summary:
895 // Render the chart according to the current information defined. This should
896 // be the last call made when defining/creating a chart, or if data within the
897 // chart has been changed.
898 // returns: dojox/charting/Chart
899 // A reference to the current chart for functional chaining.
900
901 // do we have a delayed renderer pending? If yes we need to clear it
902 if(this._delayedRenderHandle){
903 clearTimeout(this._delayedRenderHandle);
904 this._delayedRenderHandle = null;
905 }
906
907 if(this.theme){
908 this.theme.clear();
909 }
910
911 if(this.dirty){
912 return this.fullRender();
913 }
914
915 this.calculateGeometry();
916
917 // go over the stack backwards
918 func.forEachRev(this.stack, function(plot){ plot.render(this.dim, this.offsets); }, this);
919
920 // go over axes
921 func.forIn(this.axes, function(axis){ axis.render(this.dim, this.offsets); }, this);
922
923 this._makeClean();
924
925 return this; // dojox/charting/Chart
926 },
927 fullRender: function(){
928 // summary:
929 // Force a full rendering of the chart, including full resets on the chart itself.
930 // You should not call this method directly unless absolutely necessary.
931 // returns: dojox/charting/Chart
932 // A reference to the current chart for functional chaining.
933
934 // calculate geometry
935 this.fullGeometry();
936 var offsets = this.offsets, dim = this.dim;
937 var w = Math.max(0, dim.width - offsets.l - offsets.r),
938 h = Math.max(0, dim.height - offsets.t - offsets.b);
939
940 // get required colors
941 //var requiredColors = func.foldl(this.stack, "z + plot.getRequiredColors()", 0);
942 //this.theme.defineColors({num: requiredColors, cache: false});
943
944 // clear old shapes
945 arr.forEach(this.series, plot2dCommon.purgeGroup);
946 func.forIn(this.axes, plot2dCommon.purgeGroup);
947 arr.forEach(this.stack, plot2dCommon.purgeGroup);
948 var children = this.surface.children;
949 // starting with 1.9 the registry is optional and thus dispose is
950 if(shape.dispose){
951 for(var i = 0; i < children.length;++i){
952 shape.dispose(children[i]);
953 }
954 }
955 if(this.chartTitle && this.chartTitle.tagName){
956 // destroy title if it is a DOM node
957 domConstruct.destroy(this.chartTitle);
958 }
959 this.surface.clear();
960 this.chartTitle = null;
961
962 this._renderChartBackground(dim, offsets);
963 if(this._nativeClip){
964 this._renderPlotBackground(dim, offsets, w, h);
965 }else{
966 // VML
967 this._renderPlotBackground(dim, offsets, w, h);
968 }
969
970 // go over the stack backwards
971 func.foldr(this.stack, function(z, plot){ return plot.render(dim, offsets), 0; }, 0);
972
973 if(!this._nativeClip){
974 // VML, matting-clipping
975 this._renderChartBackground(dim, offsets);
976 }
977
978 //create title: Whether to make chart title as a widget which extends dojox.charting.Element?
979 if(this.title){
980 this._renderTitle(dim, offsets);
981 }
982
983 // go over axes
984 func.forIn(this.axes, function(axis){ axis.render(dim, offsets); });
985
986 this._makeClean();
987
988 return this; // dojox/charting/Chart
989 },
990 _renderTitle: function(dim, offsets){
991 // summary:
992 // Internal function to render the chart title.
993 // dim:
994 // The dimension object of the chart
995 // tags:
996 // private
997 var forceHtmlLabels = (g.renderer == "canvas") && this.htmlLabels,
998 labelType = forceHtmlLabels || !has("ie") && !has("opera") && this.htmlLabels ? "html" : "gfx",
999 tsize = g.normalizedLength(g.splitFontString(this.titleFont).size),
1000 tBox = g._base._getTextBox(this.title,{ font: this.titleFont });
1001
1002 var titleAlign = this.titleAlign;
1003 var isRtl = has("dojo-bidi") && this.isRightToLeft();
1004 var posX = dim.width/2; // Default is middle.
1005 if(titleAlign === "edge"){
1006 titleAlign = "left";
1007 if(isRtl){
1008 posX = dim.width - (offsets.r + tBox.w);
1009 }else {
1010 posX = offsets.l;
1011 }
1012 }else if(titleAlign != "middle"){
1013 if(isRtl){
1014 // We're in BIDI mode, reverse the alignment.
1015 titleAlign = titleAlign === "left" ? "right" : "left";
1016 }
1017 if(titleAlign === "left"){
1018 posX = this.margins.l;
1019 }else if(titleAlign === "right"){
1020 titleAlign = "left";
1021 posX = dim.width - (this.margins.l + tBox.w);
1022 }
1023 }
1024 this.chartTitle = common.createText[labelType](
1025 this,
1026 this.surface,
1027 posX,
1028 this.titlePos=="top" ? tsize + this.margins.t : dim.height - this.margins.b,
1029 titleAlign,
1030 this.title,
1031 this.titleFont,
1032 this.titleFontColor
1033 );
1034 },
1035 _renderChartBackground: function(dim, offsets){
1036 var t = this.theme, rect;
1037 // chart background
1038 var fill = this.fill !== undefined ? this.fill : (t.chart && t.chart.fill);
1039 var stroke = this.stroke !== undefined ? this.stroke : (t.chart && t.chart.stroke);
1040
1041 // TRT: support for "inherit" as a named value in a theme.
1042 if(fill == "inherit"){
1043 // find the background color of the nearest ancestor node, and use that explicitly.
1044 var node = this.node;
1045 fill = new Color(domStyle.get(node, "backgroundColor"));
1046 while(fill.a==0 && node!=document.documentElement){
1047 fill = new Color(domStyle.get(node, "backgroundColor"));
1048 node = node.parentNode;
1049 }
1050 }
1051
1052 if(fill){
1053 if(this._nativeClip){
1054 fill = Element.prototype._shapeFill(Element.prototype._plotFill(fill, dim),
1055 { x:0, y: 0, width: dim.width + 1, height: dim.height + 1 });
1056 this.surface.createRect({ width: dim.width + 1, height: dim.height + 1 }).setFill(fill);
1057 }else{
1058 // VML
1059 fill = Element.prototype._plotFill(fill, dim, offsets);
1060 if(offsets.l){ // left
1061 rect = {
1062 x: 0,
1063 y: 0,
1064 width: offsets.l,
1065 height: dim.height + 1
1066 };
1067 this.surface.createRect(rect).setFill(Element.prototype._shapeFill(fill, rect));
1068 }
1069 if(offsets.r){ // right
1070 rect = {
1071 x: dim.width - offsets.r,
1072 y: 0,
1073 width: offsets.r + 1,
1074 height: dim.height + 2
1075 };
1076 this.surface.createRect(rect).setFill(Element.prototype._shapeFill(fill, rect));
1077 }
1078 if(offsets.t){ // top
1079 rect = {
1080 x: 0,
1081 y: 0,
1082 width: dim.width + 1,
1083 height: offsets.t
1084 };
1085 this.surface.createRect(rect).setFill(Element.prototype._shapeFill(fill, rect));
1086 }
1087 if(offsets.b){ // bottom
1088 rect = {
1089 x: 0,
1090 y: dim.height - offsets.b,
1091 width: dim.width + 1,
1092 height: offsets.b + 2
1093 };
1094 this.surface.createRect(rect).setFill(Element.prototype._shapeFill(fill, rect));
1095 }
1096 }
1097 }
1098 if(stroke){
1099 this.surface.createRect({
1100 width: dim.width - 1,
1101 height: dim.height - 1
1102 }).setStroke(stroke);
1103 }
1104 },
1105 _renderPlotBackground: function(dim, offsets, w, h){
1106 var t = this.theme;
1107
1108 // draw a plot background
1109 var fill = t.plotarea && t.plotarea.fill;
1110 var stroke = t.plotarea && t.plotarea.stroke;
1111 // size might be neg if offsets are bigger that chart size this happens quite often at
1112 // initialization time if the chart widget is used in a BorderContainer
1113 // this will fail on IE/VML
1114 var rect = {
1115 x: offsets.l - 1, y: offsets.t - 1,
1116 width: w + 2,
1117 height: h + 2
1118 };
1119 if(fill){
1120 fill = Element.prototype._shapeFill(Element.prototype._plotFill(fill, dim, offsets), rect);
1121 this.surface.createRect(rect).setFill(fill);
1122 }
1123 if(stroke){
1124 this.surface.createRect({
1125 x: offsets.l, y: offsets.t,
1126 width: w + 1,
1127 height: h + 1
1128 }).setStroke(stroke);
1129 }
1130 },
1131 delayedRender: function(){
1132 // summary:
1133 // Delayed render, which is used to collect multiple updates
1134 // within a delayInMs time window.
1135 // returns: dojox/charting/Chart
1136 // A reference to the current chart for functional chaining.
1137
1138 if(!this._delayedRenderHandle){
1139 this._delayedRenderHandle = setTimeout(
1140 lang.hitch(this, function(){
1141 this.render();
1142 }),
1143 this.delayInMs
1144 );
1145 }
1146
1147 return this; // dojox/charting/Chart
1148 },
1149 connectToPlot: function(name, object, method){
1150 // summary:
1151 // A convenience method to connect a function to a plot.
1152 // name: String
1153 // The name of the plot as defined by addPlot.
1154 // object: Object
1155 // The object to be connected.
1156 // method: Function
1157 // The function to be executed.
1158 // returns: Array
1159 // A handle to the connection, as defined by dojo.connect (see dojo.connect).
1160 return name in this.plots ? this.stack[this.plots[name]].connect(object, method) : null; // Array
1161 },
1162 fireEvent: function(seriesName, eventName, index){
1163 // summary:
1164 // Fires a synthetic event for a series item.
1165 // seriesName: String
1166 // Series name.
1167 // eventName: String
1168 // Event name to simulate: onmouseover, onmouseout, onclick.
1169 // index: Number
1170 // Valid data value index for the event.
1171 // returns: dojox/charting/Chart
1172 // A reference to the current chart for functional chaining.
1173 if(seriesName in this.runs){
1174 var plotName = this.series[this.runs[seriesName]].plot;
1175 if(plotName in this.plots){
1176 var plot = this.stack[this.plots[plotName]];
1177 if(plot){
1178 plot.fireEvent(seriesName, eventName, index);
1179 }
1180 }
1181 }
1182 return this; // dojox/charting/Chart
1183 },
1184 _makeClean: function(){
1185 // reset dirty flags
1186 arr.forEach(this.axes, makeClean);
1187 arr.forEach(this.stack, makeClean);
1188 arr.forEach(this.series, makeClean);
1189 this.dirty = false;
1190 },
1191 _makeDirty: function(){
1192 // reset dirty flags
1193 arr.forEach(this.axes, makeDirty);
1194 arr.forEach(this.stack, makeDirty);
1195 arr.forEach(this.series, makeDirty);
1196 this.dirty = true;
1197 },
1198 _invalidateDependentPlots: function(plotName, /* Boolean */ verticalAxis){
1199 if(plotName in this.plots){
1200 var plot = this.stack[this.plots[plotName]], axis,
1201 axisName = verticalAxis ? "vAxis" : "hAxis";
1202 if(plot[axisName]){
1203 axis = this.axes[plot[axisName]];
1204 if(axis && axis.dependOnData()){
1205 axis.dirty = true;
1206 // find all plots and mark them dirty
1207 arr.forEach(this.stack, function(p){
1208 if(p[axisName] && p[axisName] == plot[axisName]){
1209 p.dirty = true;
1210 }
1211 });
1212 }
1213 }else{
1214 plot.dirty = true;
1215 }
1216 }
1217 },
1218 setDir : function(dir){
1219 return this;
1220 },
1221 _resetLeftBottom: function(axis){
1222 },
1223 formatTruncatedLabel: function(element, label, labelType){
1224 }
1225 });
1226
1227 function hSection(stats){
1228 return {min: stats.hmin, max: stats.hmax};
1229 }
1230
1231 function vSection(stats){
1232 return {min: stats.vmin, max: stats.vmax};
1233 }
1234
1235 function hReplace(stats, h){
1236 stats.hmin = h.min;
1237 stats.hmax = h.max;
1238 }
1239
1240 function vReplace(stats, v){
1241 stats.vmin = v.min;
1242 stats.vmax = v.max;
1243 }
1244
1245 function combineStats(target, source){
1246 if(target && source){
1247 target.min = Math.min(target.min, source.min);
1248 target.max = Math.max(target.max, source.max);
1249 }
1250 return target || source;
1251 }
1252
1253 function calculateAxes(stack, plotArea){
1254 var plots = {}, axes = {};
1255 arr.forEach(stack, function(plot){
1256 var stats = plots[plot.name] = plot.getSeriesStats();
1257 if(plot.hAxis){
1258 axes[plot.hAxis] = combineStats(axes[plot.hAxis], hSection(stats));
1259 }
1260 if(plot.vAxis){
1261 axes[plot.vAxis] = combineStats(axes[plot.vAxis], vSection(stats));
1262 }
1263 });
1264 arr.forEach(stack, function(plot){
1265 var stats = plots[plot.name];
1266 if(plot.hAxis){
1267 hReplace(stats, axes[plot.hAxis]);
1268 }
1269 if(plot.vAxis){
1270 vReplace(stats, axes[plot.vAxis]);
1271 }
1272 plot.initializeScalers(plotArea, stats);
1273 });
1274 }
1275
1276 return has("dojo-bidi")? declare("dojox.charting.Chart", [Chart, BidiChart]) : Chart;
1277 });