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; }
11 <script type="text/javascript">
12 function highlight() {
13 document.getElementById(location.hash.replace(/#/, "")).className = "highlight";
17 <body onload="prettyPrint(); highlight();">
18 <pre class="prettyprint lang-js"><span id='Ext-chart-series-Bar'>/**
19 </span> * Creates a Bar Chart. A Bar Chart is a useful visualization technique to display quantitative information for
20 * different categories that can show some progression (or regression) in the dataset. As with all other series, the Bar
21 * Series must be appended in the *series* Chart array configuration. See the Chart documentation for more information.
22 * A typical configuration object for the bar series could be:
25 * var store = Ext.create('Ext.data.JsonStore', {
26 * fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
28 * { 'name': 'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13 },
29 * { 'name': 'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3 },
30 * { 'name': 'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7 },
31 * { 'name': 'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23 },
32 * { 'name': 'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33 }
36 * Ext.create('Ext.chart.Chart', {
37 * renderTo: Ext.getBody(),
47 * renderer: Ext.util.Format.numberRenderer('0,0')
49 * title: 'Sample Values',
56 * title: 'Sample Metrics'
66 * renderer: function(storeItem, item) {
67 * this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data1') + ' views');
71 * display: 'insideEnd',
73 * renderer: Ext.util.Format.numberRenderer('0'),
74 * orientation: 'horizontal',
76 * 'text-anchor': 'middle'
83 * In this configuration we set `bar` as the series type, bind the values of the bar to the bottom axis and set the
84 * xField or category field to the `name` parameter of the store. We also set `highlight` to true which enables smooth
85 * animations when bars are hovered. We also set some configuration for the bar labels to be displayed inside the bar,
86 * to display the information found in the `data1` property of each element store, to render a formated text with the
87 * `Ext.util.Format` we pass in, to have an `horizontal` orientation (as opposed to a vertical one) and we also set
88 * other styles like `color`, `text-anchor`, etc.
90 Ext.define('Ext.chart.series.Bar', {
92 /* Begin Definitions */
94 extend: 'Ext.chart.series.Cartesian',
96 alternateClassName: ['Ext.chart.BarSeries', 'Ext.chart.BarChart', 'Ext.chart.StackedBarChart'],
98 requires: ['Ext.chart.axis.Axis', 'Ext.fx.Anim'],
100 /* End Definitions */
105 <span id='Ext-chart-series-Bar-cfg-column'> /**
106 </span> * @cfg {Boolean} column Whether to set the visualization as column chart or horizontal bar chart.
110 <span id='Ext-chart-series-Bar-cfg-style'> /**
111 </span> * @cfg style Style properties that will override the theming series styles.
115 <span id='Ext-chart-series-Bar-cfg-gutter'> /**
116 </span> * @cfg {Number} gutter The gutter space between single bars, as a percentage of the bar width
120 <span id='Ext-chart-series-Bar-cfg-groupGutter'> /**
121 </span> * @cfg {Number} groupGutter The gutter space between groups of bars, as a percentage of the bar width
125 <span id='Ext-chart-series-Bar-cfg-xPadding'> /**
126 </span> * @cfg {Number} xPadding Padding between the left/right axes and the bars
130 <span id='Ext-chart-series-Bar-cfg-yPadding'> /**
131 </span> * @cfg {Number} yPadding Padding between the top/bottom axes and the bars
135 constructor: function(config) {
136 this.callParent(arguments);
138 surface = me.chart.surface,
139 shadow = me.chart.shadow,
141 Ext.apply(me, config, {
150 "stroke-width": 6,
151 "stroke-opacity": 0.05,
152 stroke: 'rgb(200, 200, 200)',
158 "stroke-width": 4,
159 "stroke-opacity": 0.1,
160 stroke: 'rgb(150, 150, 150)',
166 "stroke-width": 2,
167 "stroke-opacity": 0.15,
168 stroke: 'rgb(100, 100, 100)',
175 me.group = surface.getGroup(me.seriesId + '-bars');
177 for (i = 0, l = me.shadowAttributes.length; i < l; i++) {
178 me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
183 // @private sets the bar girth.
184 getBarGirth: function() {
186 store = me.chart.getChartStore(),
188 ln = store.getCount(),
189 gutter = me.gutter / 100;
191 return (me.chart.chartBBox[column ? 'width' : 'height'] - me[column ? 'xPadding' : 'yPadding'] * 2) / (ln * (gutter + 1) - gutter);
194 // @private returns the gutters.
195 getGutters: function() {
198 gutter = Math.ceil(me[column ? 'xPadding' : 'yPadding'] + me.getBarGirth() / 2);
199 return me.column ? [gutter, 0] : [0, gutter];
202 // @private Get chart and data boundaries
203 getBounds: function() {
206 store = chart.getChartStore(),
207 bars = [].concat(me.yField),
208 barsLen = bars.length,
209 groupBarsLen = barsLen,
210 groupGutter = me.groupGutter / 100,
212 xPadding = me.xPadding,
213 yPadding = me.yPadding,
214 stacked = me.stacked,
215 barWidth = me.getBarGirth(),
219 groupBarWidth, bbox, minY, maxY, axis, out,
220 scale, zero, total, rec, j, plus, minus;
225 //Skip excluded series
227 for (j = 0, total = me.__excludes.length; j < total; j++) {
228 if (me.__excludes[j]) {
235 axis = chart.axes.get(me.axis);
237 out = axis.calcEnds();
243 if (me.yField && !Ext.isNumber(minY)) {
244 axis = Ext.create('Ext.chart.axis.Axis', {
246 fields: [].concat(me.yField)
248 out = axis.calcEnds();
253 if (!Ext.isNumber(minY)) {
256 if (!Ext.isNumber(maxY)) {
259 scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (maxY - minY);
260 groupBarWidth = barWidth / ((stacked ? 1 : groupBarsLen) * (groupGutter + 1) - groupGutter);
261 zero = (column) ? bbox.y + bbox.height - yPadding : bbox.x + xPadding;
265 store.each(function(record, i) {
266 total[0][i] = total[0][i] || 0;
267 total[1][i] = total[1][i] || 0;
268 for (j = 0; j < barsLen; j++) {
269 if (me.__excludes && me.__excludes[j]) {
272 rec = record.get(bars[j]);
273 total[+(rec > 0)][i] += mabs(rec);
276 total[+(maxY > 0)].push(mabs(maxY));
277 total[+(minY > 0)].push(mabs(minY));
278 minus = mmax.apply(math, total[0]);
279 plus = mmax.apply(math, total[1]);
280 scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (plus + minus);
281 zero = zero + minus * scale * (column ? -1 : 1);
283 else if (minY / maxY < 0) {
284 zero = zero - minY * scale * (column ? -1 : 1);
290 groupBarsLen: groupBarsLen,
292 groupBarWidth: groupBarWidth,
297 signed: minY / maxY < 0,
303 // @private Build an array of paths for the chart
304 getPaths: function() {
307 store = chart.getChartStore(),
308 bounds = me.bounds = me.getBounds(),
309 items = me.items = [],
310 gutter = me.gutter / 100,
311 groupGutter = me.groupGutter / 100,
312 animate = chart.animate,
315 enableShadows = chart.shadow,
316 shadowGroups = me.shadowGroups,
317 shadowAttributes = me.shadowAttributes,
318 shadowGroupsLn = shadowGroups.length,
320 xPadding = me.xPadding,
321 yPadding = me.yPadding,
322 stacked = me.stacked,
323 barsLen = bounds.barsLen,
324 colors = me.colorArrayStyle,
325 colorLength = colors && colors.length || 0,
330 j, yValue, height, totalDim, totalNegDim, bottom, top, hasShadow, barAttr, attrs, counter,
331 shadowIndex, shadow, sprite, offset, floorY;
333 store.each(function(record, i, total) {
334 bottom = bounds.zero;
339 for (j = 0, counter = 0; j < barsLen; j++) {
341 if (me.__excludes && me.__excludes[j]) {
344 yValue = record.get(bounds.bars[j]);
345 height = Math.round((yValue - mmax(bounds.minY, 0)) * bounds.scale);
347 fill: colors[(barsLen > 1 ? j : 0) % colorLength]
352 width: mmax(bounds.groupBarWidth, 0),
353 x: (bbox.x + xPadding + i * bounds.barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked),
358 // draw in reverse order
359 offset = (total - 1) - i;
361 height: mmax(bounds.groupBarWidth, 0),
362 width: height + (bottom == bounds.zero),
363 x: bottom + (bottom != bounds.zero),
364 y: (bbox.y + yPadding + offset * bounds.barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked + 1)
370 barAttr.height = mabs(height);
372 barAttr.x = top + height;
373 barAttr.width = mabs(height);
378 top += height * (column ? -1 : 1);
380 bottom += height * (column ? -1 : 1);
382 totalDim += mabs(height);
384 totalNegDim += mabs(height);
387 barAttr.x = Math.floor(barAttr.x) + 1;
388 floorY = Math.floor(barAttr.y);
389 if (!Ext.isIE9 && barAttr.y > floorY) {
393 barAttr.width = Math.floor(barAttr.width);
394 barAttr.height = Math.floor(barAttr.height);
398 value: [record.get(me.xField), yValue],
400 point: column ? [barAttr.x + barAttr.width / 2, yValue >= 0 ? barAttr.y : barAttr.y + barAttr.height] :
401 [yValue >= 0 ? barAttr.x + barAttr.width : barAttr.x, barAttr.y + barAttr.height / 2]
403 // When resizing, reset before animating
404 if (animate && chart.resizing) {
408 width: barAttr.width,
414 height: barAttr.height
416 if (enableShadows && (stacked && !hasShadow || !stacked)) {
419 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
420 shadow = shadowGroups[shadowIndex].getAt(stacked ? i : (i * barsLen + j));
422 shadow.setAttributes(attrs, true);
426 //update sprite position and width/height
427 sprite = group.getAt(i * barsLen + j);
429 sprite.setAttributes(attrs, true);
434 if (stacked && items.length) {
435 items[i * counter].totalDim = totalDim;
436 items[i * counter].totalNegDim = totalNegDim;
441 // @private render/setAttributes on the shadows
442 renderShadows: function(i, barAttr, baseAttrs, bounds) {
445 surface = chart.surface,
446 animate = chart.animate,
447 stacked = me.stacked,
448 shadowGroups = me.shadowGroups,
449 shadowAttributes = me.shadowAttributes,
450 shadowGroupsLn = shadowGroups.length,
451 store = chart.getChartStore(),
456 shadowIndex, shadowBarAttr, shadow, totalDim, totalNegDim, j, rendererAttributes;
458 if ((stacked && (i % bounds.groupBarsLen === 0)) || !stacked) {
459 j = i / bounds.groupBarsLen;
461 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
462 shadowBarAttr = Ext.apply({}, shadowAttributes[shadowIndex]);
463 shadow = shadowGroups[shadowIndex].getAt(stacked ? j : i);
464 Ext.copyTo(shadowBarAttr, barAttr, 'x,y,width,height');
466 shadow = surface.add(Ext.apply({
468 group: shadowGroups[shadowIndex]
469 }, Ext.apply({}, baseAttrs, shadowBarAttr)));
472 totalDim = items[i].totalDim;
473 totalNegDim = items[i].totalNegDim;
475 shadowBarAttr.y = zero - totalNegDim;
476 shadowBarAttr.height = totalDim;
479 shadowBarAttr.x = zero - totalNegDim;
480 shadowBarAttr.width = totalDim;
485 rendererAttributes = me.renderer(shadow, store.getAt(j), shadowBarAttr, i, store);
486 me.onAnimate(shadow, { to: rendererAttributes });
489 rendererAttributes = me.renderer(shadow, store.getAt(j), Ext.apply(shadowBarAttr, { hidden: true }), i, store);
490 shadow.setAttributes(rendererAttributes, true);
494 rendererAttributes = me.renderer(shadow, store.getAt(j), Ext.apply(shadowBarAttr, { hidden: false }), i, store);
495 shadow.setAttributes(rendererAttributes, true);
497 shadows.push(shadow);
503 <span id='Ext-chart-series-Bar-method-drawSeries'> /**
504 </span> * Draws the series for the current chart.
506 drawSeries: function() {
509 store = chart.getChartStore(),
510 surface = chart.surface,
511 animate = chart.animate,
512 stacked = me.stacked,
514 enableShadows = chart.shadow,
515 shadowGroups = me.shadowGroups,
516 shadowGroupsLn = shadowGroups.length,
518 seriesStyle = me.seriesStyle,
519 items, ln, i, j, baseAttrs, sprite, rendererAttributes, shadowIndex, shadowGroup,
520 bounds, endSeriesStyle, barAttr, attrs, anim;
522 if (!store || !store.getCount()) {
526 //fill colors are taken from the colors array.
527 delete seriesStyle.fill;
528 endSeriesStyle = Ext.apply(seriesStyle, this.style);
529 me.unHighlightItem();
530 me.cleanHighlights();
536 baseAttrs = column ? {
544 // Create new or reuse sprites and animate/display
545 for (i = 0; i < ln; i++) {
546 sprite = group.getAt(i);
547 barAttr = items[i].attr;
550 items[i].shadows = me.renderShadows(i, barAttr, baseAttrs, bounds);
553 // Create a new sprite if needed (no height)
555 attrs = Ext.apply({}, baseAttrs, barAttr);
556 attrs = Ext.apply(attrs, endSeriesStyle || {});
557 sprite = surface.add(Ext.apply({}, {
563 rendererAttributes = me.renderer(sprite, store.getAt(i), barAttr, i, store);
564 sprite._to = rendererAttributes;
565 anim = me.onAnimate(sprite, { to: Ext.apply(rendererAttributes, endSeriesStyle) });
566 if (enableShadows && stacked && (i % bounds.barsLen === 0)) {
567 j = i / bounds.barsLen;
568 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
569 anim.on('afteranimate', function() {
571 }, shadowGroups[shadowIndex].getAt(j));
576 rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply(barAttr, { hidden: false }), i, store);
577 sprite.setAttributes(Ext.apply(rendererAttributes, endSeriesStyle), true);
579 items[i].sprite = sprite;
582 // Hide unused sprites
583 ln = group.getCount();
584 for (j = i; j < ln; j++) {
585 group.getAt(j).hide(true);
587 // Hide unused shadows
589 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
590 shadowGroup = shadowGroups[shadowIndex];
591 ln = shadowGroup.getCount();
592 for (j = i; j < ln; j++) {
593 shadowGroup.getAt(j).hide(true);
600 // @private handled when creating a label.
601 onCreateLabel: function(storeItem, item, i, display) {
603 surface = me.chart.surface,
604 group = me.labelsGroup,
606 endLabelStyle = Ext.apply({}, config, me.seriesLabelStyle || {}),
608 return surface.add(Ext.apply({
611 }, endLabelStyle || {}));
614 // @private callback used when placing a label.
615 onPlaceLabel: function(label, storeItem, item, i, display, animate, j, index) {
616 // Determine the label's final position. Starts with the configured preferred value but
617 // may get flipped from inside to outside or vice-versa depending on space.
620 groupBarWidth = opt.groupBarWidth,
623 chartBBox = chart.chartBBox,
624 resizing = chart.resizing,
625 xValue = item.value[0],
626 yValue = item.value[1],
629 rotate = config.orientation == 'vertical',
630 field = [].concat(config.field),
631 format = config.renderer,
632 text = format(storeItem.get(field[index])),
633 size = me.getLabelSize(text),
635 height = size.height,
638 insideStart = 'insideStart',
639 insideEnd = 'insideEnd',
645 label.setAttributes({
649 label.isOutside = false;
651 if (display == outside) {
652 if (height + offsetY + attr.height > (yValue >= 0 ? zero - chartBBox.y : chartBBox.y + chartBBox.height - zero)) {
656 if (height + offsetY > attr.height) {
658 label.isOutside = true;
661 x = attr.x + groupBarWidth / 2;
662 y = display == insideStart ?
663 (zero + ((height / 2 + 3) * (yValue >= 0 ? -1 : 1))) :
664 (yValue >= 0 ? (attr.y + ((height / 2 + 3) * (display == outside ? -1 : 1))) :
665 (attr.y + attr.height + ((height / 2 + 3) * (display === outside ? 1 : -1))));
668 if (display == outside) {
669 if (width + offsetX + attr.width > (yValue >= 0 ? chartBBox.x + chartBBox.width - zero : zero - chartBBox.x)) {
674 if (width + offsetX > attr.width) {
676 label.isOutside = true;
679 x = display == insideStart ?
680 (zero + ((width / 2 + 5) * (yValue >= 0 ? 1 : -1))) :
681 (yValue >= 0 ? (attr.x + attr.width + ((width / 2 + 5) * (display === outside ? 1 : -1))) :
682 (attr.x + ((width / 2 + 5) * (display === outside ? -1 : 1))));
683 y = attr.y + groupBarWidth / 2;
699 if (animate && resizing) {
701 x = attr.x + attr.width / 2;
705 y = attr.y + attr.height / 2;
707 label.setAttributes({
712 label.setAttributes({
723 me.onAnimate(label, { to: finalAttr });
726 label.setAttributes(Ext.apply(finalAttr, {
733 * Gets the dimensions of a given bar label. Uses a single hidden sprite to avoid
734 * changing visible sprites.
737 getLabelSize: function(value) {
738 var tester = this.testerLabel,
740 endLabelStyle = Ext.apply({}, config, this.seriesLabelStyle || {}),
741 rotated = config.orientation === 'vertical',
745 tester = this.testerLabel = this.chart.surface.add(Ext.apply({
750 tester.setAttributes({
754 // Flip the width/height if rotated, as getBBox returns the pre-rotated dimensions
755 bbox = tester.getBBox();
759 width: rotated ? h : w,
760 height: rotated ? w : h
764 // @private used to animate label, markers and other sprites.
765 onAnimate: function(sprite, attr) {
767 return this.callParent(arguments);
770 isItemInPoint: function(x, y, item) {
771 var bbox = item.sprite.getBBox();
772 return bbox.x <= x && bbox.y <= y
773 && (bbox.x + bbox.width) >= x
774 && (bbox.y + bbox.height) >= y;
777 // @private hide all markers
778 hideAll: function() {
779 var axes = this.chart.axes;
780 if (!isNaN(this._index)) {
781 if (!this.__excludes) {
782 this.__excludes = [];
784 this.__excludes[this._index] = true;
786 axes.each(function(axis) {
792 // @private show all markers
793 showAll: function() {
794 var axes = this.chart.axes;
795 if (!isNaN(this._index)) {
796 if (!this.__excludes) {
797 this.__excludes = [];
799 this.__excludes[this._index] = false;
801 axes.each(function(axis) {
807 <span id='Ext-chart-series-Bar-method-getLegendColor'> /**
808 </span> * Returns a string with the color to be used for the series legend item.
811 getLegendColor: function(index) {
813 colorLength = me.colorArrayStyle.length;
815 if (me.style && me.style.fill) {
816 return me.style.fill;
818 return me.colorArrayStyle[index % colorLength];
822 highlightItem: function(item) {
823 this.callParent(arguments);
827 unHighlightItem: function() {
828 this.callParent(arguments);
832 cleanHighlights: function() {
833 this.callParent(arguments);