| 1 |
define(["dojo/_base/lang", "dojo/_base/array" ,"dojo/_base/declare", "dojo/dom-geometry", "dojo/_base/Color", |
| 2 |
"./Base", "./_PlotEvents", "./common", |
| 3 |
"dojox/gfx", "dojox/gfx/matrix", "dojox/lang/functional", "dojox/lang/utils","dojo/has"], |
| 4 |
function(lang, arr, declare, domGeom, Color, Base, PlotEvents, dc, g, m, df, du, has){ |
| 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 |
var FUDGE_FACTOR = 0.2; |
| 89 |
|
| 90 |
return declare("dojox.charting.plot2d.Pie", [Base, PlotEvents], { |
| 91 |
|
| 92 |
|
| 93 |
defaultParams: { |
| 94 |
labels: true, |
| 95 |
ticks: false, |
| 96 |
fixed: true, |
| 97 |
precision: 1, |
| 98 |
labelOffset: 20, |
| 99 |
labelStyle: "default", |
| 100 |
htmlLabels: true, |
| 101 |
radGrad: "native", |
| 102 |
fanSize: 5, |
| 103 |
startAngle: 0, |
| 104 |
innerRadius: 0, |
| 105 |
minWidth: 0, |
| 106 |
zeroDataMessage: "" |
| 107 |
}, |
| 108 |
optionalParams: { |
| 109 |
radius: 0, |
| 110 |
omitLabels: false, |
| 111 |
|
| 112 |
stroke: {}, |
| 113 |
outline: {}, |
| 114 |
shadow: {}, |
| 115 |
fill: {}, |
| 116 |
filter: {}, |
| 117 |
styleFunc: null, |
| 118 |
font: "", |
| 119 |
fontColor: "", |
| 120 |
labelWiring: {} |
| 121 |
}, |
| 122 |
|
| 123 |
constructor: function(chart, kwArgs){ |
| 124 |
|
| 125 |
|
| 126 |
this.opt = lang.clone(this.defaultParams); |
| 127 |
du.updateWithObject(this.opt, kwArgs); |
| 128 |
du.updateWithPattern(this.opt, kwArgs, this.optionalParams); |
| 129 |
this.axes = []; |
| 130 |
this.run = null; |
| 131 |
this.dyn = []; |
| 132 |
this.runFilter = []; |
| 133 |
if(kwArgs && kwArgs.hasOwnProperty("innerRadius")){ |
| 134 |
this._plotSetInnerRadius = true; |
| 135 |
} |
| 136 |
}, |
| 137 |
clear: function(){ |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
this.inherited(arguments); |
| 143 |
this.dyn = []; |
| 144 |
this.run = null; |
| 145 |
return this; |
| 146 |
}, |
| 147 |
setAxis: function(axis){ |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
return this; |
| 153 |
}, |
| 154 |
addSeries: function(run){ |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
this.run = run; |
| 160 |
return this; |
| 161 |
}, |
| 162 |
getSeriesStats: function(){ |
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
return lang.delegate(dc.defaultStats); |
| 168 |
}, |
| 169 |
getRequiredColors: function(){ |
| 170 |
|
| 171 |
|
| 172 |
return this.run ? this.run.data.length : 0; |
| 173 |
}, |
| 174 |
|
| 175 |
render: function(dim, offsets){ |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
if(!this.dirty){ return this; } |
| 185 |
this.resetEvents(); |
| 186 |
this.dirty = false; |
| 187 |
this._eventSeries = {}; |
| 188 |
this.cleanGroup(); |
| 189 |
var s = this.group, t = this.chart.theme; |
| 190 |
|
| 191 |
if(!this._plotSetInnerRadius && t && t.pieInnerRadius){ |
| 192 |
this.opt.innerRadius = t.pieInnerRadius; |
| 193 |
} |
| 194 |
|
| 195 |
|
| 196 |
var rx = (dim.width - offsets.l - offsets.r) / 2, |
| 197 |
ry = (dim.height - offsets.t - offsets.b) / 2, |
| 198 |
r = Math.min(rx, ry), |
| 199 |
taFont = "font" in this.opt ? this.opt.font : t.axis.tick.titleFont || "", |
| 200 |
size = taFont ? g.normalizedLength(g.splitFontString(taFont).size) : 0, |
| 201 |
taFontColor = this.opt.hasOwnProperty("fontColor") ? this.opt.fontColor : t.axis.tick.fontColor, |
| 202 |
startAngle = m._degToRad(this.opt.startAngle), |
| 203 |
start = startAngle, filteredRun, slices, labels, shift, labelR, |
| 204 |
run = this.run.data, |
| 205 |
events = this.events(); |
| 206 |
|
| 207 |
|
| 208 |
var noDataFunc = lang.hitch(this, function(){ |
| 209 |
var ct = t.clone(); |
| 210 |
var themes = df.map(run, function(v){ |
| 211 |
var tMixin = [this.opt, this.run]; |
| 212 |
if(v !== null && typeof v != "number"){ |
| 213 |
tMixin.push(v); |
| 214 |
} |
| 215 |
if(this.opt.styleFunc){ |
| 216 |
tMixin.push(this.opt.styleFunc(v)); |
| 217 |
} |
| 218 |
return ct.next("slice", tMixin, true); |
| 219 |
}, this); |
| 220 |
|
| 221 |
|
| 222 |
if("radius" in this.opt){ |
| 223 |
r = this.opt.radius < r ? this.opt.radius : r; |
| 224 |
} |
| 225 |
|
| 226 |
var circle = { |
| 227 |
cx: offsets.l + rx, |
| 228 |
cy: offsets.t + ry, |
| 229 |
r: r |
| 230 |
}; |
| 231 |
var rColor = new Color(taFontColor); |
| 232 |
|
| 233 |
if(this.opt.innerRadius){ |
| 234 |
rColor.a = 0.1; |
| 235 |
} |
| 236 |
var ring = this._createRing(s, circle).setStroke(rColor); |
| 237 |
if(this.opt.innerRadius){ |
| 238 |
|
| 239 |
ring.setFill(rColor); |
| 240 |
} |
| 241 |
if(this.opt.zeroDataMessage){ |
| 242 |
this.renderLabel(s, circle.cx, circle.cy + size/3, this.opt.zeroDataMessage, { |
| 243 |
series: { |
| 244 |
font: taFont, |
| 245 |
fontColor: taFontColor |
| 246 |
} |
| 247 |
}, null, "middle"); |
| 248 |
} |
| 249 |
this.dyn = []; |
| 250 |
arr.forEach(run, function(item, i){ |
| 251 |
this.dyn.push({ |
| 252 |
fill: this._plotFill(themes[i].series.fill, dim, offsets), |
| 253 |
stroke: themes[i].series.stroke}); |
| 254 |
}, this); |
| 255 |
}); |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
if(!this.run && !this.run.data.ength){ |
| 260 |
noDataFunc(); |
| 261 |
return this; |
| 262 |
} |
| 263 |
if(typeof run[0] == "number"){ |
| 264 |
filteredRun = df.map(run, "x ? Math.max(x, 0) : 0"); |
| 265 |
if(df.every(filteredRun, "<= 0")){ |
| 266 |
noDataFunc(); |
| 267 |
return this; |
| 268 |
} |
| 269 |
slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); |
| 270 |
if(this.opt.labels){ |
| 271 |
labels = arr.map(slices, function(x){ |
| 272 |
return x > 0 ? this._getLabel(x * 100) + "%" : ""; |
| 273 |
}, this); |
| 274 |
} |
| 275 |
}else{ |
| 276 |
filteredRun = df.map(run, "x ? Math.max(x.y, 0) : 0"); |
| 277 |
if(!filteredRun.length || df.every(filteredRun, "<= 0")){ |
| 278 |
noDataFunc(); |
| 279 |
return this; |
| 280 |
} |
| 281 |
slices = df.map(filteredRun, "/this", df.foldl(filteredRun, "+", 0)); |
| 282 |
if(this.opt.labels){ |
| 283 |
labels = arr.map(slices, function(x, i){ |
| 284 |
if(x < 0){ return ""; } |
| 285 |
var v = run[i]; |
| 286 |
return v.hasOwnProperty("text") ? v.text : this._getLabel(x * 100) + "%"; |
| 287 |
}, this); |
| 288 |
} |
| 289 |
} |
| 290 |
var themes = df.map(run, function(v){ |
| 291 |
var tMixin = [this.opt, this.run]; |
| 292 |
if(v !== null && typeof v != "number"){ |
| 293 |
tMixin.push(v); |
| 294 |
} |
| 295 |
if(this.opt.styleFunc){ |
| 296 |
tMixin.push(this.opt.styleFunc(v)); |
| 297 |
} |
| 298 |
return t.next("slice", tMixin, true); |
| 299 |
}, this); |
| 300 |
|
| 301 |
if(this.opt.labels) { |
| 302 |
shift = df.foldl1(df.map(labels, function(label, i){ |
| 303 |
var font = themes[i].series.font; |
| 304 |
return g._base._getTextBox(label, {font: font}).w; |
| 305 |
}, this), "Math.max(a, b)") / 2; |
| 306 |
|
| 307 |
if(this.opt.labelOffset < 0){ |
| 308 |
r = Math.min(rx - 2 * shift, ry - size) + this.opt.labelOffset; |
| 309 |
} |
| 310 |
} |
| 311 |
if(this.opt.hasOwnProperty("radius")){ |
| 312 |
r = this.opt.radius < r * 0.9 ? this.opt.radius : r * 0.9; |
| 313 |
} |
| 314 |
|
| 315 |
if (!this.opt.radius && this.opt.labels && this.opt.labelStyle == "columns") { |
| 316 |
r = r / 2; |
| 317 |
if (rx > ry && rx > r * 2) { |
| 318 |
r *= rx / (r * 2); |
| 319 |
} |
| 320 |
if (r >= ry * 0.8) { |
| 321 |
r = ry * 0.8; |
| 322 |
} |
| 323 |
} else { |
| 324 |
if (r >= ry * 0.9) { |
| 325 |
r = ry * 0.9; |
| 326 |
} |
| 327 |
} |
| 328 |
|
| 329 |
labelR = r - this.opt.labelOffset; |
| 330 |
|
| 331 |
var circle = { |
| 332 |
cx: offsets.l + rx, |
| 333 |
cy: offsets.t + ry, |
| 334 |
r: r |
| 335 |
}; |
| 336 |
|
| 337 |
this.dyn = []; |
| 338 |
|
| 339 |
var eventSeries = new Array(slices.length); |
| 340 |
|
| 341 |
|
| 342 |
var slicesSteps = [], localStart = start; |
| 343 |
var minWidth = this.opt.minWidth; |
| 344 |
arr.forEach(slices, function(slice, i){ |
| 345 |
if(slice === 0){ |
| 346 |
slicesSteps[i] = { |
| 347 |
step: 0, |
| 348 |
end: localStart, |
| 349 |
start: localStart, |
| 350 |
weak: false |
| 351 |
}; |
| 352 |
return; |
| 353 |
} |
| 354 |
var end = localStart + slice * 2 * Math.PI; |
| 355 |
if(i === slices.length - 1){ |
| 356 |
end = startAngle + 2 * Math.PI; |
| 357 |
} |
| 358 |
var step = end - localStart, |
| 359 |
dist = step * r; |
| 360 |
slicesSteps[i] = { |
| 361 |
step: step, |
| 362 |
start: localStart, |
| 363 |
end: end, |
| 364 |
weak: dist < minWidth |
| 365 |
}; |
| 366 |
localStart = end; |
| 367 |
}); |
| 368 |
|
| 369 |
if(minWidth > 0){ |
| 370 |
var weakCount = 0, weakCoef = minWidth / r, oldWeakCoefSum = 0, i; |
| 371 |
for(i = slicesSteps.length - 1; i >= 0; i--){ |
| 372 |
if(slicesSteps[i].weak){ |
| 373 |
++weakCount; |
| 374 |
oldWeakCoefSum += slicesSteps[i].step; |
| 375 |
slicesSteps[i].step = weakCoef; |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
var weakCoefSum = weakCount * weakCoef; |
| 380 |
if(weakCoefSum > Math.PI){ |
| 381 |
weakCoef = Math.PI / weakCount; |
| 382 |
for(i = 0; i < slicesSteps.length; ++i){ |
| 383 |
if(slicesSteps[i].weak){ |
| 384 |
slicesSteps[i].step = weakCoef; |
| 385 |
} |
| 386 |
} |
| 387 |
weakCoefSum = Math.PI; |
| 388 |
} |
| 389 |
|
| 390 |
if(weakCount > 0){ |
| 391 |
weakCoef = 1 - (weakCoefSum - oldWeakCoefSum) / 2 / Math.PI; |
| 392 |
for(i = 0; i < slicesSteps.length; ++i){ |
| 393 |
if(!slicesSteps[i].weak){ |
| 394 |
slicesSteps[i].step = weakCoef * slicesSteps[i].step; |
| 395 |
} |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
for(i = 0; i < slicesSteps.length; ++i){ |
| 400 |
slicesSteps[i].start = i ? slicesSteps[i].end : localStart; |
| 401 |
slicesSteps[i].end = slicesSteps[i].start + slicesSteps[i].step; |
| 402 |
} |
| 403 |
|
| 404 |
for(i = slicesSteps.length - 1; i >= 0; --i){ |
| 405 |
if(slicesSteps[i].step !== 0){ |
| 406 |
slicesSteps[i].end = localStart + 2 * Math.PI; |
| 407 |
break; |
| 408 |
} |
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
localStart = start; |
| 413 |
var o, specialFill; |
| 414 |
arr.some(slices, function(slice, i){ |
| 415 |
var shape; |
| 416 |
var v = run[i], theme = themes[i]; |
| 417 |
|
| 418 |
if(slice >= 1){ |
| 419 |
|
| 420 |
specialFill = this._plotFill(theme.series.fill, dim, offsets); |
| 421 |
specialFill = this._shapeFill(specialFill, |
| 422 |
{ |
| 423 |
x: circle.cx - circle.r, y: circle.cy - circle.r, |
| 424 |
width: 2 * circle.r, height: 2 * circle.r |
| 425 |
}); |
| 426 |
specialFill = this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, circle.r); |
| 427 |
shape = this._createRing(s, circle).setFill(specialFill).setStroke(theme.series.stroke); |
| 428 |
this.dyn.push({fill: specialFill, stroke: theme.series.stroke}); |
| 429 |
|
| 430 |
if(events){ |
| 431 |
o = { |
| 432 |
element: "slice", |
| 433 |
index: i, |
| 434 |
run: this.run, |
| 435 |
shape: shape, |
| 436 |
x: i, |
| 437 |
y: typeof v == "number" ? v : v.y, |
| 438 |
cx: circle.cx, |
| 439 |
cy: circle.cy, |
| 440 |
cr: r |
| 441 |
}; |
| 442 |
this._connectEvents(o); |
| 443 |
eventSeries[i] = o; |
| 444 |
} |
| 445 |
|
| 446 |
var k; |
| 447 |
for(k = i + 1; k < slices.length; k++){ |
| 448 |
theme = themes[k]; |
| 449 |
this.dyn.push({fill: theme.series.fill, stroke: theme.series.stroke}); |
| 450 |
} |
| 451 |
return true; |
| 452 |
} |
| 453 |
|
| 454 |
if(slicesSteps[i].step === 0){ |
| 455 |
|
| 456 |
|
| 457 |
|
| 458 |
this.dyn.push({fill: theme.series.fill, stroke: theme.series.stroke}); |
| 459 |
return false; |
| 460 |
} |
| 461 |
|
| 462 |
|
| 463 |
var step = slicesSteps[i].step, |
| 464 |
x1 = circle.cx + r * Math.cos(localStart), |
| 465 |
y1 = circle.cy + r * Math.sin(localStart), |
| 466 |
x2 = circle.cx + r * Math.cos(localStart + step), |
| 467 |
y2 = circle.cy + r * Math.sin(localStart + step); |
| 468 |
|
| 469 |
var fanSize = m._degToRad(this.opt.fanSize), stroke; |
| 470 |
if(theme.series.fill && theme.series.fill.type === "radial" && this.opt.radGrad === "fan" && step > fanSize){ |
| 471 |
var group = s.createGroup(), nfans = Math.ceil(step / fanSize), delta = step / nfans; |
| 472 |
specialFill = this._shapeFill(theme.series.fill, |
| 473 |
{x: circle.cx - circle.r, y: circle.cy - circle.r, width: 2 * circle.r, height: 2 * circle.r}); |
| 474 |
var j, alpha, beta, fansx, fansy, fanex, faney; |
| 475 |
for(j = 0; j < nfans; ++j){ |
| 476 |
alpha = localStart + (j - FUDGE_FACTOR) * delta; |
| 477 |
beta = localStart + (j + 1 + FUDGE_FACTOR) * delta; |
| 478 |
fansx = j == 0 ? x1 : circle.cx + r * Math.cos(alpha); |
| 479 |
fansy = j == 0 ? y1 : circle.cy + r * Math.sin(alpha); |
| 480 |
fanex = j == nfans - 1 ? x2 : circle.cx + r * Math.cos(beta); |
| 481 |
faney = j == nfans - 1 ? y2 : circle.cy + r * Math.sin(beta); |
| 482 |
this._createSlice(group, circle, r, fansx, fansy, fanex, faney, alpha, delta). |
| 483 |
setFill(this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, r, |
| 484 |
localStart + (j + 0.5) * delta, localStart + (j + 0.5) * delta)); |
| 485 |
} |
| 486 |
stroke = theme.series.stroke; |
| 487 |
this._createSlice(group, circle, r, x1, y1, x2, y2, localStart, step).setStroke(stroke); |
| 488 |
shape = group; |
| 489 |
}else{ |
| 490 |
stroke = theme.series.stroke; |
| 491 |
|
| 492 |
shape = this._createSlice(s, circle, r, x1, y1, x2, y2, localStart, step).setStroke(stroke); |
| 493 |
|
| 494 |
specialFill = theme.series.fill; |
| 495 |
if(specialFill && specialFill.type === "radial"){ |
| 496 |
specialFill = this._shapeFill(specialFill, {x: circle.cx - circle.r, y: circle.cy - circle.r, width: 2 * circle.r, height: 2 * circle.r}); |
| 497 |
if(this.opt.radGrad === "linear"){ |
| 498 |
specialFill = this._pseudoRadialFill(specialFill, {x: circle.cx, y: circle.cy}, r, localStart, localStart + step); |
| 499 |
} |
| 500 |
}else if(specialFill && specialFill.type === "linear"){ |
| 501 |
var bbox = lang.clone(shape.getBoundingBox()); |
| 502 |
if(g.renderer === "svg"){ |
| 503 |
|
| 504 |
|
| 505 |
var pos = {w: 0, h: 0}; |
| 506 |
try{ |
| 507 |
pos = domGeom.position(shape.rawNode); |
| 508 |
}catch(ignore){} |
| 509 |
if(pos.h > bbox.height){ |
| 510 |
bbox.height = pos.h; |
| 511 |
} |
| 512 |
if(pos.w > bbox.width){ |
| 513 |
bbox.width = pos.w; |
| 514 |
} |
| 515 |
} |
| 516 |
specialFill = this._plotFill(specialFill, dim, offsets); |
| 517 |
specialFill = this._shapeFill(specialFill, bbox); |
| 518 |
} |
| 519 |
shape.setFill(specialFill); |
| 520 |
} |
| 521 |
this.dyn.push({fill: specialFill, stroke: theme.series.stroke}); |
| 522 |
|
| 523 |
if(events){ |
| 524 |
o = { |
| 525 |
element: "slice", |
| 526 |
index: i, |
| 527 |
run: this.run, |
| 528 |
shape: shape, |
| 529 |
x: i, |
| 530 |
y: typeof v == "number" ? v : v.y, |
| 531 |
cx: circle.cx, |
| 532 |
cy: circle.cy, |
| 533 |
cr: r |
| 534 |
}; |
| 535 |
this._connectEvents(o); |
| 536 |
eventSeries[i] = o; |
| 537 |
} |
| 538 |
|
| 539 |
localStart = localStart + step; |
| 540 |
|
| 541 |
return false; |
| 542 |
}, this); |
| 543 |
|
| 544 |
if(this.opt.labels){ |
| 545 |
var isRtl = has("dojo-bidi") && this.chart.isRightToLeft(); |
| 546 |
if(this.opt.labelStyle == "default"){ |
| 547 |
start = startAngle; |
| 548 |
localStart = start; |
| 549 |
arr.some(slices, function(slice, i){ |
| 550 |
if(slice <= 0 && !this.opt.minWidth){ |
| 551 |
|
| 552 |
return false; |
| 553 |
} |
| 554 |
var theme = themes[i]; |
| 555 |
if(slice >= 1){ |
| 556 |
|
| 557 |
this.renderLabel(s, circle.cx, circle.cy + size / 2, labels[i], theme, this.opt.labelOffset > 0); |
| 558 |
return true; |
| 559 |
} |
| 560 |
|
| 561 |
var end = start + slice * 2 * Math.PI; |
| 562 |
if(i + 1 == slices.length){ |
| 563 |
end = startAngle + 2 * Math.PI; |
| 564 |
} |
| 565 |
|
| 566 |
if(this.opt.omitLabels && end-start < 0.001){ |
| 567 |
return false; |
| 568 |
} |
| 569 |
|
| 570 |
var labelAngle = localStart + (slicesSteps[i].step / 2), |
| 571 |
x = circle.cx + labelR * Math.cos(labelAngle), |
| 572 |
y = circle.cy + labelR * Math.sin(labelAngle) + size / 2; |
| 573 |
|
| 574 |
this.renderLabel(s, isRtl ? dim.width - x : x, y, labels[i], theme, this.opt.labelOffset > 0); |
| 575 |
localStart += slicesSteps[i].step; |
| 576 |
start = end; |
| 577 |
return false; |
| 578 |
}, this); |
| 579 |
}else if(this.opt.labelStyle == "columns"){ |
| 580 |
|
| 581 |
var omitLabels = this.opt.omitLabels; |
| 582 |
start = startAngle; |
| 583 |
localStart = start; |
| 584 |
var labeledSlices = [], |
| 585 |
significantCount = 0, k; |
| 586 |
for(k = slices.length - 1; k >= 0; --k){ |
| 587 |
if(slices[k]){ |
| 588 |
++significantCount; |
| 589 |
} |
| 590 |
} |
| 591 |
arr.forEach(slices, function(slice, i){ |
| 592 |
var end = start + slice * 2 * Math.PI; |
| 593 |
if(i + 1 == slices.length){ |
| 594 |
end = startAngle + 2 * Math.PI; |
| 595 |
} |
| 596 |
if(this.minWidth !== 0 || end - start >= 0.001){ |
| 597 |
|
| 598 |
var labelAngle = localStart + (slicesSteps[i].step / 2); |
| 599 |
if(significantCount === 1 && !this.opt.minWidth){ |
| 600 |
labelAngle = (start + end) / 2; |
| 601 |
} |
| 602 |
labeledSlices.push({ |
| 603 |
angle: labelAngle, |
| 604 |
left: Math.cos(labelAngle) < 0, |
| 605 |
theme: themes[i], |
| 606 |
index: i, |
| 607 |
omit: omitLabels? end - start < 0.001:false |
| 608 |
}); |
| 609 |
} |
| 610 |
start = end; |
| 611 |
localStart += slicesSteps[i].step; |
| 612 |
}, this); |
| 613 |
|
| 614 |
|
| 615 |
var labelHeight = g._base._getTextBox("a", {font:taFont, whiteSpace: "nowrap"}).h; |
| 616 |
this._getProperLabelRadius(labeledSlices, labelHeight, circle.r * 1.1); |
| 617 |
|
| 618 |
|
| 619 |
var leftColumn = circle.cx - circle.r * 2, |
| 620 |
rightColumn = circle.cx + circle.r * 2; |
| 621 |
arr.forEach(labeledSlices, function(slice){ |
| 622 |
if(slice.omit){ |
| 623 |
return; |
| 624 |
} |
| 625 |
var cTheme = themes[slice.index], lrPadding = 0; |
| 626 |
if(cTheme && cTheme.axis && cTheme.axis.tick && cTheme.axis.tick.labelGap){ |
| 627 |
|
| 628 |
lrPadding = cTheme.axis.tick.labelGap; |
| 629 |
} |
| 630 |
var labelWidth = g._base._getTextBox(labels[slice.index], |
| 631 |
{font: cTheme.series.font, whiteSpace: "nowrap", paddingLeft: lrPadding + "px"}).w, |
| 632 |
x = circle.cx + slice.labelR * Math.cos(slice.angle), |
| 633 |
y = circle.cy + slice.labelR * Math.sin(slice.angle), |
| 634 |
jointX = (slice.left) ? (leftColumn + labelWidth) : (rightColumn - labelWidth), |
| 635 |
labelX = (slice.left) ? leftColumn : jointX + lrPadding, |
| 636 |
newRadius = circle.r, |
| 637 |
wiring = s.createPath().moveTo(circle.cx + newRadius * Math.cos(slice.angle), |
| 638 |
circle.cy + newRadius * Math.sin(slice.angle)); |
| 639 |
if(Math.abs(slice.labelR * Math.cos(slice.angle)) < circle.r * 2 - labelWidth){ |
| 640 |
wiring.lineTo(x, y); |
| 641 |
} |
| 642 |
wiring.lineTo(jointX, y).setStroke(slice.theme.series.labelWiring); |
| 643 |
|
| 644 |
wiring.moveToBack(); |
| 645 |
|
| 646 |
|
| 647 |
var mid = labelHeight/3 + y; |
| 648 |
var elem = this.renderLabel(s, labelX, mid || 0, labels[slice.index], cTheme, false, "left"); |
| 649 |
|
| 650 |
if(events && !this.opt.htmlLabels){ |
| 651 |
var fontWidth = g._base._getTextBox(labels[slice.index], {font: slice.theme.series.font}).w || 0, |
| 652 |
fontHeight = g.normalizedLength(g.splitFontString(slice.theme.series.font).size); |
| 653 |
o = { |
| 654 |
element: "labels", |
| 655 |
index: slice.index, |
| 656 |
run: this.run, |
| 657 |
shape: elem, |
| 658 |
x: labelX, |
| 659 |
y: y, |
| 660 |
label: labels[slice.index] |
| 661 |
}; |
| 662 |
|
| 663 |
var shp = elem.getShape(), |
| 664 |
lt = domGeom.position(this.chart.node, true), |
| 665 |
aroundRect = lang.mixin({ type : 'rect' }, { |
| 666 |
x: shp.x, |
| 667 |
y: shp.y - 2 * fontHeight |
| 668 |
}); |
| 669 |
|
| 670 |
aroundRect.x += lt.x; |
| 671 |
aroundRect.y += lt.y; |
| 672 |
aroundRect.x = Math.round(aroundRect.x); |
| 673 |
aroundRect.y = Math.round(aroundRect.y); |
| 674 |
aroundRect.width = Math.ceil(fontWidth); |
| 675 |
aroundRect.height = Math.ceil(fontHeight); |
| 676 |
|
| 677 |
o.aroundRect = aroundRect; |
| 678 |
|
| 679 |
this._connectEvents(o); |
| 680 |
eventSeries[slices.length + slice.index] = o; |
| 681 |
} |
| 682 |
}, this); |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
var esi = 0; |
| 687 |
this._eventSeries[this.run.name] = df.map(run, function(v){ |
| 688 |
return v <= 0 ? null : eventSeries[esi++]; |
| 689 |
}); |
| 690 |
|
| 691 |
|
| 692 |
if(has("dojo-bidi")){ |
| 693 |
this._checkOrientation(this.group, dim, offsets); |
| 694 |
} |
| 695 |
|
| 696 |
return this; |
| 697 |
}, |
| 698 |
|
| 699 |
_getProperLabelRadius: function(slices, labelHeight, minRadius){ |
| 700 |
if(slices.length == 1){ |
| 701 |
slices[0].labelR = minRadius; |
| 702 |
return; |
| 703 |
} |
| 704 |
var leftCenterSlice = {}, rightCenterSlice = {}, leftMinSIN = 2, rightMinSIN = 2, i; |
| 705 |
var tempSIN; |
| 706 |
for(i = 0; i < slices.length; ++i){ |
| 707 |
tempSIN = Math.abs(Math.sin(slices[i].angle)); |
| 708 |
if(slices[i].left){ |
| 709 |
if(leftMinSIN > tempSIN){ |
| 710 |
leftMinSIN = tempSIN; |
| 711 |
leftCenterSlice = slices[i]; |
| 712 |
} |
| 713 |
}else{ |
| 714 |
if(rightMinSIN > tempSIN){ |
| 715 |
rightMinSIN = tempSIN; |
| 716 |
rightCenterSlice = slices[i]; |
| 717 |
} |
| 718 |
} |
| 719 |
} |
| 720 |
leftCenterSlice.labelR = rightCenterSlice.labelR = minRadius; |
| 721 |
this._caculateLabelR(leftCenterSlice, slices, labelHeight); |
| 722 |
this._caculateLabelR(rightCenterSlice, slices, labelHeight); |
| 723 |
}, |
| 724 |
|
| 725 |
_caculateLabelR: function(firstSlice, slices, labelHeight){ |
| 726 |
var i, j, k, length = slices.length, currentLabelR = firstSlice.labelR, nextLabelR, |
| 727 |
step = slices[firstSlice.index].left ? -labelHeight : labelHeight; |
| 728 |
for(k = 0, i = firstSlice.index, j = (i + 1) % length; k < length && slices[i].left === slices[j].left; ++k){ |
| 729 |
nextLabelR = (Math.sin(slices[i].angle) * currentLabelR + step) / Math.sin(slices[j].angle); |
| 730 |
currentLabelR = Math.max(firstSlice.labelR, nextLabelR); |
| 731 |
slices[j].labelR = currentLabelR; |
| 732 |
i = (i + 1) % length; |
| 733 |
j = (j + 1) % length; |
| 734 |
} |
| 735 |
if(k >= length){ |
| 736 |
slices[0].labelR = firstSlice.labelR; |
| 737 |
} |
| 738 |
for(k = 0, i = firstSlice.index, j = (i || length) - 1; k < length && slices[i].left === slices[j].left; ++k){ |
| 739 |
nextLabelR = (Math.sin(slices[i].angle) * currentLabelR - step) / Math.sin(slices[j].angle); |
| 740 |
currentLabelR = Math.max(firstSlice.labelR, nextLabelR); |
| 741 |
slices[j].labelR = currentLabelR; |
| 742 |
i = (i || length) - 1; |
| 743 |
j = (j || length) - 1; |
| 744 |
} |
| 745 |
}, |
| 746 |
|
| 747 |
_createRing: function(group, circle){ |
| 748 |
var r = this.opt.innerRadius; |
| 749 |
if(r > 0){ |
| 750 |
|
| 751 |
|
| 752 |
|
| 753 |
r = circle.r * (r/100); |
| 754 |
}else if(r < 0){ |
| 755 |
r = -r; |
| 756 |
} |
| 757 |
if(r){ |
| 758 |
return group.createPath({}).setAbsoluteMode(true). |
| 759 |
moveTo(circle.cx, circle.cy - circle.r). |
| 760 |
arcTo(circle.r, circle.r, 0, false, true, circle.cx + circle.r, circle.cy). |
| 761 |
arcTo(circle.r, circle.r, 0, true, true, circle.cx, circle.cy - circle.r). |
| 762 |
closePath(). |
| 763 |
moveTo(circle.cx, circle.cy - r). |
| 764 |
arcTo(r, r, 0, false, true, circle.cx + r, circle.cy). |
| 765 |
arcTo(r, r, 0, true, true, circle.cx, circle.cy - r). |
| 766 |
closePath(); |
| 767 |
} |
| 768 |
return group.createCircle(circle); |
| 769 |
}, |
| 770 |
_createSlice: function(group, circle, R, x1, y1, x2, y2, fromAngle, stepAngle){ |
| 771 |
var r = this.opt.innerRadius; |
| 772 |
if(r > 0){ |
| 773 |
|
| 774 |
|
| 775 |
|
| 776 |
r = circle.r * (r/100); |
| 777 |
}else if(r < 0){ |
| 778 |
r = -r; |
| 779 |
} |
| 780 |
if(r){ |
| 781 |
var innerX1 = circle.cx + r * Math.cos(fromAngle), |
| 782 |
innerY1 = circle.cy + r * Math.sin(fromAngle), |
| 783 |
innerX2 = circle.cx + r * Math.cos(fromAngle + stepAngle), |
| 784 |
innerY2 = circle.cy + r * Math.sin(fromAngle + stepAngle); |
| 785 |
return group.createPath({}).setAbsoluteMode(true). |
| 786 |
moveTo(innerX1, innerY1). |
| 787 |
lineTo(x1, y1). |
| 788 |
arcTo(R, R, 0, stepAngle > Math.PI, true, x2, y2). |
| 789 |
lineTo(innerX2, innerY2). |
| 790 |
arcTo(r, r, 0, stepAngle > Math.PI, false, innerX1, innerY1). |
| 791 |
closePath(); |
| 792 |
} |
| 793 |
return group.createPath({}).setAbsoluteMode(true). |
| 794 |
moveTo(circle.cx, circle.cy). |
| 795 |
lineTo(x1, y1). |
| 796 |
arcTo(R, R, 0, stepAngle > Math.PI, true, x2, y2). |
| 797 |
lineTo(circle.cx, circle.cy). |
| 798 |
closePath(); |
| 799 |
} |
| 800 |
}); |
| 801 |
}); |