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