1 <!DOCTYPE html><html><head><title>Sencha Documentation Project</title><link rel="stylesheet" href="../reset.css" type="text/css"><link rel="stylesheet" href="../prettify.css" type="text/css"><link rel="stylesheet" href="../prettify_sa.css" type="text/css"><script type="text/javascript" src="../prettify.js"></script></head><body onload="prettyPrint()"><pre class="prettyprint"><pre><span id='Ext-chart.series.Bar'>/**
2 </span> * Creates a Bar Chart. A Bar Chart is a useful visualization technique to display quantitative information for
3 * different categories that can show some progression (or regression) in the dataset. As with all other series, the Bar
4 * Series must be appended in the *series* Chart array configuration. See the Chart documentation for more information.
5 * A typical configuration object for the bar series could be:
7 * {@img Ext.chart.series.Bar/Ext.chart.series.Bar.png Ext.chart.series.Bar chart series}
9 * var store = Ext.create('Ext.data.JsonStore', {
10 * fields: ['name', 'data1', 'data2', 'data3', 'data4', 'data5'],
12 * {'name':'metric one', 'data1':10, 'data2':12, 'data3':14, 'data4':8, 'data5':13},
13 * {'name':'metric two', 'data1':7, 'data2':8, 'data3':16, 'data4':10, 'data5':3},
14 * {'name':'metric three', 'data1':5, 'data2':2, 'data3':14, 'data4':12, 'data5':7},
15 * {'name':'metric four', 'data1':2, 'data2':14, 'data3':6, 'data4':1, 'data5':23},
16 * {'name':'metric five', 'data1':27, 'data2':38, 'data3':36, 'data4':13, 'data5':33}
20 * Ext.create('Ext.chart.Chart', {
21 * renderTo: Ext.getBody(),
31 * renderer: Ext.util.Format.numberRenderer('0,0')
33 * title: 'Sample Values',
40 * title: 'Sample Metrics'
50 * renderer: function(storeItem, item) {
51 * this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data1') + ' views');
55 * display: 'insideEnd',
57 * renderer: Ext.util.Format.numberRenderer('0'),
58 * orientation: 'horizontal',
60 * 'text-anchor': 'middle'
67 * In this configuration we set `bar` as the series type, bind the values of the bar to the bottom axis and set the
68 * xField or category field to the `name` parameter of the store. We also set `highlight` to true which enables smooth
69 * animations when bars are hovered. We also set some configuration for the bar labels to be displayed inside the bar,
70 * to display the information found in the `data1` property of each element store, to render a formated text with the
71 * `Ext.util.Format` we pass in, to have an `horizontal` orientation (as opposed to a vertical one) and we also set
72 * other styles like `color`, `text-anchor`, etc.
74 Ext.define('Ext.chart.series.Bar', {
76 /* Begin Definitions */
78 extend: 'Ext.chart.series.Cartesian',
80 alternateClassName: ['Ext.chart.BarSeries', 'Ext.chart.BarChart', 'Ext.chart.StackedBarChart'],
82 requires: ['Ext.chart.axis.Axis', 'Ext.fx.Anim'],
89 <span id='Ext-chart.series.Bar-cfg-column'> /**
90 </span> * @cfg {Boolean} column Whether to set the visualization as column chart or horizontal bar chart.
94 <span id='Ext-chart.series.Bar-cfg-style'> /**
95 </span> * @cfg style Style properties that will override the theming series styles.
99 <span id='Ext-chart.series.Bar-cfg-gutter'> /**
100 </span> * @cfg {Number} gutter The gutter space between single bars, as a percentage of the bar width
104 <span id='Ext-chart.series.Bar-cfg-groupGutter'> /**
105 </span> * @cfg {Number} groupGutter The gutter space between groups of bars, as a percentage of the bar width
109 <span id='Ext-chart.series.Bar-cfg-xPadding'> /**
110 </span> * @cfg {Number} xPadding Padding between the left/right axes and the bars
114 <span id='Ext-chart.series.Bar-cfg-yPadding'> /**
115 </span> * @cfg {Number} yPadding Padding between the top/bottom axes and the bars
119 constructor: function(config) {
120 this.callParent(arguments);
122 surface = me.chart.surface,
123 shadow = me.chart.shadow,
125 Ext.apply(me, config, {
134 "stroke-width": 6,
135 "stroke-opacity": 0.05,
136 stroke: 'rgb(200, 200, 200)',
142 "stroke-width": 4,
143 "stroke-opacity": 0.1,
144 stroke: 'rgb(150, 150, 150)',
150 "stroke-width": 2,
151 "stroke-opacity": 0.15,
152 stroke: 'rgb(100, 100, 100)',
159 me.group = surface.getGroup(me.seriesId + '-bars');
161 for (i = 0, l = me.shadowAttributes.length; i < l; i++) {
162 me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
167 // @private sets the bar girth.
168 getBarGirth: function() {
170 store = me.chart.store,
172 ln = store.getCount(),
173 gutter = me.gutter / 100;
175 return (me.chart.chartBBox[column ? 'width' : 'height'] - me[column ? 'xPadding' : 'yPadding'] * 2) / (ln * (gutter + 1) - gutter);
178 // @private returns the gutters.
179 getGutters: function() {
182 gutter = Math.ceil(me[column ? 'xPadding' : 'yPadding'] + me.getBarGirth() / 2);
183 return me.column ? [gutter, 0] : [0, gutter];
186 // @private Get chart and data boundaries
187 getBounds: function() {
190 store = chart.substore || chart.store,
191 bars = [].concat(me.yField),
192 barsLen = bars.length,
193 groupBarsLen = barsLen,
194 groupGutter = me.groupGutter / 100,
196 xPadding = me.xPadding,
197 yPadding = me.yPadding,
198 stacked = me.stacked,
199 barWidth = me.getBarGirth(),
203 groupBarWidth, bbox, minY, maxY, axis, out,
204 scale, zero, total, rec, j, plus, minus;
209 //Skip excluded series
211 for (j = 0, total = me.__excludes.length; j < total; j++) {
212 if (me.__excludes[j]) {
219 axis = chart.axes.get(me.axis);
221 out = axis.calcEnds();
222 minY = out.from || axis.prevMin;
223 maxY = mmax(out.to || axis.prevMax, 0);
227 if (me.yField && !Ext.isNumber(minY)) {
228 axis = Ext.create('Ext.chart.axis.Axis', {
230 fields: [].concat(me.yField)
232 out = axis.calcEnds();
233 minY = out.from || axis.prevMin;
234 maxY = mmax(out.to || axis.prevMax, 0);
237 if (!Ext.isNumber(minY)) {
240 if (!Ext.isNumber(maxY)) {
243 scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (maxY - minY);
244 groupBarWidth = barWidth / ((stacked ? 1 : groupBarsLen) * (groupGutter + 1) - groupGutter);
245 zero = (column) ? bbox.y + bbox.height - yPadding : bbox.x + xPadding;
249 store.each(function(record, i) {
250 total[0][i] = total[0][i] || 0;
251 total[1][i] = total[1][i] || 0;
252 for (j = 0; j < barsLen; j++) {
253 if (me.__excludes && me.__excludes[j]) {
256 rec = record.get(bars[j]);
257 total[+(rec > 0)][i] += mabs(rec);
260 total[+(maxY > 0)].push(mabs(maxY));
261 total[+(minY > 0)].push(mabs(minY));
262 minus = mmax.apply(math, total[0]);
263 plus = mmax.apply(math, total[1]);
264 scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (plus + minus);
265 zero = zero + minus * scale * (column ? -1 : 1);
267 else if (minY / maxY < 0) {
268 zero = zero - minY * scale * (column ? -1 : 1);
274 groupBarsLen: groupBarsLen,
276 groupBarWidth: groupBarWidth,
281 signed: minY / maxY < 0,
287 // @private Build an array of paths for the chart
288 getPaths: function() {
291 store = chart.substore || chart.store,
292 bounds = me.bounds = me.getBounds(),
293 items = me.items = [],
294 gutter = me.gutter / 100,
295 groupGutter = me.groupGutter / 100,
296 animate = chart.animate,
299 enableShadows = chart.shadow,
300 shadowGroups = me.shadowGroups,
301 shadowAttributes = me.shadowAttributes,
302 shadowGroupsLn = shadowGroups.length,
304 xPadding = me.xPadding,
305 yPadding = me.yPadding,
306 stacked = me.stacked,
307 barsLen = bounds.barsLen,
308 colors = me.colorArrayStyle,
309 colorLength = colors && colors.length || 0,
314 j, yValue, height, totalDim, totalNegDim, bottom, top, hasShadow, barAttr, attrs, counter,
315 shadowIndex, shadow, sprite, offset, floorY;
317 store.each(function(record, i, total) {
318 bottom = bounds.zero;
323 for (j = 0, counter = 0; j < barsLen; j++) {
325 if (me.__excludes && me.__excludes[j]) {
328 yValue = record.get(bounds.bars[j]);
329 height = Math.round((yValue - ((bounds.minY < 0) ? 0 : bounds.minY)) * bounds.scale);
331 fill: colors[(barsLen > 1 ? j : 0) % colorLength]
336 width: mmax(bounds.groupBarWidth, 0),
337 x: (bbox.x + xPadding + i * bounds.barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked),
342 // draw in reverse order
343 offset = (total - 1) - i;
345 height: mmax(bounds.groupBarWidth, 0),
346 width: height + (bottom == bounds.zero),
347 x: bottom + (bottom != bounds.zero),
348 y: (bbox.y + yPadding + offset * bounds.barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked + 1)
354 barAttr.height = mabs(height);
356 barAttr.x = top + height;
357 barAttr.width = mabs(height);
362 top += height * (column ? -1 : 1);
364 bottom += height * (column ? -1 : 1);
366 totalDim += mabs(height);
368 totalNegDim += mabs(height);
371 barAttr.x = Math.floor(barAttr.x) + 1;
372 floorY = Math.floor(barAttr.y);
373 if (!Ext.isIE9 && barAttr.y > floorY) {
377 barAttr.width = Math.floor(barAttr.width);
378 barAttr.height = Math.floor(barAttr.height);
382 value: [record.get(me.xField), yValue],
384 point: column ? [barAttr.x + barAttr.width / 2, yValue >= 0 ? barAttr.y : barAttr.y + barAttr.height] :
385 [yValue >= 0 ? barAttr.x + barAttr.width : barAttr.x, barAttr.y + barAttr.height / 2]
387 // When resizing, reset before animating
388 if (animate && chart.resizing) {
392 width: barAttr.width,
398 height: barAttr.height
400 if (enableShadows && (stacked && !hasShadow || !stacked)) {
403 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
404 shadow = shadowGroups[shadowIndex].getAt(stacked ? i : (i * barsLen + j));
406 shadow.setAttributes(attrs, true);
410 //update sprite position and width/height
411 sprite = group.getAt(i * barsLen + j);
413 sprite.setAttributes(attrs, true);
418 if (stacked && items.length) {
419 items[i * counter].totalDim = totalDim;
420 items[i * counter].totalNegDim = totalNegDim;
425 // @private render/setAttributes on the shadows
426 renderShadows: function(i, barAttr, baseAttrs, bounds) {
429 surface = chart.surface,
430 animate = chart.animate,
431 stacked = me.stacked,
432 shadowGroups = me.shadowGroups,
433 shadowAttributes = me.shadowAttributes,
434 shadowGroupsLn = shadowGroups.length,
435 store = chart.substore || chart.store,
440 shadowIndex, shadowBarAttr, shadow, totalDim, totalNegDim, j, rendererAttributes;
442 if ((stacked && (i % bounds.groupBarsLen === 0)) || !stacked) {
443 j = i / bounds.groupBarsLen;
445 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
446 shadowBarAttr = Ext.apply({}, shadowAttributes[shadowIndex]);
447 shadow = shadowGroups[shadowIndex].getAt(stacked ? j : i);
448 Ext.copyTo(shadowBarAttr, barAttr, 'x,y,width,height');
450 shadow = surface.add(Ext.apply({
452 group: shadowGroups[shadowIndex]
453 }, Ext.apply({}, baseAttrs, shadowBarAttr)));
456 totalDim = items[i].totalDim;
457 totalNegDim = items[i].totalNegDim;
459 shadowBarAttr.y = zero - totalNegDim;
460 shadowBarAttr.height = totalDim;
463 shadowBarAttr.x = zero - totalNegDim;
464 shadowBarAttr.width = totalDim;
469 rendererAttributes = me.renderer(shadow, store.getAt(j), shadowBarAttr, i, store);
470 me.onAnimate(shadow, { to: rendererAttributes });
473 rendererAttributes = me.renderer(shadow, store.getAt(j), Ext.apply(shadowBarAttr, { hidden: true }), i, store);
474 shadow.setAttributes(rendererAttributes, true);
478 rendererAttributes = me.renderer(shadow, store.getAt(j), Ext.apply(shadowBarAttr, { hidden: false }), i, store);
479 shadow.setAttributes(rendererAttributes, true);
481 shadows.push(shadow);
487 <span id='Ext-chart.series.Bar-method-drawSeries'> /**
488 </span> * Draws the series for the current chart.
490 drawSeries: function() {
493 store = chart.substore || chart.store,
494 surface = chart.surface,
495 animate = chart.animate,
496 stacked = me.stacked,
498 enableShadows = chart.shadow,
499 shadowGroups = me.shadowGroups,
500 shadowGroupsLn = shadowGroups.length,
502 seriesStyle = me.seriesStyle,
503 items, ln, i, j, baseAttrs, sprite, rendererAttributes, shadowIndex, shadowGroup,
504 bounds, endSeriesStyle, barAttr, attrs, anim;
506 if (!store || !store.getCount()) {
510 //fill colors are taken from the colors array.
511 delete seriesStyle.fill;
512 endSeriesStyle = Ext.apply(seriesStyle, this.style);
513 me.unHighlightItem();
514 me.cleanHighlights();
520 baseAttrs = column ? {
528 // Create new or reuse sprites and animate/display
529 for (i = 0; i < ln; i++) {
530 sprite = group.getAt(i);
531 barAttr = items[i].attr;
534 items[i].shadows = me.renderShadows(i, barAttr, baseAttrs, bounds);
537 // Create a new sprite if needed (no height)
539 attrs = Ext.apply({}, baseAttrs, barAttr);
540 attrs = Ext.apply(attrs, endSeriesStyle || {});
541 sprite = surface.add(Ext.apply({}, {
547 rendererAttributes = me.renderer(sprite, store.getAt(i), barAttr, i, store);
548 sprite._to = rendererAttributes;
549 anim = me.onAnimate(sprite, { to: Ext.apply(rendererAttributes, endSeriesStyle) });
550 if (enableShadows && stacked && (i % bounds.barsLen === 0)) {
551 j = i / bounds.barsLen;
552 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
553 anim.on('afteranimate', function() {
555 }, shadowGroups[shadowIndex].getAt(j));
560 rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply(barAttr, { hidden: false }), i, store);
561 sprite.setAttributes(Ext.apply(rendererAttributes, endSeriesStyle), true);
563 items[i].sprite = sprite;
566 // Hide unused sprites
567 ln = group.getCount();
568 for (j = i; j < ln; j++) {
569 group.getAt(j).hide(true);
571 // Hide unused shadows
573 for (shadowIndex = 0; shadowIndex < shadowGroupsLn; shadowIndex++) {
574 shadowGroup = shadowGroups[shadowIndex];
575 ln = shadowGroup.getCount();
576 for (j = i; j < ln; j++) {
577 shadowGroup.getAt(j).hide(true);
584 // @private handled when creating a label.
585 onCreateLabel: function(storeItem, item, i, display) {
587 surface = me.chart.surface,
588 group = me.labelsGroup,
590 endLabelStyle = Ext.apply({}, config, me.seriesLabelStyle || {}),
592 return surface.add(Ext.apply({
595 }, endLabelStyle || {}));
598 // @private callback used when placing a label.
599 onPlaceLabel: function(label, storeItem, item, i, display, animate, index) {
600 // Determine the label's final position. Starts with the configured preferred value but
601 // may get flipped from inside to outside or vice-versa depending on space.
604 groupBarWidth = opt.groupBarWidth,
607 chartBBox = chart.chartBBox,
608 resizing = chart.resizing,
609 xValue = item.value[0],
610 yValue = item.value[1],
613 rotate = config.orientation == 'vertical',
614 field = [].concat(config.field),
615 format = config.renderer,
616 text = format(storeItem.get(field[index])),
617 size = me.getLabelSize(text),
619 height = size.height,
622 insideStart = 'insideStart',
623 insideEnd = 'insideEnd',
629 label.setAttributes({
634 if (display == outside) {
635 if (height + offsetY + attr.height > (yValue >= 0 ? zero - chartBBox.y : chartBBox.y + chartBBox.height - zero)) {
639 if (height + offsetY > attr.height) {
643 x = attr.x + groupBarWidth / 2;
644 y = display == insideStart ?
645 (zero + ((height / 2 + 3) * (yValue >= 0 ? -1 : 1))) :
646 (yValue >= 0 ? (attr.y + ((height / 2 + 3) * (display == outside ? -1 : 1))) :
647 (attr.y + attr.height + ((height / 2 + 3) * (display === outside ? 1 : -1))));
650 if (display == outside) {
651 if (width + offsetX + attr.width > (yValue >= 0 ? chartBBox.x + chartBBox.width - zero : zero - chartBBox.x)) {
656 if (width + offsetX > attr.width) {
660 x = display == insideStart ?
661 (zero + ((width / 2 + 5) * (yValue >= 0 ? 1 : -1))) :
662 (yValue >= 0 ? (attr.x + attr.width + ((width / 2 + 5) * (display === outside ? 1 : -1))) :
663 (attr.x + ((width / 2 + 5) * (display === outside ? -1 : 1))));
664 y = attr.y + groupBarWidth / 2;
680 if (animate && resizing) {
682 x = attr.x + attr.width / 2;
686 y = attr.y + attr.height / 2;
688 label.setAttributes({
693 label.setAttributes({
704 me.onAnimate(label, { to: finalAttr });
707 label.setAttributes(Ext.apply(finalAttr, {
714 * Gets the dimensions of a given bar label. Uses a single hidden sprite to avoid
715 * changing visible sprites.
718 getLabelSize: function(value) {
719 var tester = this.testerLabel,
721 endLabelStyle = Ext.apply({}, config, this.seriesLabelStyle || {}),
722 rotated = config.orientation === 'vertical',
726 tester = this.testerLabel = this.chart.surface.add(Ext.apply({
731 tester.setAttributes({
735 // Flip the width/height if rotated, as getBBox returns the pre-rotated dimensions
736 bbox = tester.getBBox();
740 width: rotated ? h : w,
741 height: rotated ? w : h
745 // @private used to animate label, markers and other sprites.
746 onAnimate: function(sprite, attr) {
748 return this.callParent(arguments);
751 isItemInPoint: function(x, y, item) {
752 var bbox = item.sprite.getBBox();
753 return bbox.x <= x && bbox.y <= y
754 && (bbox.x + bbox.width) >= x
755 && (bbox.y + bbox.height) >= y;
758 // @private hide all markers
759 hideAll: function() {
760 var axes = this.chart.axes;
761 if (!isNaN(this._index)) {
762 if (!this.__excludes) {
763 this.__excludes = [];
765 this.__excludes[this._index] = true;
767 axes.each(function(axis) {
773 // @private show all markers
774 showAll: function() {
775 var axes = this.chart.axes;
776 if (!isNaN(this._index)) {
777 if (!this.__excludes) {
778 this.__excludes = [];
780 this.__excludes[this._index] = false;
782 axes.each(function(axis) {
788 <span id='Ext-chart.series.Bar-method-getLegendColor'> /**
789 </span> * Returns a string with the color to be used for the series legend item.
792 getLegendColor: function(index) {
794 return me.colorArrayStyle[index % me.colorArrayStyle.length];
796 });</pre></pre></body></html>