2 * @class Ext.chart.series.Pie
3 * @extends Ext.chart.series.Series
5 * Creates a Pie Chart. A Pie Chart is a useful visualization technique to display quantitative information for different
6 * categories that also have a meaning as a whole.
7 * As with all other series, the Pie Series must be appended in the *series* Chart array configuration. See the Chart
8 * documentation for more information. A typical configuration object for the pie series could be:
10 * {@img Ext.chart.series.Pie/Ext.chart.series.Pie.png Ext.chart.series.Pie chart series}
12 * var store = Ext.create('Ext.data.JsonStore', {
13 * fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
15 * {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
16 * {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
17 * {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
18 * {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
19 * {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
23 * Ext.create('Ext.chart.Chart', {
24 * renderTo: Ext.getBody(),
29 * theme: 'Base:gradients',
38 * renderer: function(storeItem, item) {
39 * //calculate and display percentage on hover
41 * store.each(function(rec) {
42 * total += rec.get('data1');
44 * this.setTitle(storeItem.get('name') + ': ' + Math.round(storeItem.get('data1') / total * 100) + '%');
61 * In this configuration we set `pie` as the type for the series, set an object with specific style properties for highlighting options
62 * (triggered when hovering elements). We also set true to `showInLegend` so all the pie slices can be represented by a legend item.
63 * We set `data1` as the value of the field to determine the angle span for each pie slice. We also set a label configuration object
64 * 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.
65 * 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
66 * and size through the `font` parameter.
70 Ext.define('Ext.chart.series.Pie', {
72 /* Begin Definitions */
74 alternateClassName: ['Ext.chart.PieSeries', 'Ext.chart.PieChart'],
76 extend: 'Ext.chart.series.Series',
87 * @cfg {Number} highlightDuration
88 * The duration for the pie slice highlight effect.
90 highlightDuration: 150,
93 * @cfg {String} angleField
94 * The store record field name to be used for the pie angles.
95 * The values bound to this field name must be positive real numbers.
96 * This parameter is required.
101 * @cfg {String} lengthField
102 * The store record field name to be used for the pie slice lengths.
103 * The values bound to this field name must be positive real numbers.
104 * This parameter is optional.
109 * @cfg {Boolean|Number} donut
110 * Whether to set the pie chart as donut chart.
111 * Default's false. Can be set to a particular percentage to set the radius
112 * of the donut chart.
117 * @cfg {Boolean} showInLegend
118 * Whether to add the pie chart elements as legend items. Default's false.
123 * @cfg {Array} colorSet
124 * An array of color values which will be used, in order, as the pie slice fill colors.
128 * @cfg {Object} style
129 * An object containing styles for overriding series styles from Theming.
133 constructor: function(config) {
134 this.callParent(arguments);
137 surface = chart.surface,
139 shadow = chart.shadow, i, l, cfg;
147 Ext.apply(me, config, {
151 stroke: 'rgb(200, 200, 200)',
160 stroke: 'rgb(150, 150, 150)',
169 stroke: 'rgb(100, 100, 100)',
176 me.group = surface.getGroup(me.seriesId);
178 for (i = 0, l = me.shadowAttributes.length; i < l; i++) {
179 me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
182 surface.customAttributes.segment = function(opt) {
183 return me.getSegment(opt);
187 //@private updates some onbefore render parameters.
188 initialize: function() {
190 store = me.chart.substore || me.chart.store;
191 //Add yFields to be used in Legend.js
193 if (me.label.field) {
194 store.each(function(rec) {
195 me.yField.push(rec.get(me.label.field));
200 // @private returns an object with properties for a PieSlice.
201 getSegment: function(opt) {
209 x1 = 0, x2 = 0, x3 = 0, x4 = 0,
210 y1 = 0, y2 = 0, y3 = 0, y4 = 0,
212 r = opt.endRho - opt.startRho,
213 startAngle = opt.startAngle,
214 endAngle = opt.endAngle,
215 midAngle = (startAngle + endAngle) / 2 * rad,
216 margin = opt.margin || 0,
217 flag = abs(endAngle - startAngle) > 180,
218 a1 = Math.min(startAngle, endAngle) * rad,
219 a2 = Math.max(startAngle, endAngle) * rad,
222 x += margin * cos(midAngle);
223 y += margin * sin(midAngle);
225 x1 = x + opt.startRho * cos(a1);
226 y1 = y + opt.startRho * sin(a1);
228 x2 = x + opt.endRho * cos(a1);
229 y2 = y + opt.endRho * sin(a1);
231 x3 = x + opt.startRho * cos(a2);
232 y3 = y + opt.startRho * sin(a2);
234 x4 = x + opt.endRho * cos(a2);
235 y4 = y + opt.endRho * sin(a2);
237 if (abs(x1 - x3) <= delta && abs(y1 - y3) <= delta) {
240 //Solves mysterious clipping bug with IE
246 ["A", opt.endRho, opt.endRho, 0, +flag, 1, x4, y4],
254 ["A", opt.endRho, opt.endRho, 0, +flag, 1, x4, y4],
256 ["A", opt.startRho, opt.startRho, 0, +flag, 0, x1, y1],
262 // @private utility function to calculate the middle point of a pie slice.
263 calcMiddle: function(item) {
269 startAngle = slice.startAngle,
270 endAngle = slice.endAngle,
272 a1 = Math.min(startAngle, endAngle) * rad,
273 a2 = Math.max(startAngle, endAngle) * rad,
274 midAngle = -(a1 + (a2 - a1) / 2),
275 xm = x + (item.endRho + item.startRho) / 2 * Math.cos(midAngle),
276 ym = y - (item.endRho + item.startRho) / 2 * Math.sin(midAngle);
285 * Draws the series for the current chart.
287 drawSeries: function() {
289 store = me.chart.substore || me.chart.store,
291 animate = me.chart.animate,
292 field = me.angleField || me.field || me.xField,
293 lenField = [].concat(me.lengthField),
295 colors = me.colorSet,
297 surface = chart.surface,
298 chartBBox = chart.chartBBox,
299 enableShadows = chart.shadow,
300 shadowGroups = me.shadowGroups,
301 shadowAttributes = me.shadowAttributes,
302 lnsh = shadowGroups.length,
304 layers = lenField.length,
317 seriesStyle = me.seriesStyle,
318 seriesLabelStyle = me.seriesLabelStyle,
319 colorArrayStyle = me.colorArrayStyle,
320 colorArrayLength = colorArrayStyle && colorArrayStyle.length || 0,
321 gutterX = chart.maxGutter[0],
322 gutterY = chart.maxGutter[1],
351 Ext.apply(seriesStyle, me.style || {});
356 //override theme colors
358 colorArrayStyle = me.colorSet;
359 colorArrayLength = colorArrayStyle.length;
362 //if not store or store is empty then there's nothing to draw
363 if (!store || !store.getCount()) {
367 me.unHighlightItem();
368 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 = [];
376 store.each(function(record, i) {
377 if (this.__excludes && this.__excludes[i]) {
381 totalField += +record.get(field);
383 for (j = 0, totalLenField = 0; j < layers; j++) {
384 totalLenField += +record.get(lenField[j]);
386 layerTotals[i] = totalLenField;
387 maxLenField = Math.max(maxLenField, totalLenField);
391 store.each(function(record, i) {
392 if (this.__excludes && this.__excludes[i]) {
396 value = record.get(field);
397 middleAngle = angle - 360 * value / totalField / 2;
398 // TODO - Put up an empty circle
399 if (isNaN(middleAngle)) {
405 if (!i || first == 0) {
406 angle = 360 - middleAngle;
407 me.firstAngle = angle;
408 middleAngle = angle - 360 * value / totalField / 2;
410 endAngle = angle - 360 * value / totalField;
419 lenValue = layerTotals[i];
420 slice.rho = me.radius * (lenValue / maxLenField);
422 slice.rho = me.radius;
425 if((slice.startAngle % 360) == (slice.endAngle % 360)) {
426 slice.startAngle -= 0.0001;
432 //do all shadows first.
434 for (i = 0, ln = slices.length; i < ln; i++) {
435 if (this.__excludes && this.__excludes[i]) {
440 slice.shadowAttrs = [];
441 for (j = 0, rhoAcum = 0, shadows = []; j < layers; j++) {
442 sprite = group.getAt(i * layers + j);
443 deltaRho = lenField[j] ? store.getAt(i).get(lenField[j]) / layerTotals[i] * slice.rho: slice.rho;
444 //set pie slice properties
445 rendererAttributes = {
447 startAngle: slice.startAngle,
448 endAngle: slice.endAngle,
451 startRho: rhoAcum + (deltaRho * donut / 100),
452 endRho: rhoAcum + deltaRho
456 for (shindex = 0, shadows = []; shindex < lnsh; shindex++) {
457 shadowAttr = shadowAttributes[shindex];
458 shadow = shadowGroups[shindex].getAt(i);
460 shadow = chart.surface.add(Ext.apply({}, {
462 group: shadowGroups[shindex],
463 strokeLinejoin: "round"
464 }, rendererAttributes, shadowAttr));
467 shadowAttr = me.renderer(shadow, store.getAt(i), Ext.apply({}, rendererAttributes, shadowAttr), i, store);
468 me.onAnimate(shadow, {
472 shadowAttr = me.renderer(shadow, store.getAt(i), Ext.apply(shadowAttr, {
475 shadow.setAttributes(shadowAttr, true);
477 shadows.push(shadow);
479 slice.shadowAttrs[j] = shadows;
483 //do pie slices after.
484 for (i = 0, ln = slices.length; i < ln; i++) {
485 if (this.__excludes && this.__excludes[i]) {
490 for (j = 0, rhoAcum = 0; j < layers; j++) {
491 sprite = group.getAt(i * layers + j);
492 deltaRho = lenField[j] ? store.getAt(i).get(lenField[j]) / layerTotals[i] * slice.rho: slice.rho;
493 //set pie slice properties
494 rendererAttributes = Ext.apply({
496 startAngle: slice.startAngle,
497 endAngle: slice.endAngle,
500 startRho: rhoAcum + (deltaRho * donut / 100),
501 endRho: rhoAcum + deltaRho
503 }, Ext.apply(seriesStyle, colorArrayStyle && { fill: colorArrayStyle[(layers > 1? j : i) % colorArrayLength] } || {}));
505 rendererAttributes.segment, {
508 storeItem: slice.storeItem,
513 item.shadows = slice.shadowAttrs[j];
516 // Create a new sprite if needed (no height)
518 spriteOptions = Ext.apply({
522 }, Ext.apply(seriesStyle, colorArrayStyle && { fill: colorArrayStyle[(layers > 1? j : i) % colorArrayLength] } || {}));
523 sprite = surface.add(Ext.apply(spriteOptions, rendererAttributes));
525 slice.sprite = slice.sprite || [];
526 item.sprite = sprite;
527 slice.sprite.push(sprite);
528 slice.point = [item.middle.x, item.middle.y];
530 rendererAttributes = me.renderer(sprite, store.getAt(i), rendererAttributes, i, store);
531 sprite._to = rendererAttributes;
532 sprite._animating = true;
533 me.onAnimate(sprite, {
534 to: rendererAttributes,
538 this._animating = false;
545 rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply(rendererAttributes, {
548 sprite.setAttributes(rendererAttributes, true);
555 ln = group.getCount();
556 for (i = 0; i < ln; i++) {
557 if (!slices[(i / layers) >> 0] && group.getAt(i)) {
558 group.getAt(i).hide(true);
562 lnsh = shadowGroups.length;
563 for (shindex = 0; shindex < ln; shindex++) {
564 if (!slices[(shindex / layers) >> 0]) {
565 for (j = 0; j < lnsh; j++) {
566 if (shadowGroups[j].getAt(shindex)) {
567 shadowGroups[j].getAt(shindex).hide(true);
577 // @private callback for when creating a label sprite.
578 onCreateLabel: function(storeItem, item, i, display) {
580 group = me.labelsGroup,
582 centerX = me.centerX,
583 centerY = me.centerY,
584 middle = item.middle,
585 endLabelStyle = Ext.apply(me.seriesLabelStyle || {}, config || {});
587 return me.chart.surface.add(Ext.apply({
589 'text-anchor': 'middle',
596 // @private callback for when placing a label sprite.
597 onPlaceLabel: function(label, storeItem, item, i, display, animate, index) {
600 resizing = chart.resizing,
602 format = config.renderer,
603 field = [].concat(config.field),
604 centerX = me.centerX,
605 centerY = me.centerY,
606 middle = item.middle,
611 x = middle.x - centerX,
612 y = middle.y - centerY,
615 theta = Math.atan2(y, x || 1),
616 dg = theta * 180 / Math.PI,
619 function fixAngle(a) {
624 label.setAttributes({
625 text: format(storeItem.get(field[index]))
630 rho = Math.sqrt(x * x + y * y) * 2;
632 opt.x = rho * Math.cos(theta) + centerX;
633 opt.y = rho * Math.sin(theta) + centerY;
638 dg = (dg > 90 && dg < 270) ? dg + 180: dg;
640 prevDg = label.attr.rotation.degrees;
641 if (prevDg != null && Math.abs(prevDg - dg) > 180) {
651 //update rotation angle
662 //ensure the object has zero translation
666 if (animate && !resizing && (display != 'rotate' || prevDg != null)) {
667 me.onAnimate(label, {
671 label.setAttributes(opt, true);
676 // @private callback for when placing a callout sprite.
677 onPlaceCallout: function(callout, storeItem, item, i, display, animate, index) {
680 resizing = chart.resizing,
681 config = me.callouts,
682 centerX = me.centerX,
683 centerY = me.centerY,
684 middle = item.middle,
689 x = middle.x - centerX,
690 y = middle.y - centerY,
693 theta = Math.atan2(y, x || 1),
694 bbox = callout.label.getBBox(),
700 //should be able to config this.
701 rho = item.endRho + offsetFromViz;
702 rhoCenter = (item.endRho + item.startRho) / 2 + (item.endRho - item.startRho) / 3;
704 opt.x = rho * Math.cos(theta) + centerX;
705 opt.y = rho * Math.sin(theta) + centerY;
707 x = rhoCenter * Math.cos(theta);
708 y = rhoCenter * Math.sin(theta);
711 //set the line from the middle of the pie to the box.
712 me.onAnimate(callout.lines, {
714 path: ["M", x + centerX, y + centerY, "L", opt.x, opt.y, "Z", "M", opt.x, opt.y, "l", x > 0 ? offsetToSide: -offsetToSide, 0, "z"]
718 me.onAnimate(callout.box, {
720 x: opt.x + (x > 0 ? offsetToSide: -(offsetToSide + bbox.width + 2 * offsetBox)),
721 y: opt.y + (y > 0 ? ( - bbox.height - offsetBox / 2) : ( - bbox.height - offsetBox / 2)),
722 width: bbox.width + 2 * offsetBox,
723 height: bbox.height + 2 * offsetBox
727 me.onAnimate(callout.label, {
729 x: opt.x + (x > 0 ? (offsetToSide + offsetBox) : -(offsetToSide + bbox.width + offsetBox)),
730 y: opt.y + (y > 0 ? -bbox.height / 4: -bbox.height / 4)
734 //set the line from the middle of the pie to the box.
735 callout.lines.setAttributes({
736 path: ["M", x + centerX, y + centerY, "L", opt.x, opt.y, "Z", "M", opt.x, opt.y, "l", x > 0 ? offsetToSide: -offsetToSide, 0, "z"]
740 callout.box.setAttributes({
741 x: opt.x + (x > 0 ? offsetToSide: -(offsetToSide + bbox.width + 2 * offsetBox)),
742 y: opt.y + (y > 0 ? ( - bbox.height - offsetBox / 2) : ( - bbox.height - offsetBox / 2)),
743 width: bbox.width + 2 * offsetBox,
744 height: bbox.height + 2 * offsetBox
748 callout.label.setAttributes({
749 x: opt.x + (x > 0 ? (offsetToSide + offsetBox) : -(offsetToSide + bbox.width + offsetBox)),
750 y: opt.y + (y > 0 ? -bbox.height / 4: -bbox.height / 4)
755 callout[p].show(true);
759 // @private handles sprite animation for the series.
760 onAnimate: function(sprite, attr) {
762 return this.callParent(arguments);
765 isItemInPoint: function(x, y, item, i) {
772 startAngle = item.startAngle,
773 endAngle = item.endAngle,
774 rho = Math.sqrt(dx * dx + dy * dy),
775 angle = Math.atan2(y - cy, x - cx) / me.rad + 360;
777 // normalize to the same range of angles created by drawSeries
778 if (angle > me.firstAngle) {
781 return (angle <= startAngle && angle > endAngle
782 && rho >= item.startRho && rho <= item.endRho);
785 // @private hides all elements in the series.
786 hideAll: function() {
787 var i, l, shadow, shadows, sh, lsh, sprite;
788 if (!isNaN(this._index)) {
789 this.__excludes = this.__excludes || [];
790 this.__excludes[this._index] = true;
791 sprite = this.slices[this._index].sprite;
792 for (sh = 0, lsh = sprite.length; sh < lsh; sh++) {
793 sprite[sh].setAttributes({
797 if (this.slices[this._index].shadowAttrs) {
798 for (i = 0, shadows = this.slices[this._index].shadowAttrs, l = shadows.length; i < l; i++) {
800 for (sh = 0, lsh = shadow.length; sh < lsh; sh++) {
801 shadow[sh].setAttributes({
811 // @private shows all elements in the series.
812 showAll: function() {
813 if (!isNaN(this._index)) {
814 this.__excludes[this._index] = false;
820 * Highlight the specified item. If no item is provided the whole series will be highlighted.
821 * @param item {Object} Info about the item; same format as returned by #getItemForPoint
823 highlightItem: function(item) {
826 item = item || this.items[this._index];
828 //TODO(nico): sometimes in IE itemmouseover is triggered
829 //twice without triggering itemmouseout in between. This
830 //fixes the highlighting bug. Eventually, events should be
831 //changed to trigger one itemmouseout between two itemmouseovers.
832 this.unHighlightItem();
834 if (!item || item.sprite && item.sprite._animating) {
837 me.callParent([item]);
841 if ('segment' in me.highlightCfg) {
842 var highlightSegment = me.highlightCfg.segment,
843 animate = me.chart.animate,
844 attrs, i, shadows, shadow, ln, to, itemHighlightSegment, prop;
846 if (me.labelsGroup) {
847 var group = me.labelsGroup,
848 display = me.label.display,
849 label = group.getAt(item.index),
850 middle = (item.startAngle + item.endAngle) / 2 * rad,
851 r = highlightSegment.margin || 0,
852 x = r * Math.cos(middle),
853 y = r * Math.sin(middle);
855 //TODO(nico): rounding to 1e-10
856 //gives the right translation. Translation
857 //was buggy for very small numbers. In this
858 //case we're not looking to translate to very small
859 //numbers but not to translate at all.
860 if (Math.abs(x) < 1e-10) {
863 if (Math.abs(y) < 1e-10) {
868 label.stopAnimation();
876 duration: me.highlightDuration
880 label.setAttributes({
889 if (me.chart.shadow && item.shadows) {
891 shadows = item.shadows;
893 for (; i < ln; i++) {
896 itemHighlightSegment = item.sprite._from.segment;
897 for (prop in itemHighlightSegment) {
898 if (! (prop in highlightSegment)) {
899 to[prop] = itemHighlightSegment[prop];
903 segment: Ext.applyIf(to, me.highlightCfg.segment)
906 shadow.stopAnimation();
909 duration: me.highlightDuration
913 shadow.setAttributes(attrs, true);
921 * un-highlights the specified item. If no item is provided it will un-highlight the entire series.
922 * @param item {Object} Info about the item; same format as returned by #getItemForPoint
924 unHighlightItem: function() {
930 if (('segment' in me.highlightCfg) && me.items) {
931 var items = me.items,
932 animate = me.chart.animate,
933 shadowsEnabled = !!me.chart.shadow,
934 group = me.labelsGroup,
938 display = me.label.display,
939 shadowLen, p, to, ihs, hs, sprite, shadows, shadow, item, label, attrs;
941 for (; i < len; i++) {
946 sprite = item.sprite;
947 if (sprite && sprite._highlighted) {
950 label = group.getAt(item.index);
957 display == 'rotate' ? {
961 degrees: label.attr.rotation.degrees
965 label.stopAnimation();
968 duration: me.highlightDuration
972 label.setAttributes(attrs, true);
975 if (shadowsEnabled) {
976 shadows = item.shadows;
977 shadowLen = shadows.length;
978 for (; j < shadowLen; j++) {
980 ihs = item.sprite._to.segment;
981 hs = item.sprite._from.segment;
990 shadow.stopAnimation();
995 duration: me.highlightDuration
999 shadow.setAttributes({ segment: to }, true);
1006 me.callParent(arguments);
1010 * Returns the color of the series (to be displayed as color for the series legend item).
1011 * @param item {Object} Info about the item; same format as returned by #getItemForPoint
1013 getLegendColor: function(index) {
1015 return me.colorArrayStyle[index % me.colorArrayStyle.length];