1 | define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/has", |
---|
2 | "./CartesianBase", "./_PlotEvents", "./common", "dojox/lang/functional", "dojox/lang/functional/reversed", |
---|
3 | "dojox/lang/utils", "dojox/gfx/fx"], |
---|
4 | function(lang, declare, arr, has, CartesianBase, _PlotEvents, dc, df, dfr, du, fx){ |
---|
5 | |
---|
6 | var purgeGroup = dfr.lambda("item.purgeGroup()"); |
---|
7 | |
---|
8 | return declare("dojox.charting.plot2d.Bubble", [CartesianBase, _PlotEvents], { |
---|
9 | // summary: |
---|
10 | // A plot representing bubbles. Note that data for Bubbles requires 3 parameters, |
---|
11 | // in the form of: { x, y, size }, where size determines the size of the bubble. |
---|
12 | defaultParams: { |
---|
13 | animate: null // animate bars into place |
---|
14 | }, |
---|
15 | optionalParams: { |
---|
16 | // theme component |
---|
17 | stroke: {}, |
---|
18 | outline: {}, |
---|
19 | shadow: {}, |
---|
20 | fill: {}, |
---|
21 | filter: {}, |
---|
22 | styleFunc: null, |
---|
23 | font: "", |
---|
24 | fontColor: "", |
---|
25 | labelFunc: null |
---|
26 | }, |
---|
27 | |
---|
28 | constructor: function(chart, kwArgs){ |
---|
29 | // summary: |
---|
30 | // Create a plot of bubbles. |
---|
31 | // chart: dojox/charting/Chart |
---|
32 | // The chart this plot belongs to. |
---|
33 | // kwArgs: dojox.charting.plot2d.__DefaultCtorArgs? |
---|
34 | // Optional keyword arguments object to help define plot parameters. |
---|
35 | this.opt = lang.clone(lang.mixin(this.opt, this.defaultParams)); |
---|
36 | du.updateWithObject(this.opt, kwArgs); |
---|
37 | du.updateWithPattern(this.opt, kwArgs, this.optionalParams); |
---|
38 | if(!this.opt.labelFunc){ |
---|
39 | this.opt.labelFunc = function(value, fixed, precision){ |
---|
40 | return this._getLabel(value.size, fixed, precision); |
---|
41 | }; |
---|
42 | } |
---|
43 | this.animate = this.opt.animate; |
---|
44 | }, |
---|
45 | |
---|
46 | // override the render so that we are plotting only circles. |
---|
47 | render: function(dim, offsets){ |
---|
48 | // summary: |
---|
49 | // Run the calculations for any axes for this plot. |
---|
50 | // dim: Object |
---|
51 | // An object in the form of { width, height } |
---|
52 | // offsets: Object |
---|
53 | // An object of the form { l, r, t, b}. |
---|
54 | // returns: dojox/charting/plot2d/Bubble |
---|
55 | // A reference to this plot for functional chaining. |
---|
56 | var s; |
---|
57 | if(this.zoom && !this.isDataDirty()){ |
---|
58 | return this.performZoom(dim, offsets); |
---|
59 | } |
---|
60 | this.resetEvents(); |
---|
61 | this.dirty = this.isDirty(); |
---|
62 | if(this.dirty){ |
---|
63 | arr.forEach(this.series, purgeGroup); |
---|
64 | this._eventSeries = {}; |
---|
65 | this.cleanGroup(); |
---|
66 | s = this.getGroup(); |
---|
67 | df.forEachRev(this.series, function(item){ item.cleanGroup(s); }); |
---|
68 | } |
---|
69 | |
---|
70 | var t = this.chart.theme, |
---|
71 | ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler), |
---|
72 | vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler), |
---|
73 | events = this.events(); |
---|
74 | |
---|
75 | for(var i = this.series.length - 1; i >= 0; --i){ |
---|
76 | var run = this.series[i]; |
---|
77 | if(!this.dirty && !run.dirty){ |
---|
78 | t.skip(); |
---|
79 | this._reconnectEvents(run.name); |
---|
80 | continue; |
---|
81 | } |
---|
82 | run.cleanGroup(); |
---|
83 | if(!run.data.length){ |
---|
84 | run.dirty = false; |
---|
85 | t.skip(); |
---|
86 | continue; |
---|
87 | } |
---|
88 | |
---|
89 | if(typeof run.data[0] == "number"){ |
---|
90 | console.warn("dojox.charting.plot2d.Bubble: the data in the following series cannot be rendered as a bubble chart; ", run); |
---|
91 | continue; |
---|
92 | } |
---|
93 | |
---|
94 | s = run.group; |
---|
95 | var theme = t.next("circle", [this.opt, run]), |
---|
96 | points = arr.map(run.data, function(v){ |
---|
97 | return v ? { |
---|
98 | x: ht(v.x) + offsets.l, |
---|
99 | y: dim.height - offsets.b - vt(v.y), |
---|
100 | radius: this._vScaler.bounds.scale * (v.size / 2) |
---|
101 | } : null; |
---|
102 | }, this); |
---|
103 | |
---|
104 | var frontCircles = null, outlineCircles = null, shadowCircles = null, styleFunc = this.opt.styleFunc; |
---|
105 | |
---|
106 | var getFinalTheme = function(item){ |
---|
107 | if(styleFunc){ |
---|
108 | return t.addMixin(theme, "circle", [item, styleFunc(item)], true); |
---|
109 | } |
---|
110 | return t.addMixin(theme, "circle", item, true); |
---|
111 | }; |
---|
112 | |
---|
113 | // make shadows if needed |
---|
114 | if(theme.series.shadow){ |
---|
115 | shadowCircles = arr.map(points, function(item, i){ |
---|
116 | if(item !== null){ |
---|
117 | var finalTheme = getFinalTheme(run.data[i]), |
---|
118 | shadow = finalTheme.series.shadow; |
---|
119 | var shape = s.createCircle({ |
---|
120 | cx: item.x + shadow.dx, cy: item.y + shadow.dy, r: item.radius |
---|
121 | }).setStroke(shadow).setFill(shadow.color); |
---|
122 | if(this.animate){ |
---|
123 | this._animateBubble(shape, dim.height - offsets.b, item.radius); |
---|
124 | } |
---|
125 | return shape; |
---|
126 | } |
---|
127 | return null; |
---|
128 | }, this); |
---|
129 | if(shadowCircles.length){ |
---|
130 | run.dyn.shadow = shadowCircles[shadowCircles.length - 1].getStroke(); |
---|
131 | } |
---|
132 | } |
---|
133 | |
---|
134 | // make outlines if needed |
---|
135 | if(theme.series.outline){ |
---|
136 | outlineCircles = arr.map(points, function(item, i){ |
---|
137 | if(item !== null){ |
---|
138 | var finalTheme = getFinalTheme(run.data[i]), |
---|
139 | outline = dc.makeStroke(finalTheme.series.outline); |
---|
140 | outline.width = 2 * outline.width + theme.series.stroke.width; |
---|
141 | var shape = s.createCircle({ |
---|
142 | cx: item.x, cy: item.y, r: item.radius |
---|
143 | }).setStroke(outline); |
---|
144 | if(this.animate){ |
---|
145 | this._animateBubble(shape, dim.height - offsets.b, item.radius); |
---|
146 | } |
---|
147 | return shape; |
---|
148 | } |
---|
149 | return null; |
---|
150 | }, this); |
---|
151 | if(outlineCircles.length){ |
---|
152 | run.dyn.outline = outlineCircles[outlineCircles.length - 1].getStroke(); |
---|
153 | } |
---|
154 | } |
---|
155 | |
---|
156 | // run through the data and add the circles. |
---|
157 | frontCircles = arr.map(points, function(item, i){ |
---|
158 | if(item !== null){ |
---|
159 | var finalTheme = getFinalTheme(run.data[i]), |
---|
160 | rect = { |
---|
161 | x: item.x - item.radius, |
---|
162 | y: item.y - item.radius, |
---|
163 | width: 2 * item.radius, |
---|
164 | height: 2 * item.radius |
---|
165 | }; |
---|
166 | var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets); |
---|
167 | specialFill = this._shapeFill(specialFill, rect); |
---|
168 | var shape = s.createCircle({ |
---|
169 | cx: item.x, cy: item.y, r: item.radius |
---|
170 | }).setFill(specialFill).setStroke(finalTheme.series.stroke); |
---|
171 | if(shape.setFilter && finalTheme.series.filter){ |
---|
172 | shape.setFilter(finalTheme.series.filter); |
---|
173 | } |
---|
174 | if(this.animate){ |
---|
175 | this._animateBubble(shape, dim.height - offsets.b, item.radius); |
---|
176 | } |
---|
177 | this.createLabel(s, run.data[i], rect, finalTheme); |
---|
178 | return shape; |
---|
179 | } |
---|
180 | return null; |
---|
181 | }, this); |
---|
182 | if(frontCircles.length){ |
---|
183 | run.dyn.fill = frontCircles[frontCircles.length - 1].getFill(); |
---|
184 | run.dyn.stroke = frontCircles[frontCircles.length - 1].getStroke(); |
---|
185 | } |
---|
186 | |
---|
187 | if(events){ |
---|
188 | var eventSeries = new Array(frontCircles.length); |
---|
189 | arr.forEach(frontCircles, function(s, i){ |
---|
190 | if(s !== null){ |
---|
191 | var o = { |
---|
192 | element: "circle", |
---|
193 | index: i, |
---|
194 | run: run, |
---|
195 | shape: s, |
---|
196 | outline: outlineCircles && outlineCircles[i] || null, |
---|
197 | shadow: shadowCircles && shadowCircles[i] || null, |
---|
198 | x: run.data[i].x, |
---|
199 | y: run.data[i].y, |
---|
200 | r: run.data[i].size / 2, |
---|
201 | cx: points[i].x, |
---|
202 | cy: points[i].y, |
---|
203 | cr: points[i].radius |
---|
204 | }; |
---|
205 | this._connectEvents(o); |
---|
206 | eventSeries[i] = o; |
---|
207 | } |
---|
208 | }, this); |
---|
209 | this._eventSeries[run.name] = eventSeries; |
---|
210 | }else{ |
---|
211 | delete this._eventSeries[run.name]; |
---|
212 | } |
---|
213 | |
---|
214 | run.dirty = false; |
---|
215 | } |
---|
216 | this.dirty = false; |
---|
217 | // chart mirroring starts |
---|
218 | if(has("dojo-bidi")){ |
---|
219 | this._checkOrientation(this.group, dim, offsets); |
---|
220 | } |
---|
221 | // chart mirroring ends |
---|
222 | return this; // dojox/charting/plot2d/Bubble |
---|
223 | }, |
---|
224 | _animateBubble: function(shape, offset, size){ |
---|
225 | fx.animateTransform(lang.delegate({ |
---|
226 | shape: shape, |
---|
227 | duration: 1200, |
---|
228 | transform: [ |
---|
229 | {name: "translate", start: [0, offset], end: [0, 0]}, |
---|
230 | {name: "scale", start: [0, 1/size], end: [1, 1]}, |
---|
231 | {name: "original"} |
---|
232 | ] |
---|
233 | }, this.animate)).play(); |
---|
234 | } |
---|
235 | }); |
---|
236 | }); |
---|