Debian Codegraph @

dojox/charting/plot2d/Scatter.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", "dojox/gfx/gradutils"],
3 function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, df, du, fx, gradutils){
4
5 return declare("dojox.charting.plot2d.Scatter", [CartesianBase, _PlotEvents], {
6 // summary:
7 // A plot object representing a typical scatter chart.
8 defaultParams: {
9 shadows: null, // draw shadows
10 animate: null // animate chart to place
11 },
12 optionalParams: {
13 // theme component
14 markerStroke: {},
15 markerOutline: {},
16 markerShadow: {},
17 markerFill: {},
18 markerFont: "",
19 markerFontColor: "",
20 styleFunc: null
21 },
22
23 constructor: function(chart, kwArgs){
24 // summary:
25 // Create the scatter plot.
26 // chart: dojox/charting/Chart
27 // The chart this plot belongs to.
28 // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs?
29 // An optional keyword arguments object to help define this plot's parameters.
30 this.opt = lang.clone(lang.mixin(this.opt, this.defaultParams));
31 du.updateWithObject(this.opt, kwArgs);
32 du.updateWithPattern(this.opt, kwArgs, this.optionalParams);
33 this.animate = this.opt.animate;
34 },
35
36 render: function(dim, offsets){
37 // summary:
38 // Run the calculations for any axes for this plot.
39 // dim: Object
40 // An object in the form of { width, height }
41 // offsets: Object
42 // An object of the form { l, r, t, b}.
43 // returns: dojox/charting/plot2d/Scatter
44 // A reference to this plot for functional chaining.
45 if(this.zoom && !this.isDataDirty()){
46 return this.performZoom(dim, offsets);
47 }
48 this.resetEvents();
49 this.dirty = this.isDirty();
50 var s;
51 if(this.dirty){
52 arr.forEach(this.series, dc.purgeGroup);
53 this._eventSeries = {};
54 this.cleanGroup();
55 s = this.getGroup();
56 df.forEachRev(this.series, function(item){ item.cleanGroup(s); });
57 }
58 var t = this.chart.theme, events = this.events();
59 for(var i = 0; i < this.series.length; i++){
60 var run = this.series[i];
61 if(!this.dirty && !run.dirty){
62 t.skip();
63 this._reconnectEvents(run.name);
64 continue;
65 }
66 run.cleanGroup();
67 if(!run.data.length){
68 run.dirty = false;
69 t.skip();
70 continue;
71 }
72
73 var theme = t.next("marker", [this.opt, run]), lpoly,
74 ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler),
75 vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler);
76 if(run.hidden){
77 run.dyn.marker = theme.symbol;
78 run.dyn.markerFill = theme.marker.fill;
79 run.dyn.markerStroke = theme.marker.stroke;
80 continue;
81 }
82 s = run.group;
83 if(typeof run.data[0] == "number"){
84 lpoly = arr.map(run.data, function(v, i){
85 return {
86 x: ht(i + 1) + offsets.l,
87 y: dim.height - offsets.b - vt(v)
88 };
89 }, this);
90 }else{
91 lpoly = arr.map(run.data, function(v, i){
92 return {
93 x: ht(v.x) + offsets.l,
94 y: dim.height - offsets.b - vt(v.y)
95 };
96 }, this);
97 }
98
99 var shadowMarkers = new Array(lpoly.length),
100 frontMarkers = new Array(lpoly.length),
101 outlineMarkers = new Array(lpoly.length);
102
103 arr.forEach(lpoly, function(c, i){
104 var value = run.data[i], finalTheme;
105 if(this.opt.styleFunc || typeof value != "number"){
106 var tMixin = typeof value != "number" ? [value] : [];
107 if(this.opt.styleFunc){
108 tMixin.push(this.opt.styleFunc(value));
109 }
110 finalTheme = t.addMixin(theme, "marker", tMixin, true);
111 }else{
112 finalTheme = t.post(theme, "marker");
113 }
114 var path = "M" + c.x + " " + c.y + " " + finalTheme.symbol;
115 if(finalTheme.marker.shadow){
116 shadowMarkers[i] = s.createPath("M" + (c.x + finalTheme.marker.shadow.dx) + " " +
117 (c.y + finalTheme.marker.shadow.dy) + " " + finalTheme.symbol).
118 setStroke(finalTheme.marker.shadow).setFill(finalTheme.marker.shadow.color);
119 if(this.animate){
120 this._animateScatter(shadowMarkers[i], dim.height - offsets.b);
121 }
122 }
123 if(finalTheme.marker.outline){
124 var outline = dc.makeStroke(finalTheme.marker.outline);
125 outline.width = 2 * outline.width + (finalTheme.marker.stroke && finalTheme.marker.stroke.width || 0);
126 outlineMarkers[i] = s.createPath(path).setStroke(outline);
127 if(this.animate){
128 this._animateScatter(outlineMarkers[i], dim.height - offsets.b);
129 }
130 }
131 var stroke = dc.makeStroke(finalTheme.marker.stroke),
132 fill = this._plotFill(finalTheme.marker.fill, dim, offsets);
133 if(fill && (fill.type === "linear" || fill.type == "radial")){
134 var color = gradutils.getColor(fill, {x: c.x, y: c.y});
135 if(stroke){
136 stroke.color = color;
137 }
138 frontMarkers[i] = s.createPath(path).setStroke(stroke).setFill(color);
139 }else{
140 frontMarkers[i] = s.createPath(path).setStroke(stroke).setFill(fill);
141 }
142 if(this.opt.labels){
143 var markerBox = frontMarkers[i].getBoundingBox();
144 this.createLabel(s, value, markerBox, finalTheme);
145 }
146 if(this.animate){
147 this._animateScatter(frontMarkers[i], dim.height - offsets.b);
148 }
149 }, this);
150 if(frontMarkers.length){
151 run.dyn.marker = theme.symbol;
152 run.dyn.markerStroke = frontMarkers[frontMarkers.length - 1].getStroke();
153 run.dyn.markerFill = frontMarkers[frontMarkers.length - 1].getFill();
154 }
155
156 if(events){
157 var eventSeries = new Array(frontMarkers.length);
158 arr.forEach(frontMarkers, function(s, i){
159 var o = {
160 element: "marker",
161 index: i,
162 run: run,
163 shape: s,
164 outline: outlineMarkers && outlineMarkers[i] || null,
165 shadow: shadowMarkers && shadowMarkers[i] || null,
166 cx: lpoly[i].x,
167 cy: lpoly[i].y
168 };
169 if(typeof run.data[0] == "number"){
170 o.x = i + 1;
171 o.y = run.data[i];
172 }else{
173 o.x = run.data[i].x;
174 o.y = run.data[i].y;
175 }
176 this._connectEvents(o);
177 eventSeries[i] = o;
178 }, this);
179 this._eventSeries[run.name] = eventSeries;
180 }else{
181 delete this._eventSeries[run.name];
182 }
183 run.dirty = false;
184 }
185 this.dirty = false;
186 // chart mirroring starts
187 if(has("dojo-bidi")){
188 this._checkOrientation(this.group, dim, offsets);
189 }
190 // chart mirroring ends
191 return this; // dojox/charting/plot2d/Scatter
192 },
193 _animateScatter: function(shape, offset){
194 fx.animateTransform(lang.delegate({
195 shape: shape,
196 duration: 1200,
197 transform: [
198 {name: "translate", start: [0, offset], end: [0, 0]},
199 {name: "scale", start: [0, 0], end: [1, 1]},
200 {name: "original"}
201 ]
202 }, this.animate)).play();
203 }
204 });
205 });