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