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