| 1 |
define(["dojo/_base/array", "dojo/dom-construct","dojo/_base/declare", "dojox/gfx", "dojox/gfx/shape"], |
| 2 |
function(arr, domConstruct, declare, gfx, shape){ |
| 3 |
|
| 4 |
return declare("dojox.charting.Element", null, { |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
chart: null, |
| 18 |
group: null, |
| 19 |
htmlElements: null, |
| 20 |
dirty: true, |
| 21 |
renderingOptions: null, |
| 22 |
|
| 23 |
constructor: function(chart, kwArgs){ |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
this.chart = chart; |
| 29 |
this.group = null; |
| 30 |
this.htmlElements = []; |
| 31 |
this.dirty = true; |
| 32 |
this.trailingSymbol = "..."; |
| 33 |
this._events = []; |
| 34 |
if (kwArgs && kwArgs.renderingOptions) { |
| 35 |
this.renderingOptions = kwArgs.renderingOptions; |
| 36 |
} |
| 37 |
}, |
| 38 |
purgeGroup: function(){ |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
this.destroyHtmlElements(); |
| 44 |
if(this.group){ |
| 45 |
|
| 46 |
this.getGroup().removeShape(); |
| 47 |
var children = this.getGroup().children; |
| 48 |
|
| 49 |
if(shape.dispose){ |
| 50 |
for(var i = 0; i < children.length;++i){ |
| 51 |
shape.dispose(children[i], true); |
| 52 |
} |
| 53 |
} |
| 54 |
if(this.getGroup().rawNode){ |
| 55 |
domConstruct.empty(this.getGroup().rawNode); |
| 56 |
} |
| 57 |
this.getGroup().clear(); |
| 58 |
|
| 59 |
if(shape.dispose){ |
| 60 |
shape.dispose(this.getGroup(), true); |
| 61 |
} |
| 62 |
if(this.getGroup() != this.group){ |
| 63 |
|
| 64 |
if(this.group.rawNode){ |
| 65 |
domConstruct.empty(this.group.rawNode); |
| 66 |
} |
| 67 |
this.group.clear(); |
| 68 |
|
| 69 |
if(shape.dispose){ |
| 70 |
shape.dispose(this.group, true); |
| 71 |
} |
| 72 |
} |
| 73 |
this.group = null; |
| 74 |
} |
| 75 |
this.dirty = true; |
| 76 |
if(this._events.length){ |
| 77 |
arr.forEach(this._events, function(item){ |
| 78 |
item.shape.disconnect(item.handle); |
| 79 |
}); |
| 80 |
this._events = []; |
| 81 |
} |
| 82 |
return this; |
| 83 |
}, |
| 84 |
cleanGroup: function(creator){ |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
this.destroyHtmlElements(); |
| 92 |
if(!creator){ creator = this.chart.surface; } |
| 93 |
if(this.group){ |
| 94 |
var bgnode; |
| 95 |
var children = this.getGroup().children; |
| 96 |
|
| 97 |
if(shape.dispose){ |
| 98 |
for(var i = 0; i < children.length;++i){ |
| 99 |
shape.dispose(children[i], true); |
| 100 |
} |
| 101 |
} |
| 102 |
if(this.getGroup().rawNode){ |
| 103 |
bgnode = this.getGroup().bgNode; |
| 104 |
domConstruct.empty(this.getGroup().rawNode); |
| 105 |
} |
| 106 |
this.getGroup().clear(); |
| 107 |
if(bgnode){ |
| 108 |
this.getGroup().rawNode.appendChild(bgnode); |
| 109 |
} |
| 110 |
}else{ |
| 111 |
this.group = creator.createGroup(); |
| 112 |
|
| 113 |
|
| 114 |
if (this.renderingOptions && this.group.rawNode && |
| 115 |
this.group.rawNode.namespaceURI == "http://www.w3.org/2000/svg") { |
| 116 |
for (var key in this.renderingOptions) { |
| 117 |
this.group.rawNode.setAttribute(key, this.renderingOptions[key]); |
| 118 |
} |
| 119 |
} |
| 120 |
} |
| 121 |
this.dirty = true; |
| 122 |
return this; |
| 123 |
}, |
| 124 |
getGroup: function(){ |
| 125 |
return this.group; |
| 126 |
}, |
| 127 |
destroyHtmlElements: function(){ |
| 128 |
|
| 129 |
|
| 130 |
if(this.htmlElements.length){ |
| 131 |
arr.forEach(this.htmlElements, domConstruct.destroy); |
| 132 |
this.htmlElements = []; |
| 133 |
} |
| 134 |
}, |
| 135 |
destroy: function(){ |
| 136 |
|
| 137 |
|
| 138 |
this.purgeGroup(); |
| 139 |
}, |
| 140 |
overrideShape: function(shape, params){ |
| 141 |
|
| 142 |
|
| 143 |
}, |
| 144 |
|
| 145 |
getTextWidth: function(s, font){ |
| 146 |
return gfx._base._getTextBox(s, {font: font}).w || 0; |
| 147 |
}, |
| 148 |
getTextWithLimitLength: function(s, font, limitWidth, truncated){ |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
if(!s || s.length <= 0){ |
| 165 |
return { |
| 166 |
text: "", |
| 167 |
truncated: truncated || false |
| 168 |
}; |
| 169 |
} |
| 170 |
if(!limitWidth || limitWidth <= 0){ |
| 171 |
return { |
| 172 |
text: s, |
| 173 |
truncated: truncated || false |
| 174 |
}; |
| 175 |
} |
| 176 |
var delta = 2, |
| 177 |
|
| 178 |
trucPercentage = 0.618, |
| 179 |
minStr = s.substring(0,1) + this.trailingSymbol, |
| 180 |
minWidth = this.getTextWidth(minStr, font); |
| 181 |
if(limitWidth <= minWidth){ |
| 182 |
return { |
| 183 |
text: minStr, |
| 184 |
truncated: true |
| 185 |
}; |
| 186 |
} |
| 187 |
var width = this.getTextWidth(s, font); |
| 188 |
if(width <= limitWidth){ |
| 189 |
return { |
| 190 |
text: s, |
| 191 |
truncated: truncated || false |
| 192 |
}; |
| 193 |
}else{ |
| 194 |
var begin = 0, |
| 195 |
end = s.length; |
| 196 |
while(begin < end){ |
| 197 |
if(end - begin <= delta ){ |
| 198 |
while (this.getTextWidth(s.substring(0, begin) + this.trailingSymbol, font) > limitWidth) { |
| 199 |
begin -= 1; |
| 200 |
} |
| 201 |
return { |
| 202 |
text: (s.substring(0,begin) + this.trailingSymbol), |
| 203 |
truncated: true |
| 204 |
}; |
| 205 |
} |
| 206 |
var index = begin + Math.round((end - begin) * trucPercentage), |
| 207 |
widthIntercepted = this.getTextWidth(s.substring(0, index), font); |
| 208 |
if(widthIntercepted < limitWidth){ |
| 209 |
begin = index; |
| 210 |
end = end; |
| 211 |
}else{ |
| 212 |
begin = begin; |
| 213 |
end = index; |
| 214 |
} |
| 215 |
} |
| 216 |
} |
| 217 |
}, |
| 218 |
getTextWithLimitCharCount: function(s, font, wcLimit, truncated){ |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
if (!s || s.length <= 0) { |
| 235 |
return { |
| 236 |
text: "", |
| 237 |
truncated: truncated || false |
| 238 |
}; |
| 239 |
} |
| 240 |
if(!wcLimit || wcLimit <= 0 || s.length <= wcLimit){ |
| 241 |
return { |
| 242 |
text: s, |
| 243 |
truncated: truncated || false |
| 244 |
}; |
| 245 |
} |
| 246 |
return { |
| 247 |
text: s.substring(0, wcLimit) + this.trailingSymbol, |
| 248 |
truncated: true |
| 249 |
}; |
| 250 |
}, |
| 251 |
|
| 252 |
_plotFill: function(fill, dim, offsets){ |
| 253 |
|
| 254 |
if(!fill || !fill.type || !fill.space){ |
| 255 |
return fill; |
| 256 |
} |
| 257 |
var space = fill.space, span; |
| 258 |
switch(fill.type){ |
| 259 |
case "linear": |
| 260 |
if(space === "plot" || space === "shapeX" || space === "shapeY"){ |
| 261 |
|
| 262 |
fill = gfx.makeParameters(gfx.defaultLinearGradient, fill); |
| 263 |
fill.space = space; |
| 264 |
|
| 265 |
if(space === "plot" || space === "shapeX"){ |
| 266 |
|
| 267 |
span = dim.height - offsets.t - offsets.b; |
| 268 |
fill.y1 = offsets.t + span * fill.y1 / 100; |
| 269 |
fill.y2 = offsets.t + span * fill.y2 / 100; |
| 270 |
} |
| 271 |
if(space === "plot" || space === "shapeY"){ |
| 272 |
|
| 273 |
span = dim.width - offsets.l - offsets.r; |
| 274 |
fill.x1 = offsets.l + span * fill.x1 / 100; |
| 275 |
fill.x2 = offsets.l + span * fill.x2 / 100; |
| 276 |
} |
| 277 |
} |
| 278 |
break; |
| 279 |
case "radial": |
| 280 |
if(space === "plot"){ |
| 281 |
|
| 282 |
|
| 283 |
fill = gfx.makeParameters(gfx.defaultRadialGradient, fill); |
| 284 |
fill.space = space; |
| 285 |
|
| 286 |
var spanX = dim.width - offsets.l - offsets.r, |
| 287 |
spanY = dim.height - offsets.t - offsets.b; |
| 288 |
fill.cx = offsets.l + spanX * fill.cx / 100; |
| 289 |
fill.cy = offsets.t + spanY * fill.cy / 100; |
| 290 |
fill.r = fill.r * Math.sqrt(spanX * spanX + spanY * spanY) / 200; |
| 291 |
} |
| 292 |
break; |
| 293 |
case "pattern": |
| 294 |
if(space === "plot" || space === "shapeX" || space === "shapeY"){ |
| 295 |
|
| 296 |
fill = gfx.makeParameters(gfx.defaultPattern, fill); |
| 297 |
fill.space = space; |
| 298 |
|
| 299 |
if(space === "plot" || space === "shapeX"){ |
| 300 |
|
| 301 |
span = dim.height - offsets.t - offsets.b; |
| 302 |
fill.y = offsets.t + span * fill.y / 100; |
| 303 |
fill.height = span * fill.height / 100; |
| 304 |
} |
| 305 |
if(space === "plot" || space === "shapeY"){ |
| 306 |
|
| 307 |
span = dim.width - offsets.l - offsets.r; |
| 308 |
fill.x = offsets.l + span * fill.x / 100; |
| 309 |
fill.width = span * fill.width / 100; |
| 310 |
} |
| 311 |
} |
| 312 |
break; |
| 313 |
} |
| 314 |
return fill; |
| 315 |
}, |
| 316 |
_shapeFill: function(fill, bbox){ |
| 317 |
|
| 318 |
if(!fill || !fill.space){ |
| 319 |
return fill; |
| 320 |
} |
| 321 |
var space = fill.space, span; |
| 322 |
switch(fill.type){ |
| 323 |
case "linear": |
| 324 |
if(space === "shape" || space === "shapeX" || space === "shapeY"){ |
| 325 |
|
| 326 |
fill = gfx.makeParameters(gfx.defaultLinearGradient, fill); |
| 327 |
fill.space = space; |
| 328 |
|
| 329 |
if(space === "shape" || space === "shapeX"){ |
| 330 |
|
| 331 |
span = bbox.width; |
| 332 |
fill.x1 = bbox.x + span * fill.x1 / 100; |
| 333 |
fill.x2 = bbox.x + span * fill.x2 / 100; |
| 334 |
} |
| 335 |
if(space === "shape" || space === "shapeY"){ |
| 336 |
|
| 337 |
span = bbox.height; |
| 338 |
fill.y1 = bbox.y + span * fill.y1 / 100; |
| 339 |
fill.y2 = bbox.y + span * fill.y2 / 100; |
| 340 |
} |
| 341 |
} |
| 342 |
break; |
| 343 |
case "radial": |
| 344 |
if(space === "shape"){ |
| 345 |
|
| 346 |
|
| 347 |
fill = gfx.makeParameters(gfx.defaultRadialGradient, fill); |
| 348 |
fill.space = space; |
| 349 |
|
| 350 |
fill.cx = bbox.x + bbox.width / 2; |
| 351 |
fill.cy = bbox.y + bbox.height / 2; |
| 352 |
fill.r = fill.r * bbox.width / 200; |
| 353 |
} |
| 354 |
break; |
| 355 |
case "pattern": |
| 356 |
if(space === "shape" || space === "shapeX" || space === "shapeY"){ |
| 357 |
|
| 358 |
fill = gfx.makeParameters(gfx.defaultPattern, fill); |
| 359 |
fill.space = space; |
| 360 |
|
| 361 |
if(space === "shape" || space === "shapeX"){ |
| 362 |
|
| 363 |
span = bbox.width; |
| 364 |
fill.x = bbox.x + span * fill.x / 100; |
| 365 |
fill.width = span * fill.width / 100; |
| 366 |
} |
| 367 |
if(space === "shape" || space === "shapeY"){ |
| 368 |
|
| 369 |
span = bbox.height; |
| 370 |
fill.y = bbox.y + span * fill.y / 100; |
| 371 |
fill.height = span * fill.height / 100; |
| 372 |
} |
| 373 |
} |
| 374 |
break; |
| 375 |
} |
| 376 |
return fill; |
| 377 |
}, |
| 378 |
_pseudoRadialFill: function(fill, center, radius, start, end){ |
| 379 |
|
| 380 |
if(!fill || fill.type !== "radial" || fill.space !== "shape"){ |
| 381 |
return fill; |
| 382 |
} |
| 383 |
|
| 384 |
var space = fill.space; |
| 385 |
fill = gfx.makeParameters(gfx.defaultRadialGradient, fill); |
| 386 |
fill.space = space; |
| 387 |
if(arguments.length < 4){ |
| 388 |
|
| 389 |
fill.cx = center.x; |
| 390 |
fill.cy = center.y; |
| 391 |
fill.r = fill.r * radius / 100; |
| 392 |
return fill; |
| 393 |
} |
| 394 |
|
| 395 |
var angle = arguments.length < 5 ? start : (end + start) / 2; |
| 396 |
return { |
| 397 |
type: "linear", |
| 398 |
x1: center.x, |
| 399 |
y1: center.y, |
| 400 |
x2: center.x + fill.r * radius * Math.cos(angle) / 100, |
| 401 |
y2: center.y + fill.r * radius * Math.sin(angle) / 100, |
| 402 |
colors: fill.colors |
| 403 |
}; |
| 404 |
} |
| 405 |
}); |
| 406 |
}); |