Debian Codegraph @

dojox/charting/plot2d/Columns.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common",
2 "dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
3 function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, du, fx){
4
5 var alwaysFalse = function(){ return false; };
6
7 return declare("dojox.charting.plot2d.Columns", [CartesianBase, _PlotEvents], {
8 // summary:
9 // The plot object representing a column chart (vertical bars).
10 defaultParams: {
11 gap: 0, // gap between columns in pixels
12 animate: null, // animate bars into place
13 enableCache: false
14 },
15 optionalParams: {
16 minBarSize: 1, // minimal column width in pixels
17 maxBarSize: 1, // maximal column width in pixels
18 // theme component
19 stroke: {},
20 outline: {},
21 shadow: {},
22 fill: {},
23 filter: {},
24 styleFunc: null,
25 font: "",
26 fontColor: ""
27 },
28
29 constructor: function(chart, kwArgs){
30 // summary:
31 // The constructor for a columns chart.
32 // chart: dojox/charting/Chart
33 // The chart this plot belongs to.
34 // kwArgs: dojox.charting.plot2d.__BarCtorArgs?
35 // An optional keyword arguments object to help define the plot.
36 this.opt = lang.clone(lang.mixin(this.opt, this.defaultParams));
37 du.updateWithObject(this.opt, kwArgs);
38 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
39 this.animate = this.opt.animate;
40 this.renderingOptions = { "shape-rendering": "crispEdges" };
41 },
42
43 getSeriesStats: function(){
44 // summary:
45 // Calculate the min/max on all attached series in both directions.
46 // returns: Object
47 // {hmin, hmax, vmin, vmax} min/max in both directions.
48 var stats = dc.collectSimpleStats(this.series, lang.hitch(this, "isNullValue"));
49 stats.hmin -= 0.5;
50 stats.hmax += 0.5;
51 return stats; // Object
52 },
53
54 createRect: function(run, creator, params){
55 var rect;
56 if(this.opt.enableCache && run._rectFreePool.length > 0){
57 rect = run._rectFreePool.pop();
58 rect.setShape(params);
59 // was cleared, add it back
60 creator.add(rect);
61 }else{
62 rect = creator.createRect(params);
63 }
64 if(this.opt.enableCache){
65 run._rectUsePool.push(rect);
66 }
67 return rect;
68 },
69
70 render: function(dim, offsets){
71 // summary:
72 // Run the calculations for any axes for this plot.
73 // dim: Object
74 // An object in the form of { width, height }
75 // offsets: Object
76 // An object of the form { l, r, t, b}.
77 // returns: dojox/charting/plot2d/Columns
78 // A reference to this plot for functional chaining.
79 if(this.zoom && !this.isDataDirty()){
80 return this.performZoom(dim, offsets);
81 }
82 this.resetEvents();
83 this.dirty = this.isDirty();
84 var s;
85 if(this.dirty){
86 arr.forEach(this.series, dc.purgeGroup);
87 this._eventSeries = {};
88 this.cleanGroup();
89 s = this.getGroup();
90 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
91 }
92 var t = this.chart.theme,
93 ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
94 vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
95 baseline = Math.max(this._vScaler.bounds.lower,
96 this._vAxis ? this._vAxis.naturalBaseline : 0),
97 baselineHeight = vt(baseline),
98 events = this.events(),
99 bar = this.getBarProperties();
100
101 var z = 0; // the non-hidden series index
102
103 // Collect and calculate all values
104 var extractedValues = this.extractValues(this._hScaler);
105 extractedValues = this.rearrangeValues(extractedValues, vt, baselineHeight);
106
107 for(var i = 0; i < this.series.length; i++){
108 var run = this.series[i];
109 if(!this.dirty && !run.dirty){
110 t.skip();
111 this._reconnectEvents(run.name);
112 continue;
113 }
114 run.cleanGroup();
115 if(this.opt.enableCache){
116 run._rectFreePool = (run._rectFreePool?run._rectFreePool:[]).concat(run._rectUsePool?run._rectUsePool:[]);
117 run._rectUsePool = [];
118 }
119 var theme = t.next("column", [this.opt, run]),
120 eventSeries = new Array(run.data.length);
121
122 if(run.hidden){
123 run.dyn.fill = theme.series.fill;
124 continue;
125 }
126
127 s = run.group;
128 var indexed = arr.some(run.data, function(item){
129 return typeof item == "number" || (item && !item.hasOwnProperty("x"));
130 });
131 // on indexed charts we can easily just interate from the first visible to the last visible
132 // data point to save time
133 var min = indexed?Math.max(0, Math.floor(this._hScaler.bounds.from - 1)):0;
134 var max = indexed?Math.min(run.data.length, Math.ceil(this._hScaler.bounds.to)):run.data.length;
135 for(var j = min; j < max; ++j){
136 var value = run.data[j];
137 if(!this.isNullValue(value)){
138 var val = this.getValue(value, j, i, indexed),
139 vv = vt(val.y),
140 h = extractedValues[i][j],
141 finalTheme,
142 sshape;
143
144 if(this.opt.styleFunc || typeof value != "number"){
145 var tMixin = typeof value != "number" ? [value] : [];
146 if(this.opt.styleFunc){
147 tMixin.push(this.opt.styleFunc(value));
148 }
149 finalTheme = t.addMixin(theme, "column", tMixin, true);
150 }else{
151 finalTheme = t.post(theme, "column");
152 }
153
154 if(bar.width >= 1){
155 var rect = {
156 x: offsets.l + ht(val.x + 0.5) + bar.gap + bar.thickness * z,
157 y: dim.height - offsets.b - baselineHeight - Math.max(h, 0),
158 width: bar.width,
159 height: Math.abs(h)
160 };
161 if(finalTheme.series.shadow){
162 var srect = lang.clone(rect);
163 srect.x += finalTheme.series.shadow.dx;
164 srect.y += finalTheme.series.shadow.dy;
165 sshape = this.createRect(run, s, srect).setFill(finalTheme.series.shadow.color).setStroke(finalTheme.series.shadow);
166 if(this.animate){
167 this._animateColumn(sshape, dim.height - offsets.b + baselineHeight, h);
168 }
169 }
170
171 var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets);
172 specialFill = this._shapeFill(specialFill, rect);
173 var shape = this.createRect(run, s, rect).setFill(specialFill).setStroke(finalTheme.series.stroke);
174 this.overrideShape(shape, {index: j, value: val});
175 if(shape.setFilter && finalTheme.series.filter){
176 shape.setFilter(finalTheme.series.filter);
177 }
178 run.dyn.fill = shape.getFill();
179 run.dyn.stroke = shape.getStroke();
180 if(events){
181 var o = {
182 element: "column",
183 index: j,
184 run: run,
185 shape: shape,
186 shadow: sshape,
187 cx: val.x + 0.5,
188 cy: val.y,
189 x: indexed?j:run.data[j].x,
190 y: indexed?run.data[j]:run.data[j].y
191 };
192 this._connectEvents(o);
193 eventSeries[j] = o;
194 }
195 // if val.py is here, this means we are stacking and we need to subtract previous
196 // value to get the high in which we will lay out the label
197 if(!isNaN(val.py) && val.py > baseline){
198 rect.height = h - vt(val.py);
199 }
200 this.createLabel(s, value, rect, finalTheme);
201 if(this.animate){
202 this._animateColumn(shape, dim.height - offsets.b - baselineHeight, h);
203 }
204 }
205 }
206 }
207 this._eventSeries[run.name] = eventSeries;
208 run.dirty = false;
209 z++;
210 }
211 this.dirty = false;
212 // chart mirroring starts
213 if(has("dojo-bidi")){
214 this._checkOrientation(this.group, dim, offsets);
215 }
216 // chart mirroring ends
217 return this; // dojox/charting/plot2d/Columns
218 },
219 getValue: function(value, j, seriesIndex, indexed){
220 var y,x;
221 if(indexed){
222 if(typeof value == "number"){
223 y = value;
224 }else{
225 y = value.y;
226 }
227 x = j;
228 }else{
229 y = value.y;
230 x = value.x - 1;
231 }
232 return { x: x, y: y };
233 },
234 extractValues: function(scaler){
235 var extracted = [];
236 for(var i = this.series.length - 1; i >= 0; --i){
237 var run = this.series[i];
238 if(!this.dirty && !run.dirty){
239 continue;
240 }
241 // on indexed charts we can easily just interate from the first visible to the last visible
242 // data point to save time
243 var indexed = arr.some(run.data, function(item){
244 return typeof item == "number" || (item && !item.hasOwnProperty("x"));
245 }),
246 min = indexed ? Math.max(0, Math.floor(scaler.bounds.from - 1)) : 0,
247 max = indexed ? Math.min(run.data.length, Math.ceil(scaler.bounds.to)) : run.data.length,
248 extractedSet = extracted[i] = [];
249 extractedSet.min = min;
250 extractedSet.max = max;
251 for(var j = min; j < max; ++j){
252 var value = run.data[j];
253 extractedSet[j] = this.isNullValue(value) ? 0 :
254 (typeof value == "number" ? value : value.y);
255 }
256 }
257 return extracted;
258 },
259 rearrangeValues: function(values, transform, baseline){
260 // transform to pixels
261 for(var i = 0, n = values.length; i < n; ++i){
262 var extractedSet = values[i];
263 if(extractedSet){
264 for(var j = extractedSet.min, k = extractedSet.max; j < k; ++j){
265 var value = extractedSet[j];
266 extractedSet[j] = this.isNullValue(value) ? 0 : transform(value) - baseline;
267 }
268 }
269 }
270 return values;
271 },
272
273 isNullValue: function(value){
274 if(value === null || typeof value == "undefined"){
275 return true;
276 }
277 var h = this._hAxis ? this._hAxis.isNullValue : alwaysFalse,
278 v = this._vAxis ? this._vAxis.isNullValue : alwaysFalse;
279 if(typeof value == "number"){
280 return v(0.5) || h(value);
281 }
282 return v(isNaN(value.x) ? 0.5 : value.x + 0.5) || value.y === null || h(value.y);
283 },
284 getBarProperties: function(){
285 var f = dc.calculateBarSize(this._hScaler.bounds.scale, this.opt);
286 return {gap: f.gap, width: f.size, thickness: 0};
287 },
288 _animateColumn: function(shape, voffset, vsize){
289 if(vsize===0){
290 vsize = 1;
291 }
292 fx.animateTransform(lang.delegate({
293 shape: shape,
294 duration: 1200,
295 transform: [
296 {name: "translate", start: [0, voffset - (voffset/vsize)], end: [0, 0]},
297 {name: "scale", start: [1, 1/vsize], end: [1, 1]},
298 {name: "original"}
299 ]
300 }, this.animate)).play();
301 }
302
303 });
304 });