Debian Codegraph @

dojox/charting/plot2d/Bars.js

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