Pie.html 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>The source code</title>
  6. <link href="../resources/prettify/prettify.css" type="text/css" rel="stylesheet" />
  7. <script type="text/javascript" src="../resources/prettify/prettify.js"></script>
  8. <style type="text/css">
  9. .highlight { display: block; background-color: #ddd; }
  10. </style>
  11. <script type="text/javascript">
  12. function highlight() {
  13. document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
  14. }
  15. </script>
  16. </head>
  17. <body onload="prettyPrint(); highlight();">
  18. <pre class="prettyprint lang-js"><span id='Ext-chart-series-Pie'>/**
  19. </span> * @class Ext.chart.series.Pie
  20. *
  21. * Creates a Pie Chart. A Pie Chart is a useful visualization technique to display quantitative information for different
  22. * categories that also have a meaning as a whole.
  23. * As with all other series, the Pie Series must be appended in the *series* Chart array configuration. See the Chart
  24. * documentation for more information. A typical configuration object for the pie series could be:
  25. *
  26. * @example
  27. * var store = Ext.create('Ext.data.JsonStore', {
  28. * fields: ['name', 'data'],
  29. * data: [
  30. * { 'name': 'metric one', 'data': 10 },
  31. * { 'name': 'metric two', 'data': 7 },
  32. * { 'name': 'metric three', 'data': 5 },
  33. * { 'name': 'metric four', 'data': 2 },
  34. * { 'name': 'metric five', 'data': 27 }
  35. * ]
  36. * });
  37. *
  38. * Ext.create('Ext.chart.Chart', {
  39. * renderTo: Ext.getBody(),
  40. * width: 500,
  41. * height: 350,
  42. * animate: true,
  43. * store: store,
  44. * theme: 'Base:gradients',
  45. * series: [{
  46. * type: 'pie',
  47. * angleField: 'data',
  48. * showInLegend: true,
  49. * tips: {
  50. * trackMouse: true,
  51. * width: 140,
  52. * height: 28,
  53. * renderer: function(storeItem, item) {
  54. * // calculate and display percentage on hover
  55. * var total = 0;
  56. * store.each(function(rec) {
  57. * total += rec.get('data');
  58. * });
  59. * this.setTitle(storeItem.get('name') + ': ' + Math.round(storeItem.get('data') / total * 100) + '%');
  60. * }
  61. * },
  62. * highlight: {
  63. * segment: {
  64. * margin: 20
  65. * }
  66. * },
  67. * label: {
  68. * field: 'name',
  69. * display: 'rotate',
  70. * contrast: true,
  71. * font: '18px Arial'
  72. * }
  73. * }]
  74. * });
  75. *
  76. * In this configuration we set `pie` as the type for the series, set an object with specific style properties for highlighting options
  77. * (triggered when hovering elements). We also set true to `showInLegend` so all the pie slices can be represented by a legend item.
  78. *
  79. * We set `data` as the value of the field to determine the angle span for each pie slice. We also set a label configuration object
  80. * where we set the field name of the store field to be renderer as text for the label. The labels will also be displayed rotated.
  81. *
  82. * We set `contrast` to `true` to flip the color of the label if it is to similar to the background color. Finally, we set the font family
  83. * and size through the `font` parameter.
  84. *
  85. * @xtype pie
  86. */
  87. Ext.define('Ext.chart.series.Pie', {
  88. /* Begin Definitions */
  89. alternateClassName: ['Ext.chart.PieSeries', 'Ext.chart.PieChart'],
  90. extend: 'Ext.chart.series.Series',
  91. /* End Definitions */
  92. type: &quot;pie&quot;,
  93. alias: 'series.pie',
  94. accuracy: 100000,
  95. rad: Math.PI * 2 / 100000,
  96. <span id='Ext-chart-series-Pie-cfg-highlightDuration'> /**
  97. </span> * @cfg {Number} highlightDuration
  98. * The duration for the pie slice highlight effect.
  99. */
  100. highlightDuration: 150,
  101. <span id='Ext-chart-series-Pie-cfg-angleField'> /**
  102. </span> * @cfg {String} angleField (required)
  103. * The store record field name to be used for the pie angles.
  104. * The values bound to this field name must be positive real numbers.
  105. */
  106. angleField: false,
  107. <span id='Ext-chart-series-Pie-cfg-field'> /**
  108. </span> * @cfg {String} field
  109. * Alias for {@link #angleField}.
  110. */
  111. <span id='Ext-chart-series-Pie-cfg-xField'> /**
  112. </span> * @cfg {String} xField
  113. * Alias for {@link #angleField}.
  114. */
  115. <span id='Ext-chart-series-Pie-cfg-lengthField'> /**
  116. </span> * @cfg {String} lengthField
  117. * The store record field name to be used for the pie slice lengths.
  118. * The values bound to this field name must be positive real numbers.
  119. */
  120. lengthField: false,
  121. <span id='Ext-chart-series-Pie-cfg-donut'> /**
  122. </span> * @cfg {Boolean/Number} donut
  123. * Whether to set the pie chart as donut chart.
  124. * Default's false. Can be set to a particular percentage to set the radius
  125. * of the donut chart.
  126. */
  127. donut: false,
  128. <span id='Ext-chart-series-Pie-cfg-showInLegend'> /**
  129. </span> * @cfg {Boolean} showInLegend
  130. * Whether to add the pie chart elements as legend items. Default's false.
  131. */
  132. showInLegend: false,
  133. <span id='Ext-chart-series-Pie-cfg-colorSet'> /**
  134. </span> * @cfg {Array} colorSet
  135. * An array of color values which will be used, in order, as the pie slice fill colors.
  136. */
  137. <span id='Ext-chart-series-Pie-cfg-style'> /**
  138. </span> * @cfg {Object} style
  139. * An object containing styles for overriding series styles from Theming.
  140. */
  141. style: {},
  142. constructor: function(config) {
  143. this.callParent(arguments);
  144. var me = this,
  145. chart = me.chart,
  146. surface = chart.surface,
  147. store = chart.store,
  148. shadow = chart.shadow, i, l, cfg;
  149. config.highlightCfg = Ext.merge({
  150. segment: {
  151. margin: 20
  152. }
  153. }, config.highlightCfg);
  154. Ext.apply(me, config, {
  155. shadowAttributes: [{
  156. &quot;stroke-width&quot;: 6,
  157. &quot;stroke-opacity&quot;: 1,
  158. stroke: 'rgb(200, 200, 200)',
  159. translate: {
  160. x: 1.2,
  161. y: 2
  162. }
  163. },
  164. {
  165. &quot;stroke-width&quot;: 4,
  166. &quot;stroke-opacity&quot;: 1,
  167. stroke: 'rgb(150, 150, 150)',
  168. translate: {
  169. x: 0.9,
  170. y: 1.5
  171. }
  172. },
  173. {
  174. &quot;stroke-width&quot;: 2,
  175. &quot;stroke-opacity&quot;: 1,
  176. stroke: 'rgb(100, 100, 100)',
  177. translate: {
  178. x: 0.6,
  179. y: 1
  180. }
  181. }]
  182. });
  183. me.group = surface.getGroup(me.seriesId);
  184. if (shadow) {
  185. for (i = 0, l = me.shadowAttributes.length; i &lt; l; i++) {
  186. me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
  187. }
  188. }
  189. surface.customAttributes.segment = function(opt) {
  190. //Browsers will complain if we create a path
  191. //element that has no path commands. So ensure a dummy
  192. //path command for an empty path.
  193. var ans = me.getSegment(opt);
  194. if (!ans.path || ans.path.length === 0) {
  195. ans.path = ['M', 0, 0];
  196. }
  197. return ans;
  198. };
  199. me.__excludes = me.__excludes || [];
  200. },
  201. // @private updates some onbefore render parameters.
  202. initialize: function() {
  203. var me = this,
  204. store = me.chart.getChartStore(),
  205. data = store.data.items,
  206. i, ln, rec;
  207. //Add yFields to be used in Legend.js
  208. me.yField = [];
  209. if (me.label.field) {
  210. for (i = 0, ln = data.length; i &lt; ln; i++) {
  211. rec = data[i];
  212. me.yField.push(rec.get(me.label.field));
  213. }
  214. }
  215. },
  216. // @private returns an object with properties for a PieSlice.
  217. getSegment: function(opt) {
  218. var me = this,
  219. rad = me.rad,
  220. cos = Math.cos,
  221. sin = Math.sin,
  222. x = me.centerX,
  223. y = me.centerY,
  224. x1 = 0, x2 = 0, x3 = 0, x4 = 0,
  225. y1 = 0, y2 = 0, y3 = 0, y4 = 0,
  226. x5 = 0, y5 = 0, x6 = 0, y6 = 0,
  227. delta = 1e-2,
  228. startAngle = opt.startAngle,
  229. endAngle = opt.endAngle,
  230. midAngle = (startAngle + endAngle) / 2 * rad,
  231. margin = opt.margin || 0,
  232. a1 = Math.min(startAngle, endAngle) * rad,
  233. a2 = Math.max(startAngle, endAngle) * rad,
  234. c1 = cos(a1), s1 = sin(a1),
  235. c2 = cos(a2), s2 = sin(a2),
  236. cm = cos(midAngle), sm = sin(midAngle),
  237. flag = 0, hsqr2 = 0.7071067811865476; // sqrt(0.5)
  238. if (a2 - a1 &lt; delta) {
  239. return {path: &quot;&quot;};
  240. }
  241. if (margin !== 0) {
  242. x += margin * cm;
  243. y += margin * sm;
  244. }
  245. x2 = x + opt.endRho * c1;
  246. y2 = y + opt.endRho * s1;
  247. x4 = x + opt.endRho * c2;
  248. y4 = y + opt.endRho * s2;
  249. x6 = x + opt.endRho * cm;
  250. y6 = y + opt.endRho * sm;
  251. if (opt.startRho !== 0) {
  252. x1 = x + opt.startRho * c1;
  253. y1 = y + opt.startRho * s1;
  254. x3 = x + opt.startRho * c2;
  255. y3 = y + opt.startRho * s2;
  256. x5 = x + opt.startRho * cm;
  257. y5 = y + opt.startRho * sm;
  258. return {
  259. path: [
  260. [&quot;M&quot;, x2, y2],
  261. [&quot;A&quot;, opt.endRho, opt.endRho, 0, 0, 1, x6, y6], [&quot;L&quot;, x6, y6],
  262. [&quot;A&quot;, opt.endRho, opt.endRho, 0, flag, 1, x4, y4], [&quot;L&quot;, x4, y4],
  263. [&quot;L&quot;, x3, y3],
  264. [&quot;A&quot;, opt.startRho, opt.startRho, 0, flag, 0, x5, y5], [&quot;L&quot;, x5, y5],
  265. [&quot;A&quot;, opt.startRho, opt.startRho, 0, 0, 0, x1, y1], [&quot;L&quot;, x1, y1],
  266. [&quot;Z&quot;]
  267. ]
  268. };
  269. } else {
  270. return {
  271. path: [
  272. [&quot;M&quot;, x, y],
  273. [&quot;L&quot;, x2, y2],
  274. [&quot;A&quot;, opt.endRho, opt.endRho, 0, 0, 1, x6, y6], [&quot;L&quot;, x6, y6],
  275. [&quot;A&quot;, opt.endRho, opt.endRho, 0, flag, 1, x4, y4], [&quot;L&quot;, x4, y4],
  276. [&quot;L&quot;, x, y],
  277. [&quot;Z&quot;]
  278. ]
  279. };
  280. }
  281. },
  282. // @private utility function to calculate the middle point of a pie slice.
  283. calcMiddle: function(item) {
  284. var me = this,
  285. rad = me.rad,
  286. slice = item.slice,
  287. x = me.centerX,
  288. y = me.centerY,
  289. startAngle = slice.startAngle,
  290. endAngle = slice.endAngle,
  291. donut = +me.donut,
  292. midAngle = -(startAngle + endAngle) * rad / 2,
  293. r = (item.endRho + item.startRho) / 2,
  294. xm = x + r * Math.cos(midAngle),
  295. ym = y - r * Math.sin(midAngle);
  296. item.middle = {
  297. x: xm,
  298. y: ym
  299. };
  300. },
  301. <span id='Ext-chart-series-Pie-method-drawSeries'> /**
  302. </span> * Draws the series for the current chart.
  303. */
  304. drawSeries: function() {
  305. var me = this,
  306. store = me.chart.getChartStore(),
  307. data = store.data.items,
  308. record,
  309. group = me.group,
  310. animate = me.chart.animate,
  311. field = me.angleField || me.field || me.xField,
  312. lenField = [].concat(me.lengthField),
  313. totalLenField = 0,
  314. chart = me.chart,
  315. surface = chart.surface,
  316. chartBBox = chart.chartBBox,
  317. enableShadows = chart.shadow,
  318. shadowGroups = me.shadowGroups,
  319. shadowAttributes = me.shadowAttributes,
  320. lnsh = shadowGroups.length,
  321. layers = lenField.length,
  322. rhoAcum = 0,
  323. donut = +me.donut,
  324. layerTotals = [],
  325. items = [],
  326. totalField = 0,
  327. maxLenField = 0,
  328. angle = 0,
  329. seriesStyle = me.seriesStyle,
  330. colorArrayStyle = me.colorArrayStyle,
  331. colorArrayLength = colorArrayStyle &amp;&amp; colorArrayStyle.length || 0,
  332. rendererAttributes,
  333. shadowAttr,
  334. shadows,
  335. shadow,
  336. shindex,
  337. centerX,
  338. centerY,
  339. deltaRho,
  340. first = 0,
  341. slice,
  342. slices,
  343. sprite,
  344. value,
  345. item,
  346. lenValue,
  347. ln,
  348. i,
  349. j,
  350. endAngle,
  351. path,
  352. p,
  353. spriteOptions, bbox;
  354. Ext.apply(seriesStyle, me.style || {});
  355. me.setBBox();
  356. bbox = me.bbox;
  357. //override theme colors
  358. if (me.colorSet) {
  359. colorArrayStyle = me.colorSet;
  360. colorArrayLength = colorArrayStyle.length;
  361. }
  362. //if not store or store is empty then there's nothing to draw
  363. if (!store || !store.getCount() || me.seriesIsHidden) {
  364. me.hide();
  365. me.items = [];
  366. return;
  367. }
  368. me.unHighlightItem();
  369. me.cleanHighlights();
  370. centerX = me.centerX = chartBBox.x + (chartBBox.width / 2);
  371. centerY = me.centerY = chartBBox.y + (chartBBox.height / 2);
  372. me.radius = Math.min(centerX - chartBBox.x, centerY - chartBBox.y);
  373. me.slices = slices = [];
  374. me.items = items = [];
  375. for (i = 0, ln = data.length; i &lt; ln; i++) {
  376. record = data[i];
  377. if (this.__excludes &amp;&amp; this.__excludes[i]) {
  378. //hidden series
  379. continue;
  380. }
  381. totalField += +record.get(field);
  382. if (lenField[0]) {
  383. for (j = 0, totalLenField = 0; j &lt; layers; j++) {
  384. totalLenField += +record.get(lenField[j]);
  385. }
  386. layerTotals[i] = totalLenField;
  387. maxLenField = Math.max(maxLenField, totalLenField);
  388. }
  389. }
  390. totalField = totalField || 1;
  391. for (i = 0, ln = data.length; i &lt; ln; i++) {
  392. record = data[i];
  393. if (this.__excludes &amp;&amp; this.__excludes[i]) {
  394. value = 0;
  395. } else {
  396. value = record.get(field);
  397. if (first == 0) {
  398. first = 1;
  399. }
  400. }
  401. // First slice
  402. if (first == 1) {
  403. first = 2;
  404. me.firstAngle = angle = me.accuracy * value / totalField / 2;
  405. for (j = 0; j &lt; i; j++) {
  406. slices[j].startAngle = slices[j].endAngle = me.firstAngle;
  407. }
  408. }
  409. endAngle = angle - me.accuracy * value / totalField;
  410. slice = {
  411. series: me,
  412. value: value,
  413. startAngle: angle,
  414. endAngle: endAngle,
  415. storeItem: record
  416. };
  417. if (lenField[0]) {
  418. lenValue = +layerTotals[i];
  419. //removing the floor will break Opera 11.6*
  420. slice.rho = Math.floor(me.radius / maxLenField * lenValue);
  421. } else {
  422. slice.rho = me.radius;
  423. }
  424. slices[i] = slice;
  425. // Do not remove this closure for the sake of https://sencha.jira.com/browse/EXTJSIV-5836
  426. (function () {
  427. angle = endAngle;
  428. })();
  429. }
  430. //do all shadows first.
  431. if (enableShadows) {
  432. for (i = 0, ln = slices.length; i &lt; ln; i++) {
  433. slice = slices[i];
  434. slice.shadowAttrs = [];
  435. for (j = 0, rhoAcum = 0, shadows = []; j &lt; layers; j++) {
  436. sprite = group.getAt(i * layers + j);
  437. deltaRho = lenField[j] ? store.getAt(i).get(lenField[j]) / layerTotals[i] * slice.rho: slice.rho;
  438. //set pie slice properties
  439. rendererAttributes = {
  440. segment: {
  441. startAngle: slice.startAngle,
  442. endAngle: slice.endAngle,
  443. margin: 0,
  444. rho: slice.rho,
  445. startRho: rhoAcum + (deltaRho * donut / 100),
  446. endRho: rhoAcum + deltaRho
  447. },
  448. hidden: !slice.value &amp;&amp; (slice.startAngle % me.accuracy) == (slice.endAngle % me.accuracy)
  449. };
  450. //create shadows
  451. for (shindex = 0, shadows = []; shindex &lt; lnsh; shindex++) {
  452. shadowAttr = shadowAttributes[shindex];
  453. shadow = shadowGroups[shindex].getAt(i);
  454. if (!shadow) {
  455. shadow = chart.surface.add(Ext.apply({}, {
  456. type: 'path',
  457. group: shadowGroups[shindex],
  458. strokeLinejoin: &quot;round&quot;
  459. }, rendererAttributes, shadowAttr));
  460. }
  461. shadowAttr = me.renderer(shadow, store.getAt(i), Ext.apply({}, rendererAttributes, shadowAttr), i, store);
  462. if (animate) {
  463. me.onAnimate(shadow, {
  464. to: shadowAttr
  465. });
  466. } else {
  467. shadow.setAttributes(shadowAttr, true);
  468. }
  469. shadows.push(shadow);
  470. }
  471. slice.shadowAttrs[j] = shadows;
  472. }
  473. }
  474. }
  475. //do pie slices after.
  476. for (i = 0, ln = slices.length; i &lt; ln; i++) {
  477. slice = slices[i];
  478. for (j = 0, rhoAcum = 0; j &lt; layers; j++) {
  479. sprite = group.getAt(i * layers + j);
  480. deltaRho = lenField[j] ? store.getAt(i).get(lenField[j]) / layerTotals[i] * slice.rho: slice.rho;
  481. //set pie slice properties
  482. rendererAttributes = Ext.apply({
  483. segment: {
  484. startAngle: slice.startAngle,
  485. endAngle: slice.endAngle,
  486. margin: 0,
  487. rho: slice.rho,
  488. startRho: rhoAcum + (deltaRho * donut / 100),
  489. endRho: rhoAcum + deltaRho
  490. },
  491. hidden: (!slice.value &amp;&amp; (slice.startAngle % me.accuracy) == (slice.endAngle % me.accuracy))
  492. }, Ext.apply(seriesStyle, colorArrayStyle &amp;&amp; { fill: colorArrayStyle[(layers &gt; 1? j : i) % colorArrayLength] } || {}));
  493. item = Ext.apply({},
  494. rendererAttributes.segment, {
  495. slice: slice,
  496. series: me,
  497. storeItem: slice.storeItem,
  498. index: i
  499. });
  500. me.calcMiddle(item);
  501. if (enableShadows) {
  502. item.shadows = slice.shadowAttrs[j];
  503. }
  504. items[i] = item;
  505. // Create a new sprite if needed (no height)
  506. if (!sprite) {
  507. spriteOptions = Ext.apply({
  508. type: &quot;path&quot;,
  509. group: group,
  510. middle: item.middle
  511. }, Ext.apply(seriesStyle, colorArrayStyle &amp;&amp; { fill: colorArrayStyle[(layers &gt; 1? j : i) % colorArrayLength] } || {}));
  512. sprite = surface.add(Ext.apply(spriteOptions, rendererAttributes));
  513. }
  514. slice.sprite = slice.sprite || [];
  515. item.sprite = sprite;
  516. slice.sprite.push(sprite);
  517. slice.point = [item.middle.x, item.middle.y];
  518. if (animate) {
  519. rendererAttributes = me.renderer(sprite, store.getAt(i), rendererAttributes, i, store);
  520. sprite._to = rendererAttributes;
  521. sprite._animating = true;
  522. me.onAnimate(sprite, {
  523. to: rendererAttributes,
  524. listeners: {
  525. afteranimate: {
  526. fn: function() {
  527. this._animating = false;
  528. },
  529. scope: sprite
  530. }
  531. }
  532. });
  533. } else {
  534. rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply(rendererAttributes, {
  535. hidden: false
  536. }), i, store);
  537. sprite.setAttributes(rendererAttributes, true);
  538. }
  539. rhoAcum += deltaRho;
  540. }
  541. }
  542. // Hide unused bars
  543. ln = group.getCount();
  544. for (i = 0; i &lt; ln; i++) {
  545. if (!slices[(i / layers) &gt;&gt; 0] &amp;&amp; group.getAt(i)) {
  546. group.getAt(i).hide(true);
  547. }
  548. }
  549. if (enableShadows) {
  550. lnsh = shadowGroups.length;
  551. for (shindex = 0; shindex &lt; ln; shindex++) {
  552. if (!slices[(shindex / layers) &gt;&gt; 0]) {
  553. for (j = 0; j &lt; lnsh; j++) {
  554. if (shadowGroups[j].getAt(shindex)) {
  555. shadowGroups[j].getAt(shindex).hide(true);
  556. }
  557. }
  558. }
  559. }
  560. }
  561. me.renderLabels();
  562. me.renderCallouts();
  563. },
  564. // @private callback for when creating a label sprite.
  565. onCreateLabel: function(storeItem, item, i, display) {
  566. var me = this,
  567. group = me.labelsGroup,
  568. config = me.label,
  569. centerX = me.centerX,
  570. centerY = me.centerY,
  571. middle = item.middle,
  572. endLabelStyle = Ext.apply(me.seriesLabelStyle || {}, config || {});
  573. return me.chart.surface.add(Ext.apply({
  574. 'type': 'text',
  575. 'text-anchor': 'middle',
  576. 'group': group,
  577. 'x': middle.x,
  578. 'y': middle.y
  579. }, endLabelStyle));
  580. },
  581. // @private callback for when placing a label sprite.
  582. onPlaceLabel: function(label, storeItem, item, i, display, animate, index) {
  583. var me = this,
  584. chart = me.chart,
  585. resizing = chart.resizing,
  586. config = me.label,
  587. format = config.renderer,
  588. field = [].concat(config.field),
  589. centerX = me.centerX,
  590. centerY = me.centerY,
  591. middle = item.middle,
  592. opt = {
  593. x: middle.x,
  594. y: middle.y
  595. },
  596. x = middle.x - centerX,
  597. y = middle.y - centerY,
  598. from = {},
  599. rho = 1,
  600. theta = Math.atan2(y, x || 1),
  601. dg = theta * 180 / Math.PI,
  602. prevDg;
  603. opt.hidden = false;
  604. if (this.__excludes &amp;&amp; this.__excludes[i]) {
  605. opt.hidden = true;
  606. }
  607. function fixAngle(a) {
  608. if (a &lt; 0) {
  609. a += 360;
  610. }
  611. return a % 360;
  612. }
  613. label.setAttributes({
  614. text: format(storeItem.get(field[index]))
  615. }, true);
  616. switch (display) {
  617. case 'outside':
  618. rho = Math.sqrt(x * x + y * y) * 2;
  619. //update positions
  620. opt.x = rho * Math.cos(theta) + centerX;
  621. opt.y = rho * Math.sin(theta) + centerY;
  622. break;
  623. case 'rotate':
  624. dg = fixAngle(dg);
  625. dg = (dg &gt; 90 &amp;&amp; dg &lt; 270) ? dg + 180: dg;
  626. prevDg = label.attr.rotation.degrees;
  627. if (prevDg != null &amp;&amp; Math.abs(prevDg - dg) &gt; 180 * 0.5) {
  628. if (dg &gt; prevDg) {
  629. dg -= 360;
  630. } else {
  631. dg += 360;
  632. }
  633. dg = dg % 360;
  634. } else {
  635. dg = fixAngle(dg);
  636. }
  637. //update rotation angle
  638. opt.rotate = {
  639. degrees: dg,
  640. x: opt.x,
  641. y: opt.y
  642. };
  643. break;
  644. default:
  645. break;
  646. }
  647. //ensure the object has zero translation
  648. opt.translate = {
  649. x: 0, y: 0
  650. };
  651. if (animate &amp;&amp; !resizing &amp;&amp; (display != 'rotate' || prevDg != null)) {
  652. me.onAnimate(label, {
  653. to: opt
  654. });
  655. } else {
  656. label.setAttributes(opt, true);
  657. }
  658. label._from = from;
  659. },
  660. // @private callback for when placing a callout sprite.
  661. onPlaceCallout: function(callout, storeItem, item, i, display, animate, index) {
  662. var me = this,
  663. chart = me.chart,
  664. centerX = me.centerX,
  665. centerY = me.centerY,
  666. middle = item.middle,
  667. opt = {
  668. x: middle.x,
  669. y: middle.y
  670. },
  671. x = middle.x - centerX,
  672. y = middle.y - centerY,
  673. rho = 1,
  674. rhoCenter,
  675. theta = Math.atan2(y, x || 1),
  676. bbox = callout.label.getBBox(),
  677. offsetFromViz = 20,
  678. offsetToSide = 10,
  679. offsetBox = 10,
  680. p;
  681. //should be able to config this.
  682. rho = item.endRho + offsetFromViz;
  683. rhoCenter = (item.endRho + item.startRho) / 2 + (item.endRho - item.startRho) / 3;
  684. //update positions
  685. opt.x = rho * Math.cos(theta) + centerX;
  686. opt.y = rho * Math.sin(theta) + centerY;
  687. x = rhoCenter * Math.cos(theta);
  688. y = rhoCenter * Math.sin(theta);
  689. if (chart.animate) {
  690. //set the line from the middle of the pie to the box.
  691. me.onAnimate(callout.lines, {
  692. to: {
  693. path: [&quot;M&quot;, x + centerX, y + centerY, &quot;L&quot;, opt.x, opt.y, &quot;Z&quot;, &quot;M&quot;, opt.x, opt.y, &quot;l&quot;, x &gt; 0 ? offsetToSide: -offsetToSide, 0, &quot;z&quot;]
  694. }
  695. });
  696. //set box position
  697. me.onAnimate(callout.box, {
  698. to: {
  699. x: opt.x + (x &gt; 0 ? offsetToSide: -(offsetToSide + bbox.width + 2 * offsetBox)),
  700. y: opt.y + (y &gt; 0 ? ( - bbox.height - offsetBox / 2) : ( - bbox.height - offsetBox / 2)),
  701. width: bbox.width + 2 * offsetBox,
  702. height: bbox.height + 2 * offsetBox
  703. }
  704. });
  705. //set text position
  706. me.onAnimate(callout.label, {
  707. to: {
  708. x: opt.x + (x &gt; 0 ? (offsetToSide + offsetBox) : -(offsetToSide + bbox.width + offsetBox)),
  709. y: opt.y + (y &gt; 0 ? -bbox.height / 4: -bbox.height / 4)
  710. }
  711. });
  712. } else {
  713. //set the line from the middle of the pie to the box.
  714. callout.lines.setAttributes({
  715. path: [&quot;M&quot;, x + centerX, y + centerY, &quot;L&quot;, opt.x, opt.y, &quot;Z&quot;, &quot;M&quot;, opt.x, opt.y, &quot;l&quot;, x &gt; 0 ? offsetToSide: -offsetToSide, 0, &quot;z&quot;]
  716. },
  717. true);
  718. //set box position
  719. callout.box.setAttributes({
  720. x: opt.x + (x &gt; 0 ? offsetToSide: -(offsetToSide + bbox.width + 2 * offsetBox)),
  721. y: opt.y + (y &gt; 0 ? ( - bbox.height - offsetBox / 2) : ( - bbox.height - offsetBox / 2)),
  722. width: bbox.width + 2 * offsetBox,
  723. height: bbox.height + 2 * offsetBox
  724. },
  725. true);
  726. //set text position
  727. callout.label.setAttributes({
  728. x: opt.x + (x &gt; 0 ? (offsetToSide + offsetBox) : -(offsetToSide + bbox.width + offsetBox)),
  729. y: opt.y + (y &gt; 0 ? -bbox.height / 4: -bbox.height / 4)
  730. },
  731. true);
  732. }
  733. for (p in callout) {
  734. callout[p].show(true);
  735. }
  736. },
  737. // @private handles sprite animation for the series.
  738. onAnimate: function(sprite, attr) {
  739. sprite.show();
  740. return this.callParent(arguments);
  741. },
  742. isItemInPoint: function(x, y, item, i) {
  743. var me = this,
  744. cx = me.centerX,
  745. cy = me.centerY,
  746. abs = Math.abs,
  747. dx = abs(x - cx),
  748. dy = abs(y - cy),
  749. startAngle = item.startAngle,
  750. endAngle = item.endAngle,
  751. rho = Math.sqrt(dx * dx + dy * dy),
  752. angle = Math.atan2(y - cy, x - cx) / me.rad;
  753. // normalize to the same range of angles created by drawSeries
  754. if (angle &gt; me.firstAngle) {
  755. angle -= me.accuracy;
  756. }
  757. return (angle &lt;= startAngle &amp;&amp; angle &gt; endAngle
  758. &amp;&amp; rho &gt;= item.startRho &amp;&amp; rho &lt;= item.endRho);
  759. },
  760. // @private hides all elements in the series.
  761. hideAll: function(index) {
  762. var i, l, shadow, shadows, sh, lsh, sprite;
  763. index = (isNaN(this._index) ? index : this._index) || 0;
  764. this.__excludes = this.__excludes || [];
  765. this.__excludes[index] = true;
  766. sprite = this.slices[index].sprite;
  767. for (sh = 0, lsh = sprite.length; sh &lt; lsh; sh++) {
  768. sprite[sh].setAttributes({
  769. hidden: true
  770. }, true);
  771. }
  772. if (this.slices[index].shadowAttrs) {
  773. for (i = 0, shadows = this.slices[index].shadowAttrs, l = shadows.length; i &lt; l; i++) {
  774. shadow = shadows[i];
  775. for (sh = 0, lsh = shadow.length; sh &lt; lsh; sh++) {
  776. shadow[sh].setAttributes({
  777. hidden: true
  778. }, true);
  779. }
  780. }
  781. }
  782. this.drawSeries();
  783. },
  784. // @private shows all elements in the series.
  785. showAll: function(index) {
  786. index = (isNaN(this._index) ? index : this._index) || 0;
  787. this.__excludes[index] = false;
  788. this.drawSeries();
  789. },
  790. <span id='Ext-chart-series-Pie-method-highlightItem'> /**
  791. </span> * Highlight the specified item. If no item is provided the whole series will be highlighted.
  792. * @param item {Object} Info about the item; same format as returned by #getItemForPoint
  793. */
  794. highlightItem: function(item) {
  795. var me = this,
  796. rad = me.rad,
  797. highlightSegment,
  798. animate,
  799. attrs,
  800. i,
  801. shadows,
  802. shadow,
  803. ln,
  804. to,
  805. itemHighlightSegment,
  806. prop,
  807. group,
  808. display,
  809. label,
  810. middle,
  811. r,
  812. x,
  813. y;
  814. item = item || this.items[this._index];
  815. //TODO(nico): sometimes in IE itemmouseover is triggered
  816. //twice without triggering itemmouseout in between. This
  817. //fixes the highlighting bug. Eventually, events should be
  818. //changed to trigger one itemmouseout between two itemmouseovers.
  819. this.unHighlightItem();
  820. if (!item || me.animating || (item.sprite &amp;&amp; item.sprite._animating)) {
  821. return;
  822. }
  823. me.callParent([item]);
  824. if (!me.highlight) {
  825. return;
  826. }
  827. if ('segment' in me.highlightCfg) {
  828. highlightSegment = me.highlightCfg.segment;
  829. animate = me.chart.animate;
  830. //animate labels
  831. if (me.labelsGroup) {
  832. group = me.labelsGroup;
  833. display = me.label.display;
  834. label = group.getAt(item.index);
  835. middle = (item.startAngle + item.endAngle) / 2 * rad;
  836. r = highlightSegment.margin || 0;
  837. x = r * Math.cos(middle);
  838. y = r * Math.sin(middle);
  839. //TODO(nico): rounding to 1e-10
  840. //gives the right translation. Translation
  841. //was buggy for very small numbers. In this
  842. //case we're not looking to translate to very small
  843. //numbers but not to translate at all.
  844. if (Math.abs(x) &lt; 1e-10) {
  845. x = 0;
  846. }
  847. if (Math.abs(y) &lt; 1e-10) {
  848. y = 0;
  849. }
  850. if (animate) {
  851. label.stopAnimation();
  852. label.animate({
  853. to: {
  854. translate: {
  855. x: x,
  856. y: y
  857. }
  858. },
  859. duration: me.highlightDuration
  860. });
  861. }
  862. else {
  863. label.setAttributes({
  864. translate: {
  865. x: x,
  866. y: y
  867. }
  868. }, true);
  869. }
  870. }
  871. //animate shadows
  872. if (me.chart.shadow &amp;&amp; item.shadows) {
  873. i = 0;
  874. shadows = item.shadows;
  875. ln = shadows.length;
  876. for (; i &lt; ln; i++) {
  877. shadow = shadows[i];
  878. to = {};
  879. itemHighlightSegment = item.sprite._from.segment;
  880. for (prop in itemHighlightSegment) {
  881. if (! (prop in highlightSegment)) {
  882. to[prop] = itemHighlightSegment[prop];
  883. }
  884. }
  885. attrs = {
  886. segment: Ext.applyIf(to, me.highlightCfg.segment)
  887. };
  888. if (animate) {
  889. shadow.stopAnimation();
  890. shadow.animate({
  891. to: attrs,
  892. duration: me.highlightDuration
  893. });
  894. }
  895. else {
  896. shadow.setAttributes(attrs, true);
  897. }
  898. }
  899. }
  900. }
  901. },
  902. <span id='Ext-chart-series-Pie-method-unHighlightItem'> /**
  903. </span> * Un-highlights the specified item. If no item is provided it will un-highlight the entire series.
  904. * @param item {Object} Info about the item; same format as returned by #getItemForPoint
  905. */
  906. unHighlightItem: function() {
  907. var me = this,
  908. items,
  909. animate,
  910. shadowsEnabled,
  911. group,
  912. len,
  913. i,
  914. j,
  915. display,
  916. shadowLen,
  917. p,
  918. to,
  919. ihs,
  920. hs,
  921. sprite,
  922. shadows,
  923. shadow,
  924. item,
  925. label,
  926. attrs;
  927. if (!me.highlight) {
  928. return;
  929. }
  930. if (('segment' in me.highlightCfg) &amp;&amp; me.items) {
  931. items = me.items;
  932. animate = me.chart.animate;
  933. shadowsEnabled = !!me.chart.shadow;
  934. group = me.labelsGroup;
  935. len = items.length;
  936. i = 0;
  937. j = 0;
  938. display = me.label.display;
  939. for (; i &lt; len; i++) {
  940. item = items[i];
  941. if (!item) {
  942. continue;
  943. }
  944. sprite = item.sprite;
  945. if (sprite &amp;&amp; sprite._highlighted) {
  946. //animate labels
  947. if (group) {
  948. label = group.getAt(item.index);
  949. attrs = Ext.apply({
  950. translate: {
  951. x: 0,
  952. y: 0
  953. }
  954. },
  955. display == 'rotate' ? {
  956. rotate: {
  957. x: label.attr.x,
  958. y: label.attr.y,
  959. degrees: label.attr.rotation.degrees
  960. }
  961. }: {});
  962. if (animate) {
  963. label.stopAnimation();
  964. label.animate({
  965. to: attrs,
  966. duration: me.highlightDuration
  967. });
  968. }
  969. else {
  970. label.setAttributes(attrs, true);
  971. }
  972. }
  973. if (shadowsEnabled) {
  974. shadows = item.shadows;
  975. shadowLen = shadows.length;
  976. for (; j &lt; shadowLen; j++) {
  977. to = {};
  978. ihs = item.sprite._to.segment;
  979. hs = item.sprite._from.segment;
  980. Ext.apply(to, hs);
  981. for (p in ihs) {
  982. if (! (p in hs)) {
  983. to[p] = ihs[p];
  984. }
  985. }
  986. shadow = shadows[j];
  987. if (animate) {
  988. shadow.stopAnimation();
  989. shadow.animate({
  990. to: {
  991. segment: to
  992. },
  993. duration: me.highlightDuration
  994. });
  995. }
  996. else {
  997. shadow.setAttributes({ segment: to }, true);
  998. }
  999. }
  1000. }
  1001. }
  1002. }
  1003. }
  1004. me.callParent(arguments);
  1005. },
  1006. <span id='Ext-chart-series-Pie-method-getLegendColor'> /**
  1007. </span> * Returns the color of the series (to be displayed as color for the series legend item).
  1008. * @param item {Object} Info about the item; same format as returned by #getItemForPoint
  1009. */
  1010. getLegendColor: function(index) {
  1011. var me = this;
  1012. return (me.colorSet &amp;&amp; me.colorSet[index % me.colorSet.length]) || me.colorArrayStyle[index % me.colorArrayStyle.length];
  1013. }
  1014. });
  1015. </pre>
  1016. </body>
  1017. </html>