Debian Codegraph @

dojox/charting/plot2d/Default.js

raw symbols SCIP
1 define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has",
2 "./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional", "dojox/lang/utils", "dojox/gfx/fx"],
3 function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, du, fx){
4
5 /*=====
6 declare("dojox.charting.plot2d.__DefaultCtorArgs", dojox.charting.plot2d.__CartesianCtorArgs, {
7 // summary:
8 // The arguments used for any/most plots.
9
10 // lines: Boolean?
11 // Whether or not to draw lines on this plot. Defaults to true.
12 lines: true,
13
14 // areas: Boolean?
15 // Whether or not to draw areas on this plot. Defaults to false.
16 areas: false,
17
18 // markers: Boolean?
19 // Whether or not to draw markers at data points on this plot. Default is false.
20 markers: false,
21
22 // tension: Number|String?
23 // Whether or not to apply 'tensioning' to the lines on this chart.
24 // Options include a number, "X", "x", or "S"; if a number is used, the
25 // simpler bezier curve calculations are used to draw the lines. If X, x or S
26 // is used, the more accurate smoothing algorithm is used.
27 tension: "",
28
29 // animate: Boolean?|Number?
30 // Whether or not to animate the chart to place. When a Number it specifies the duration of the animation.
31 // Default is false.
32 animate: false,
33
34 // stroke: dojox.gfx.Stroke?
35 // An optional stroke to use for any series on the plot.
36 stroke: {},
37
38 // outline: dojox.gfx.Stroke?
39 // An optional stroke used to outline any series on the plot.
40 outline: {},
41
42 // shadow: dojox.gfx.Stroke?
43 // An optional stroke to use to draw any shadows for a series on a plot.
44 shadow: {},
45
46 // fill: dojox.gfx.Fill?
47 // Any fill to be used for elements on the plot (such as areas).
48 fill: {},
49
50 // filter: dojox.gfx.Filter?
51 // An SVG filter to be used for elements on the plot. gfx SVG renderer must be used and dojox/gfx/svgext must
52 // be required for this to work.
53 filter: {},
54
55 // styleFunc: Function?
56 // A function that returns a styling object for the a given data item.
57 styleFunc: null,
58
59 // font: String?
60 // A font definition to be used for labels and other text-based elements on the plot.
61 font: "",
62
63 // fontColor: String|dojo.Color?
64 // The color to be used for any text-based elements on the plot.
65 fontColor: "",
66
67 // markerStroke: dojo.gfx.Stroke?
68 // An optional stroke to use for any markers on the plot.
69 markerStroke: {},
70
71 // markerOutline: dojo.gfx.Stroke?
72 // An optional outline to use for any markers on the plot.
73 markerOutline: {},
74
75 // markerShadow: dojo.gfx.Stroke?
76 // An optional shadow to use for any markers on the plot.
77 markerShadow: {},
78
79 // markerFill: dojo.gfx.Fill?
80 // An optional fill to use for any markers on the plot.
81 markerFill: {},
82
83 // markerFont: String?
84 // An optional font definition to use for any markers on the plot.
85 markerFont: "",
86
87 // markerFontColor: String|dojo.Color?
88 // An optional color to use for any marker text on the plot.
89 markerFontColor: "",
90
91 // enableCache: Boolean?
92 // Whether the markers are cached from one rendering to another. This improves the rendering performance of
93 // successive rendering but penalize the first rendering. Default false.
94 enableCache: false,
95
96 // interpolate: Boolean?
97 // Whether when there is a null data point in the data the plot interpolates it or if the lines is split at that
98 // point. Default false.
99 interpolate: false,
100
101 // zeroLine: Number?
102 // Zero line for an area fill. Should be a numeric value in user coordinates.
103 // Default: the lowest value on a vertical axis.
104 zeroLine: 0
105 });
106 =====*/
107
108 var DEFAULT_ANIMATION_LENGTH = 1200; // in ms
109
110 return declare("dojox.charting.plot2d.Default", [CartesianBase, _PlotEvents], {
111
112 // defaultParams:
113 // The default parameters of this plot.
114 defaultParams: {
115 lines: true, // draw lines
116 areas: false, // draw areas
117 markers: false, // draw markers
118 tension: "", // draw curved lines (tension is "X", "x", or "S")
119 animate: false, // animate chart to place
120 enableCache: false,
121 interpolate: false
122 },
123
124 // optionalParams:
125 // The optional parameters of this plot.
126 optionalParams: {
127 // theme component
128 stroke: {},
129 outline: {},
130 shadow: {},
131 fill: {},
132 filter: {},
133 styleFunc: null,
134 font: "",
135 fontColor: "",
136 marker: "",
137 markerStroke: {},
138 markerOutline: {},
139 markerShadow: {},
140 markerFill: {},
141 markerFont: "",
142 markerFontColor: "",
143 zeroLine: 0
144 },
145
146 constructor: function(chart, kwArgs){
147 // summary:
148 // Return a new plot.
149 // chart: dojox/charting/Chart
150 // The chart this plot belongs to.
151 // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
152 // An optional arguments object to help define this plot.
153 this.opt = lang.clone(lang.mixin(this.opt, this.defaultParams));
154 du.updateWithObject(this.opt, kwArgs);
155 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
156 // animation properties
157 this.animate = this.opt.animate;
158 },
159
160 createPath: function(run, creator, params){
161 var path;
162 if(this.opt.enableCache && run._pathFreePool.length > 0){
163 path = run._pathFreePool.pop();
164 path.setShape(params);
165 // was cleared, add it back
166 creator.add(path);
167 }else{
168 path = creator.createPath(params);
169 }
170 if(this.opt.enableCache){
171 run._pathUsePool.push(path);
172 }
173 return path;
174 },
175
176 buildSegments: function(i, indexed){
177 var run = this.series[i],
178 min = indexed?Math.max(0, Math.floor(this._hScaler.bounds.from - 1)):0,
179 max = indexed?Math.min(run.data.length, Math.ceil(this._hScaler.bounds.to)):run.data.length,
180 rseg = null, segments = [];
181
182 // split the run data into dense segments (each containing no nulls)
183 // except if interpolates is false in which case ignore null between valid data
184 for(var j = min; j < max; j++){
185 if(!this.isNullValue(run.data[j])){
186 if(!rseg){
187 rseg = [];
188 segments.push({index: j, rseg: rseg});
189 }
190 rseg.push((indexed && run.data[j].hasOwnProperty("y"))?run.data[j].y:run.data[j]);
191 }else{
192 if(!this.opt.interpolate || indexed){
193 // we break the line only if not interpolating or if we have indexed data
194 rseg = null;
195 }
196 }
197 }
198 return segments;
199 },
200
201 render: function(dim, offsets){
202 // summary:
203 // Render/draw everything on this plot.
204 // dim: Object
205 // An object of the form { width, height }
206 // offsets: Object
207 // An object of the form { l, r, t, b }
208 // returns: dojox/charting/plot2d/Default
209 // A reference to this plot for functional chaining.
210
211 // make sure all the series is not modified
212 if(this.zoom && !this.isDataDirty()){
213 return this.performZoom(dim, offsets);
214 }
215
216 this.resetEvents();
217 this.dirty = this.isDirty();
218 var s;
219 if(this.dirty){
220 arr.forEach(this.series, dc.purgeGroup);
221 this._eventSeries = {};
222 this.cleanGroup();
223 this.getGroup().setTransform(null);
224 s = this.getGroup();
225 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
226 }
227 var t = this.chart.theme, stroke, outline, events = this.events();
228
229 for(var i = 0; i < this.series.length; i++){
230 var run = this.series[i];
231 if(!this.dirty && !run.dirty){
232 t.skip();
233 this._reconnectEvents(run.name);
234 continue;
235 }
236 run.cleanGroup();
237 if(this.opt.enableCache){
238 run._pathFreePool = (run._pathFreePool?run._pathFreePool:[]).concat(run._pathUsePool?run._pathUsePool:[]);
239 run._pathUsePool = [];
240 }
241 if(!run.data.length){
242 run.dirty = false;
243 t.skip();
244 continue;
245 }
246
247 var theme = t.next(this.opt.areas ? "area" : "line", [this.opt, run], true),
248 lpoly,
249 ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
250 vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler),
251 eventSeries = this._eventSeries[run.name] = new Array(run.data.length);
252
253 s = run.group;
254 if(run.hidden){
255 if(this.opt.lines){
256 run.dyn.stroke = theme.series.stroke;
257 }
258 if (run.markers || (run.markers === undefined && this.opt.markers)) {
259 run.dyn.markerFill = theme.marker.fill;
260 run.dyn.markerStroke = theme.marker.stroke;
261 run.dyn.marker = theme.symbol;
262 }
263 if(this.opt.areas){
264 run.dyn.fill = theme.series.fill;
265 }
266 continue;
267 }
268
269 // optim works only for index based case
270 var indexed = arr.some(run.data, function(item){
271 return typeof item == "number" || (item && !item.hasOwnProperty("x"));
272 });
273
274 var rsegments = this.buildSegments(i, indexed);
275 for(var seg = 0; seg < rsegments.length; seg++){
276 var rsegment = rsegments[seg];
277 if(indexed){
278 lpoly = arr.map(rsegment.rseg, function(v, i){
279 return {
280 x: ht(i + rsegment.index + 1) + offsets.l,
281 y: dim.height - offsets.b - vt(v),
282 data: v
283 };
284 }, this);
285 }else{
286 lpoly = arr.map(rsegment.rseg, function(v){
287 return {
288 x: ht(v.x) + offsets.l,
289 y: dim.height - offsets.b - vt(v.y),
290 data: v
291 };
292 }, this);
293 }
294
295 // if we are indexed & we interpolate we need to put all the segments as a single one now
296 if(indexed && this.opt.interpolate){
297 while(seg < rsegments.length) {
298 seg++;
299 rsegment = rsegments[seg];
300 if(rsegment){
301 lpoly = lpoly.concat(arr.map(rsegment.rseg, function(v, i){
302 return {
303 x: ht(i + rsegment.index + 1) + offsets.l,
304 y: dim.height - offsets.b - vt(v),
305 data: v
306 };
307 }, this));
308 }
309 }
310 }
311
312 var lpath = this.opt.tension ? dc.curve(lpoly, this.opt.tension) : "";
313
314 if(this.opt.areas && lpoly.length > 1){
315 var fill = this._plotFill(theme.series.fill, dim, offsets), apoly = lang.clone(lpoly),
316 zeroLine = dim.height - offsets.b;
317 if(!isNaN(this.opt.zeroLine)){
318 zeroLine = Math.max(offsets.t, Math.min(dim.height - offsets.b - vt(this.opt.zeroLine), zeroLine));
319 }
320 if(this.opt.tension){
321 var apath = "L" + apoly[apoly.length-1].x + "," + zeroLine +
322 " L" + apoly[0].x + "," + zeroLine +
323 " L" + apoly[0].x + "," + apoly[0].y;
324 run.dyn.fill = s.createPath(lpath + " " + apath).setFill(fill).getFill();
325 } else {
326 apoly.push({x: lpoly[lpoly.length - 1].x, y: zeroLine});
327 apoly.push({x: lpoly[0].x, y: zeroLine});
328 apoly.push(lpoly[0]);
329 run.dyn.fill = s.createPolyline(apoly).setFill(fill).getFill();
330 }
331 }
332 if(this.opt.lines || this.opt.markers){
333 // need a stroke
334 stroke = theme.series.stroke;
335 if(theme.series.outline){
336 outline = run.dyn.outline = dc.makeStroke(theme.series.outline);
337 outline.width = 2 * outline.width + (stroke && stroke.width || 0);
338 }
339 }
340 if(this.opt.markers){
341 run.dyn.marker = theme.symbol;
342 }
343 var frontMarkers = null, outlineMarkers = null, shadowMarkers = null;
344 if(stroke && theme.series.shadow && lpoly.length > 1){
345 var shadow = theme.series.shadow,
346 spoly = arr.map(lpoly, function(c){
347 return {x: c.x + shadow.dx, y: c.y + shadow.dy};
348 });
349 if(this.opt.lines){
350 if(this.opt.tension){
351 run.dyn.shadow = s.createPath(dc.curve(spoly, this.opt.tension)).setStroke(shadow).getStroke();
352 } else {
353 run.dyn.shadow = s.createPolyline(spoly).setStroke(shadow).getStroke();
354 }
355 }
356 if(this.opt.markers && theme.marker.shadow){
357 shadow = theme.marker.shadow;
358 shadowMarkers = arr.map(spoly, function(c){
359 return this.createPath(run, s, "M" + c.x + " " + c.y + " " + theme.symbol).
360 setStroke(shadow).setFill(shadow.color);
361 }, this);
362 }
363 }
364 if(this.opt.lines && lpoly.length > 1){
365 var shape;
366 if(outline){
367 if(this.opt.tension){
368 run.dyn.outline = s.createPath(lpath).setStroke(outline).getStroke();
369 } else {
370 run.dyn.outline = s.createPolyline(lpoly).setStroke(outline).getStroke();
371 }
372 }
373 if(this.opt.tension){
374 run.dyn.stroke = (shape = s.createPath(lpath)).setStroke(stroke).getStroke();
375 } else {
376 run.dyn.stroke = (shape = s.createPolyline(lpoly)).setStroke(stroke).getStroke();
377 }
378 if(shape.setFilter && theme.series.filter){
379 shape.setFilter(theme.series.filter);
380 }
381 }
382 var markerBox = null;
383 if(this.opt.markers){
384 var markerTheme = theme;
385 frontMarkers = new Array(lpoly.length);
386 outlineMarkers = new Array(lpoly.length);
387 outline = null;
388 if(markerTheme.marker.outline){
389 outline = dc.makeStroke(markerTheme.marker.outline);
390 outline.width = 2 * outline.width + (markerTheme.marker.stroke ? markerTheme.marker.stroke.width : 0);
391 }
392 arr.forEach(lpoly, function(c, i){
393 if(this.opt.styleFunc || typeof c.data != "number"){
394 var tMixin = typeof c.data != "number" ? [c.data] : [];
395 if(this.opt.styleFunc){
396 tMixin.push(this.opt.styleFunc(c.data));
397 }
398 markerTheme = t.addMixin(theme, "marker", tMixin, true);
399 }else{
400 markerTheme = t.post(theme, "marker");
401 }
402 var path = "M" + c.x + " " + c.y + " " + markerTheme.symbol;
403 if(outline){
404 outlineMarkers[i] = this.createPath(run, s, path).setStroke(outline);
405 }
406 frontMarkers[i] = this.createPath(run, s, path).setStroke(markerTheme.marker.stroke).setFill(markerTheme.marker.fill);
407 }, this);
408 run.dyn.markerFill = markerTheme.marker.fill;
409 run.dyn.markerStroke = markerTheme.marker.stroke;
410 if(!markerBox && this.opt.labels){
411 markerBox = frontMarkers[0].getBoundingBox();
412 }
413 if(events){
414 arr.forEach(frontMarkers, function(s, i){
415 var o = {
416 element: "marker",
417 index: i + rsegment.index,
418 run: run,
419 shape: s,
420 outline: outlineMarkers[i] || null,
421 shadow: shadowMarkers && shadowMarkers[i] || null,
422 cx: lpoly[i].x,
423 cy: lpoly[i].y
424 };
425 if(indexed){
426 o.x = i + rsegment.index + 1;
427 o.y = run.data[i + rsegment.index];
428 }else{
429 o.x = rsegment.rseg[i].x;
430 o.y = run.data[i + rsegment.index].y;
431 }
432 this._connectEvents(o);
433 eventSeries[i + rsegment.index] = o;
434 }, this);
435 }else{
436 delete this._eventSeries[run.name];
437 }
438 }
439 if(this.opt.labels){
440 var labelBoxW = markerBox?markerBox.width:2;
441 var labelBoxH = markerBox?markerBox.height:2;
442 arr.forEach(lpoly, function(c, i){
443 if(this.opt.styleFunc || typeof c.data != "number"){
444 var tMixin = typeof c.data != "number" ? [c.data] : [];
445 if(this.opt.styleFunc){
446 tMixin.push(this.opt.styleFunc(c.data));
447 }
448 markerTheme = t.addMixin(theme, "marker", tMixin, true);
449 }else{
450 markerTheme = t.post(theme, "marker");
451 }
452 this.createLabel(s, rsegment.rseg[i], { x: c.x - labelBoxW / 2, y: c.y - labelBoxH / 2,
453 width: labelBoxW , height: labelBoxH }, markerTheme);
454 }, this);
455 }
456 }
457 run.dirty = false;
458 }
459 // chart mirroring starts
460 if(has("dojo-bidi")){
461 this._checkOrientation(this.group, dim, offsets);
462 }
463 // chart mirroring ends
464 if(this.animate){
465 // grow from the bottom
466 var plotGroup = this.getGroup();
467 fx.animateTransform(lang.delegate({
468 shape: plotGroup,
469 duration: DEFAULT_ANIMATION_LENGTH,
470 transform:[
471 {name:"translate", start: [0, dim.height - offsets.b], end: [0, 0]},
472 {name:"scale", start: [1, 0], end:[1, 1]},
473 {name:"original"}
474 ]
475 }, this.animate)).play();
476 }
477 this.dirty = false;
478 return this; // dojox/charting/plot2d/Default
479 }
480 });
481 });