| 1 |
define(["dojo/_base/lang", "dojo/_base/array", "dojo/_base/declare", "dojo/has", "./CartesianBase", "./_PlotEvents", "./common", |
| 2 |
"dojox/gfx/fx", "dojox/lang/utils", "dojox/lang/functional"], |
| 3 |
function(lang, arr, declare, has, CartesianBase, _PlotEvents, dc, fx, du, df){ |
| 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 |
var alwaysFalse = function(){ return false; } |
| 59 |
|
| 60 |
return declare("dojox.charting.plot2d.Bars", [CartesianBase, _PlotEvents], { |
| 61 |
|
| 62 |
|
| 63 |
defaultParams: { |
| 64 |
gap: 0, |
| 65 |
animate: null, |
| 66 |
enableCache: false |
| 67 |
}, |
| 68 |
optionalParams: { |
| 69 |
minBarSize: 1, |
| 70 |
maxBarSize: 1, |
| 71 |
|
| 72 |
stroke: {}, |
| 73 |
outline: {}, |
| 74 |
shadow: {}, |
| 75 |
fill: {}, |
| 76 |
filter: {}, |
| 77 |
styleFunc: null, |
| 78 |
font: "", |
| 79 |
fontColor: "" |
| 80 |
}, |
| 81 |
|
| 82 |
constructor: function(chart, kwArgs){ |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
this.opt = lang.clone(lang.mixin(this.opt, this.defaultParams)); |
| 90 |
du.updateWithObject(this.opt, kwArgs); |
| 91 |
du.updateWithPattern(this.opt, kwArgs, this.optionalParams); |
| 92 |
this.animate = this.opt.animate; |
| 93 |
this.renderingOptions = { "shape-rendering": "crispEdges" }; |
| 94 |
}, |
| 95 |
|
| 96 |
getSeriesStats: function(){ |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
var stats = dc.collectSimpleStats(this.series, lang.hitch(this, "isNullValue")), t; |
| 102 |
stats.hmin -= 0.5; |
| 103 |
stats.hmax += 0.5; |
| 104 |
t = stats.hmin, stats.hmin = stats.vmin, stats.vmin = t; |
| 105 |
t = stats.hmax, stats.hmax = stats.vmax, stats.vmax = t; |
| 106 |
return stats; |
| 107 |
}, |
| 108 |
|
| 109 |
createRect: function(run, creator, params){ |
| 110 |
var rect; |
| 111 |
if(this.opt.enableCache && run._rectFreePool.length > 0){ |
| 112 |
rect = run._rectFreePool.pop(); |
| 113 |
rect.setShape(params); |
| 114 |
|
| 115 |
creator.add(rect); |
| 116 |
}else{ |
| 117 |
rect = creator.createRect(params); |
| 118 |
} |
| 119 |
if(this.opt.enableCache){ |
| 120 |
run._rectUsePool.push(rect); |
| 121 |
} |
| 122 |
return rect; |
| 123 |
}, |
| 124 |
|
| 125 |
createLabel: function(group, value, bbox, theme){ |
| 126 |
if(this.opt.labels && this.opt.labelStyle == "outside"){ |
| 127 |
var y = bbox.y + bbox.height / 2; |
| 128 |
var x = bbox.x + bbox.width + this.opt.labelOffset; |
| 129 |
this.renderLabel(group, x, y, this._getLabel(isNaN(value.y)?value:value.y), theme, "start"); |
| 130 |
}else{ |
| 131 |
this.inherited(arguments); |
| 132 |
} |
| 133 |
}, |
| 134 |
|
| 135 |
render: function(dim, offsets){ |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
if(this.zoom && !this.isDataDirty()){ |
| 145 |
return this.performZoom(dim, offsets); |
| 146 |
} |
| 147 |
this.dirty = this.isDirty(); |
| 148 |
this.resetEvents(); |
| 149 |
var s; |
| 150 |
if(this.dirty){ |
| 151 |
arr.forEach(this.series, dc.purgeGroup); |
| 152 |
this._eventSeries = {}; |
| 153 |
this.cleanGroup(); |
| 154 |
s = this.getGroup(); |
| 155 |
df.forEachRev(this.series, function(item){ item.cleanGroup(s); }); |
| 156 |
} |
| 157 |
var t = this.chart.theme, |
| 158 |
ht = this._hScaler.scaler.getTransformerFromModel(this._hScaler), |
| 159 |
vt = this._vScaler.scaler.getTransformerFromModel(this._vScaler), |
| 160 |
baseline = Math.max(this._hScaler.bounds.lower, |
| 161 |
this._hAxis ? this._hAxis.naturalBaseline : 0), |
| 162 |
baselineWidth = ht(baseline), |
| 163 |
events = this.events(); |
| 164 |
var bar = this.getBarProperties(); |
| 165 |
|
| 166 |
var actualLength = this.series.length; |
| 167 |
arr.forEach(this.series, function(serie){if(serie.hidden){actualLength--;}}); |
| 168 |
var z = actualLength; |
| 169 |
|
| 170 |
|
| 171 |
var extractedValues = this.extractValues(this._vScaler); |
| 172 |
extractedValues = this.rearrangeValues(extractedValues, ht, baselineWidth); |
| 173 |
|
| 174 |
for(var i = 0; i < this.series.length; i++){ |
| 175 |
var run = this.series[i]; |
| 176 |
if(!this.dirty && !run.dirty){ |
| 177 |
t.skip(); |
| 178 |
this._reconnectEvents(run.name); |
| 179 |
continue; |
| 180 |
} |
| 181 |
run.cleanGroup(); |
| 182 |
if(this.opt.enableCache){ |
| 183 |
run._rectFreePool = (run._rectFreePool?run._rectFreePool:[]).concat(run._rectUsePool?run._rectUsePool:[]); |
| 184 |
run._rectUsePool = []; |
| 185 |
} |
| 186 |
var theme = t.next("bar", [this.opt, run]); |
| 187 |
if(run.hidden){ |
| 188 |
run.dyn.fill = theme.series.fill; |
| 189 |
run.dyn.stroke = theme.series.stroke; |
| 190 |
continue; |
| 191 |
} |
| 192 |
z--; |
| 193 |
|
| 194 |
var eventSeries = new Array(run.data.length); |
| 195 |
s = run.group; |
| 196 |
var indexed = arr.some(run.data, function(item){ |
| 197 |
return typeof item == "number" || (item && !item.hasOwnProperty("x")); |
| 198 |
}); |
| 199 |
|
| 200 |
|
| 201 |
var min = indexed?Math.max(0, Math.floor(this._vScaler.bounds.from - 1)):0; |
| 202 |
var max = indexed?Math.min(run.data.length, Math.ceil(this._vScaler.bounds.to)):run.data.length; |
| 203 |
for(var j = min; j < max; ++j){ |
| 204 |
var value = run.data[j]; |
| 205 |
if(!this.isNullValue(value)){ |
| 206 |
var val = this.getValue(value, j, i, indexed), |
| 207 |
w = extractedValues[i][j], finalTheme, sshape; |
| 208 |
if(this.opt.styleFunc || typeof value != "number"){ |
| 209 |
var tMixin = typeof value != "number" ? [value] : []; |
| 210 |
if(this.opt.styleFunc){ |
| 211 |
tMixin.push(this.opt.styleFunc(value)); |
| 212 |
} |
| 213 |
finalTheme = t.addMixin(theme, "bar", tMixin, true); |
| 214 |
}else{ |
| 215 |
finalTheme = t.post(theme, "bar"); |
| 216 |
} |
| 217 |
if(w && bar.height >= 1){ |
| 218 |
var rect = { |
| 219 |
x: offsets.l + baselineWidth + Math.min(w, 0), |
| 220 |
y: dim.height - offsets.b - vt(val.x + 1.5) + bar.gap + bar.thickness * (actualLength - z - 1), |
| 221 |
width: Math.abs(w), |
| 222 |
height: bar.height |
| 223 |
}; |
| 224 |
if(finalTheme.series.shadow){ |
| 225 |
var srect = lang.clone(rect); |
| 226 |
srect.x += finalTheme.series.shadow.dx; |
| 227 |
srect.y += finalTheme.series.shadow.dy; |
| 228 |
sshape = this.createRect(run, s, srect).setFill(finalTheme.series.shadow.color).setStroke(finalTheme.series.shadow); |
| 229 |
if(this.animate){ |
| 230 |
this._animateBar(sshape, offsets.l + baselineWidth, -w); |
| 231 |
} |
| 232 |
} |
| 233 |
|
| 234 |
var specialFill = this._plotFill(finalTheme.series.fill, dim, offsets); |
| 235 |
specialFill = this._shapeFill(specialFill, rect); |
| 236 |
var shape = this.createRect(run, s, rect).setFill(specialFill).setStroke(finalTheme.series.stroke); |
| 237 |
if(shape.setFilter && finalTheme.series.filter){ |
| 238 |
shape.setFilter(finalTheme.series.filter); |
| 239 |
} |
| 240 |
run.dyn.fill = shape.getFill(); |
| 241 |
run.dyn.stroke = shape.getStroke(); |
| 242 |
if(events){ |
| 243 |
var o = { |
| 244 |
element: "bar", |
| 245 |
index: j, |
| 246 |
run: run, |
| 247 |
shape: shape, |
| 248 |
shadow: sshape, |
| 249 |
cx: val.y, |
| 250 |
cy: val.x + 1.5, |
| 251 |
x: indexed?j:run.data[j].x, |
| 252 |
y: indexed?run.data[j]:run.data[j].y |
| 253 |
}; |
| 254 |
this._connectEvents(o); |
| 255 |
eventSeries[j] = o; |
| 256 |
} |
| 257 |
if(!isNaN(val.py) && val.py > baseline){ |
| 258 |
rect.x += ht(val.py); |
| 259 |
rect.width -= ht(val.py); |
| 260 |
} |
| 261 |
this.createLabel(s, value, rect, finalTheme); |
| 262 |
if(this.animate){ |
| 263 |
this._animateBar(shape, offsets.l + baselineWidth, -Math.abs(w)); |
| 264 |
} |
| 265 |
} |
| 266 |
} |
| 267 |
} |
| 268 |
this._eventSeries[run.name] = eventSeries; |
| 269 |
run.dirty = false; |
| 270 |
} |
| 271 |
this.dirty = false; |
| 272 |
|
| 273 |
if(has("dojo-bidi")){ |
| 274 |
this._checkOrientation(this.group, dim, offsets); |
| 275 |
} |
| 276 |
|
| 277 |
return this; |
| 278 |
}, |
| 279 |
getValue: function(value, j, seriesIndex, indexed){ |
| 280 |
var y, x; |
| 281 |
if(indexed){ |
| 282 |
if(typeof value == "number"){ |
| 283 |
y = value; |
| 284 |
}else{ |
| 285 |
y = value.y; |
| 286 |
} |
| 287 |
x = j; |
| 288 |
}else{ |
| 289 |
y = value.y; |
| 290 |
x = value.x -1; |
| 291 |
} |
| 292 |
return {y:y, x:x}; |
| 293 |
}, |
| 294 |
extractValues: function(scaler){ |
| 295 |
var extracted = []; |
| 296 |
for(var i = this.series.length - 1; i >= 0; --i){ |
| 297 |
var run = this.series[i]; |
| 298 |
if(!this.dirty && !run.dirty){ |
| 299 |
continue; |
| 300 |
} |
| 301 |
|
| 302 |
|
| 303 |
var indexed = arr.some(run.data, function(item){ |
| 304 |
return typeof item == "number" || (item && !item.hasOwnProperty("x")); |
| 305 |
}), |
| 306 |
min = indexed ? Math.max(0, Math.floor(scaler.bounds.from - 1)) : 0, |
| 307 |
max = indexed ? Math.min(run.data.length, Math.ceil(scaler.bounds.to)) : run.data.length, |
| 308 |
extractedSet = extracted[i] = []; |
| 309 |
extractedSet.min = min; |
| 310 |
extractedSet.max = max; |
| 311 |
for(var j = min; j < max; ++j){ |
| 312 |
var value = run.data[j]; |
| 313 |
extractedSet[j] = this.isNullValue(value) ? 0 : |
| 314 |
(typeof value == "number" ? value : value.y); |
| 315 |
} |
| 316 |
} |
| 317 |
return extracted; |
| 318 |
}, |
| 319 |
rearrangeValues: function(values, transform, baseline){ |
| 320 |
|
| 321 |
for(var i = 0, n = values.length; i < n; ++i){ |
| 322 |
var extractedSet = values[i]; |
| 323 |
if(extractedSet){ |
| 324 |
for(var j = extractedSet.min, k = extractedSet.max; j < k; ++j){ |
| 325 |
var value = extractedSet[j]; |
| 326 |
extractedSet[j] = this.isNullValue(value) ? 0 : transform(value) - baseline; |
| 327 |
} |
| 328 |
} |
| 329 |
} |
| 330 |
return values; |
| 331 |
}, |
| 332 |
isNullValue: function(value){ |
| 333 |
if(value === null || typeof value == "undefined"){ |
| 334 |
return true; |
| 335 |
} |
| 336 |
var h = this._hAxis ? this._hAxis.isNullValue : alwaysFalse, |
| 337 |
v = this._vAxis ? this._vAxis.isNullValue : alwaysFalse; |
| 338 |
if(typeof value == "number"){ |
| 339 |
return v(0.5) || h(value); |
| 340 |
} |
| 341 |
return v(isNaN(value.x) ? 0.5 : value.x + 0.5) || value.y === null || h(value.y); |
| 342 |
}, |
| 343 |
getBarProperties: function(){ |
| 344 |
var f = dc.calculateBarSize(this._vScaler.bounds.scale, this.opt); |
| 345 |
return {gap: f.gap, height: f.size, thickness: 0}; |
| 346 |
}, |
| 347 |
_animateBar: function(shape, hoffset, hsize){ |
| 348 |
if(hsize==0){ |
| 349 |
hsize = 1; |
| 350 |
} |
| 351 |
fx.animateTransform(lang.delegate({ |
| 352 |
shape: shape, |
| 353 |
duration: 1200, |
| 354 |
transform: [ |
| 355 |
{name: "translate", start: [hoffset - (hoffset/hsize), 0], end: [0, 0]}, |
| 356 |
{name: "scale", start: [1/hsize, 1], end: [1, 1]}, |
| 357 |
{name: "original"} |
| 358 |
] |
| 359 |
}, this.animate)).play(); |
| 360 |
} |
| 361 |
}); |
| 362 |
}); |