| 1 |
define(["dojo/_base/lang", "dojo/_base/declare", "dojo/_base/array", "dojo/sniff", |
| 2 |
"./CartesianBase", "./common", "dojox/lang/utils", "dojox/gfx/fx"], |
| 3 |
function(lang, declare, arr, has, CartesianBase, dc, du, fx){ |
| 4 |
|
| 5 |
var sortTicks = function(a,b){return a.value - b.value}; |
| 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 |
return declare("dojox.charting.plot2d.Grid", CartesianBase, { |
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
defaultParams: { |
| 84 |
hMajorLines: true, |
| 85 |
hMinorLines: false, |
| 86 |
vMajorLines: true, |
| 87 |
vMinorLines: false, |
| 88 |
hStripes: false, |
| 89 |
vStripes: false, |
| 90 |
animate: null, |
| 91 |
enableCache: false, |
| 92 |
renderOnAxis: true |
| 93 |
}, |
| 94 |
|
| 95 |
optionalParams: { |
| 96 |
majorHLine: {}, |
| 97 |
minorHLine: {}, |
| 98 |
majorVLine: {}, |
| 99 |
minorVLine: {}, |
| 100 |
hFill: {}, |
| 101 |
vFill: {}, |
| 102 |
hAlternateFill: {}, |
| 103 |
vAlternateFill: {} |
| 104 |
}, |
| 105 |
|
| 106 |
constructor: function(chart, kwArgs){ |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
this.opt = lang.clone(this.defaultParams); |
| 114 |
du.updateWithObject(this.opt, kwArgs); |
| 115 |
du.updateWithPattern(this.opt, kwArgs, this.optionalParams); |
| 116 |
this.animate = this.opt.animate; |
| 117 |
if(this.opt.enableCache){ |
| 118 |
this._lineFreePool = []; |
| 119 |
this._lineUsePool = []; |
| 120 |
this._rectFreePool = []; |
| 121 |
this._rectUsePool = []; |
| 122 |
} |
| 123 |
}, |
| 124 |
addSeries: function(run){ |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
return this; |
| 130 |
}, |
| 131 |
getSeriesStats: function(){ |
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
return lang.delegate(dc.defaultStats); |
| 137 |
}, |
| 138 |
cleanGroup: function(){ |
| 139 |
this.inherited(arguments); |
| 140 |
if(this.opt.enableCache){ |
| 141 |
this._lineFreePool = this._lineFreePool.concat(this._lineUsePool); |
| 142 |
this._lineUsePool = []; |
| 143 |
this._rectFreePool = this._rectFreePool.concat(this._rectUsePool); |
| 144 |
this._rectUsePool = []; |
| 145 |
} |
| 146 |
}, |
| 147 |
createLine: function(creator, params){ |
| 148 |
var line; |
| 149 |
if(this.opt.enableCache && this._lineFreePool.length > 0){ |
| 150 |
line = this._lineFreePool.pop(); |
| 151 |
line.setShape(params); |
| 152 |
|
| 153 |
creator.add(line); |
| 154 |
}else{ |
| 155 |
line = creator.createLine(params); |
| 156 |
} |
| 157 |
if(this.opt.enableCache){ |
| 158 |
this._lineUsePool.push(line); |
| 159 |
} |
| 160 |
return line; |
| 161 |
}, |
| 162 |
createRect: function(creator, params){ |
| 163 |
var rect; |
| 164 |
if(this.opt.enableCache && this._rectFreePool.length > 0){ |
| 165 |
rect = this._rectFreePool.pop(); |
| 166 |
rect.setShape(params); |
| 167 |
|
| 168 |
creator.add(rect); |
| 169 |
}else{ |
| 170 |
rect = creator.createRect(params); |
| 171 |
} |
| 172 |
if(this.opt.enableCache){ |
| 173 |
this._rectUsePool.push(rect); |
| 174 |
} |
| 175 |
return rect; |
| 176 |
}, |
| 177 |
|
| 178 |
render: function(dim, offsets){ |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
if(this.zoom){ |
| 188 |
return this.performZoom(dim, offsets); |
| 189 |
} |
| 190 |
this.dirty = this.isDirty(); |
| 191 |
if(!this.dirty){ return this; } |
| 192 |
this.cleanGroup(); |
| 193 |
var s = this.getGroup(), ta = this.chart.theme, lineStroke, ticks; |
| 194 |
if((has("ios") && has("ios") < 6) || has("android") || (has("safari") && !has("ios"))){ |
| 195 |
|
| 196 |
|
| 197 |
var w = Math.max(0, dim.width - offsets.l - offsets.r), |
| 198 |
h = Math.max(0, dim.height - offsets.t - offsets.b); |
| 199 |
s.createRect({ x: offsets.l, y: offsets.t, width: w, height: h}); |
| 200 |
} |
| 201 |
if(this._vAxis){ |
| 202 |
|
| 203 |
ticks = this._vAxis.getTicks(); |
| 204 |
var vScaler = this._vAxis.getScaler(); |
| 205 |
if(ticks != null && vScaler != null){ |
| 206 |
var vt = vScaler.scaler.getTransformerFromModel(vScaler); |
| 207 |
if(this.opt.hStripes){ |
| 208 |
this._renderHRect(ticks, ta.grid, dim, offsets, vScaler, vt); |
| 209 |
} |
| 210 |
if(this.opt.hMinorLines){ |
| 211 |
lineStroke = this.opt.minorHLine || (ta.grid && ta.grid.minorLine) || ta.axis.minorTick; |
| 212 |
this._renderHLines(ticks.minor, lineStroke, dim, offsets, vScaler, vt); |
| 213 |
} |
| 214 |
if(this.opt.hMajorLines){ |
| 215 |
lineStroke = this.opt.majorHLine || (ta.grid && ta.grid.majorLine) || ta.axis.majorTick; |
| 216 |
this._renderHLines(ticks.major, lineStroke, dim, offsets, vScaler, vt); |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
} |
| 221 |
if(this._hAxis){ |
| 222 |
|
| 223 |
ticks = this._hAxis.getTicks(); |
| 224 |
var hScaler = this._hAxis.getScaler(); |
| 225 |
if(ticks != null && hScaler != null){ |
| 226 |
var ht = hScaler.scaler.getTransformerFromModel(hScaler); |
| 227 |
if(this.opt.vStripes){ |
| 228 |
this._renderVRect(ticks, ta.grid, dim, offsets, hScaler, ht); |
| 229 |
} |
| 230 |
if(ticks && this.opt.vMinorLines){ |
| 231 |
lineStroke = this.opt.minorVLine || (ta.grid && ta.grid.minorLine) || ta.axis.minorTick; |
| 232 |
this._renderVLines(ticks.minor, lineStroke, dim, offsets, hScaler, ht); |
| 233 |
} |
| 234 |
if(ticks && this.opt.vMajorLines){ |
| 235 |
lineStroke = this.opt.majorVLine || (ta.grid && ta.grid.majorLine) || ta.axis.majorTick; |
| 236 |
this._renderVLines(ticks.major, lineStroke, dim, offsets, hScaler, ht); |
| 237 |
} |
| 238 |
|
| 239 |
} |
| 240 |
} |
| 241 |
this.dirty = false; |
| 242 |
return this; |
| 243 |
}, |
| 244 |
_renderHLines: function(ticks, lineStroke, dim, offsets, vScaler, vt){ |
| 245 |
var s = this.getGroup(); |
| 246 |
arr.forEach(ticks, function(tick){ |
| 247 |
if(!this.opt.renderOnAxis && tick.value == (this._vAxis.opt.leftBottom?vScaler.bounds.from:vScaler.bounds.to)){ |
| 248 |
return; |
| 249 |
} |
| 250 |
var y = dim.height - offsets.b - vt(tick.value); |
| 251 |
var hLine = this.createLine(s, { |
| 252 |
x1: offsets.l, |
| 253 |
y1: y, |
| 254 |
x2: dim.width - offsets.r, |
| 255 |
y2: y |
| 256 |
}).setStroke(lineStroke); |
| 257 |
if(this.animate){ |
| 258 |
this._animateGrid(hLine, "h", offsets.l, offsets.r + offsets.l - dim.width); |
| 259 |
} |
| 260 |
}, this); |
| 261 |
}, |
| 262 |
_renderVLines: function(ticks, lineStroke, dim, offsets, hScaler, ht){ |
| 263 |
var s = this.getGroup(); |
| 264 |
arr.forEach(ticks, function(tick){ |
| 265 |
if(!this.opt.renderOnAxis && tick.value == (this._hAxis.opt.leftBottom?hScaler.bounds.from:hScaler.bounds.to)){ |
| 266 |
return; |
| 267 |
} |
| 268 |
var x = offsets.l + ht(tick.value); |
| 269 |
var vLine = this.createLine(s, { |
| 270 |
x1: x, |
| 271 |
y1: offsets.t, |
| 272 |
x2: x, |
| 273 |
y2: dim.height - offsets.b |
| 274 |
}).setStroke(lineStroke); |
| 275 |
if(this.animate){ |
| 276 |
this._animateGrid(vLine, "v", dim.height - offsets.b, dim.height - offsets.b - offsets.t); |
| 277 |
} |
| 278 |
}, this); |
| 279 |
}, |
| 280 |
_renderHRect: function(ticks, theme, dim, offsets, vScaler, vt){ |
| 281 |
var fill, tick, y, y2, hStripe; |
| 282 |
var allTicks = ticks.major.concat(ticks.minor); |
| 283 |
allTicks.sort(sortTicks); |
| 284 |
if(allTicks[0].value > vScaler.bounds.from){ |
| 285 |
allTicks.splice(0, 0, {value: vScaler.bounds.from}); |
| 286 |
} |
| 287 |
if(allTicks[allTicks.length - 1].value < vScaler.bounds.to){ |
| 288 |
allTicks.push({value: vScaler.bounds.to}); |
| 289 |
} |
| 290 |
var s = this.getGroup(); |
| 291 |
for(var j = 0; j < allTicks.length - 1; j++){ |
| 292 |
tick = allTicks[j]; |
| 293 |
y = dim.height - offsets.b - vt(tick.value); |
| 294 |
y2 = dim.height - offsets.b - vt(allTicks[j+1].value); |
| 295 |
|
| 296 |
fill = (j%2 == 0)?(this.opt.hAlternateFill ||(theme && theme.alternateFill)): |
| 297 |
(this.opt.hFill || (theme && theme.fill)); |
| 298 |
if(fill){ |
| 299 |
hStripe = this.createRect(s, { |
| 300 |
x: offsets.l, |
| 301 |
y: y, |
| 302 |
width: dim.width - offsets.r, |
| 303 |
height: y - y2 |
| 304 |
}).setFill(fill); |
| 305 |
if(this.animate){ |
| 306 |
this._animateGrid(hStripe, "h", offsets.l, offsets.r + offsets.l - dim.width); |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
}, |
| 311 |
_renderVRect: function(ticks, theme, dim, offsets, hScaler, ht){ |
| 312 |
var fill, tick, x, x2, vStripe; |
| 313 |
var allTicks = ticks.major.concat(ticks.minor); |
| 314 |
allTicks.sort(sortTicks); |
| 315 |
if(allTicks[0].value > hScaler.bounds.from){ |
| 316 |
allTicks.splice(0, 0, {value: hScaler.bounds.from}); |
| 317 |
} |
| 318 |
if(allTicks[allTicks.length - 1].value < hScaler.bounds.to){ |
| 319 |
allTicks.push({value: hScaler.bounds.to}); |
| 320 |
} |
| 321 |
var s = this.getGroup(); |
| 322 |
for(var j = 0; j < allTicks.length - 1; j++){ |
| 323 |
tick = allTicks[j]; |
| 324 |
x = offsets.l + ht(tick.value); |
| 325 |
x2 = offsets.l + ht(allTicks[j+1].value); |
| 326 |
|
| 327 |
fill = (j%2 == 0)?(this.opt.vAlternateFill ||(theme && theme.alternateFill)): |
| 328 |
(this.opt.vFill || (theme && theme.fill)); |
| 329 |
if(fill){ |
| 330 |
vStripe = this.createRect(s, { |
| 331 |
x: x, |
| 332 |
y: offsets.t, |
| 333 |
width: x2 - x, |
| 334 |
height: dim.width - offsets.r |
| 335 |
}).setFill(fill); |
| 336 |
if(this.animate){ |
| 337 |
this._animateGrid(vStripe, "v", dim.height - offsets.b, dim.height - offsets.b - offsets.t); |
| 338 |
} |
| 339 |
} |
| 340 |
} |
| 341 |
}, |
| 342 |
_animateGrid: function(shape, type, offset, size){ |
| 343 |
var transStart = type == "h" ? [offset, 0] : [0, offset]; |
| 344 |
var scaleStart = type == "h" ? [1/size, 1] : [1, 1/size]; |
| 345 |
fx.animateTransform(lang.delegate({ |
| 346 |
shape: shape, |
| 347 |
duration: 1200, |
| 348 |
transform: [ |
| 349 |
{name: "translate", start: transStart, end: [0, 0]}, |
| 350 |
{name: "scale", start: scaleStart, end: [1, 1]}, |
| 351 |
{name: "original"} |
| 352 |
] |
| 353 |
}, this.animate)).play(); |
| 354 |
} |
| 355 |
}); |
| 356 |
}); |