Bar2.html 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884
  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-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:
  23. *
  24. * @example
  25. * var store = Ext.create('Ext.data.JsonStore', {
  26. * fields: ['name', 'data'],
  27. * data: [
  28. * { 'name': 'metric one', 'data':10 },
  29. * { 'name': 'metric two', 'data': 7 },
  30. * { 'name': 'metric three', 'data': 5 },
  31. * { 'name': 'metric four', 'data': 2 },
  32. * { 'name': 'metric five', 'data':27 }
  33. * ]
  34. * });
  35. *
  36. * Ext.create('Ext.chart.Chart', {
  37. * renderTo: Ext.getBody(),
  38. * width: 500,
  39. * height: 300,
  40. * animate: true,
  41. * store: store,
  42. * axes: [{
  43. * type: 'Numeric',
  44. * position: 'bottom',
  45. * fields: ['data'],
  46. * label: {
  47. * renderer: Ext.util.Format.numberRenderer('0,0')
  48. * },
  49. * title: 'Sample Values',
  50. * grid: true,
  51. * minimum: 0
  52. * }, {
  53. * type: 'Category',
  54. * position: 'left',
  55. * fields: ['name'],
  56. * title: 'Sample Metrics'
  57. * }],
  58. * series: [{
  59. * type: 'bar',
  60. * axis: 'bottom',
  61. * highlight: true,
  62. * tips: {
  63. * trackMouse: true,
  64. * width: 140,
  65. * height: 28,
  66. * renderer: function(storeItem, item) {
  67. * this.setTitle(storeItem.get('name') + ': ' + storeItem.get('data') + ' views');
  68. * }
  69. * },
  70. * label: {
  71. * display: 'insideEnd',
  72. * field: 'data',
  73. * renderer: Ext.util.Format.numberRenderer('0'),
  74. * orientation: 'horizontal',
  75. * color: '#333',
  76. * 'text-anchor': 'middle'
  77. * },
  78. * xField: 'name',
  79. * yField: 'data'
  80. * }]
  81. * });
  82. *
  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.
  89. */
  90. Ext.define('Ext.chart.series.Bar', {
  91. /* Begin Definitions */
  92. extend: 'Ext.chart.series.Cartesian',
  93. alternateClassName: ['Ext.chart.BarSeries', 'Ext.chart.BarChart', 'Ext.chart.StackedBarChart'],
  94. requires: ['Ext.chart.axis.Axis', 'Ext.fx.Anim'],
  95. /* End Definitions */
  96. type: 'bar',
  97. alias: 'series.bar',
  98. <span id='Ext-chart-series-Bar-cfg-column'> /**
  99. </span> * @cfg {Boolean} column Whether to set the visualization as column chart or horizontal bar chart.
  100. */
  101. column: false,
  102. <span id='Ext-chart-series-Bar-cfg-style'> /**
  103. </span> * @cfg style Style properties that will override the theming series styles.
  104. */
  105. style: {},
  106. <span id='Ext-chart-series-Bar-cfg-gutter'> /**
  107. </span> * @cfg {Number} gutter The gutter space between single bars, as a percentage of the bar width
  108. */
  109. gutter: 38.2,
  110. <span id='Ext-chart-series-Bar-cfg-groupGutter'> /**
  111. </span> * @cfg {Number} groupGutter The gutter space between groups of bars, as a percentage of the bar width
  112. */
  113. groupGutter: 38.2,
  114. <span id='Ext-chart-series-Bar-cfg-xPadding'> /**
  115. </span> * @cfg {Number} xPadding Padding between the left/right axes and the bars
  116. */
  117. xPadding: 0,
  118. <span id='Ext-chart-series-Bar-cfg-yPadding'> /**
  119. </span> * @cfg {Number} yPadding Padding between the top/bottom axes and the bars
  120. */
  121. yPadding: 10,
  122. constructor: function(config) {
  123. this.callParent(arguments);
  124. var me = this,
  125. surface = me.chart.surface,
  126. shadow = me.chart.shadow,
  127. i, l;
  128. config.highlightCfg = Ext.Object.merge({
  129. lineWidth: 3,
  130. stroke: '#55c',
  131. opacity: 0.8,
  132. color: '#f00'
  133. }, config.highlightCfg);
  134. Ext.apply(me, config, {
  135. shadowAttributes: [{
  136. &quot;stroke-width&quot;: 6,
  137. &quot;stroke-opacity&quot;: 0.05,
  138. stroke: 'rgb(200, 200, 200)',
  139. translate: {
  140. x: 1.2,
  141. y: 1.2
  142. }
  143. }, {
  144. &quot;stroke-width&quot;: 4,
  145. &quot;stroke-opacity&quot;: 0.1,
  146. stroke: 'rgb(150, 150, 150)',
  147. translate: {
  148. x: 0.9,
  149. y: 0.9
  150. }
  151. }, {
  152. &quot;stroke-width&quot;: 2,
  153. &quot;stroke-opacity&quot;: 0.15,
  154. stroke: 'rgb(100, 100, 100)',
  155. translate: {
  156. x: 0.6,
  157. y: 0.6
  158. }
  159. }]
  160. });
  161. me.group = surface.getGroup(me.seriesId + '-bars');
  162. if (shadow) {
  163. for (i = 0, l = me.shadowAttributes.length; i &lt; l; i++) {
  164. me.shadowGroups.push(surface.getGroup(me.seriesId + '-shadows' + i));
  165. }
  166. }
  167. },
  168. // @private sets the bar girth.
  169. getBarGirth: function() {
  170. var me = this,
  171. store = me.chart.getChartStore(),
  172. column = me.column,
  173. ln = store.getCount(),
  174. gutter = me.gutter / 100;
  175. return (me.chart.chartBBox[column ? 'width' : 'height'] - me[column ? 'xPadding' : 'yPadding'] * 2) / (ln * (gutter + 1) - gutter);
  176. },
  177. // @private returns the gutters.
  178. getGutters: function() {
  179. var me = this,
  180. column = me.column,
  181. gutter = Math.ceil(me[column ? 'xPadding' : 'yPadding'] + me.getBarGirth() / 2);
  182. return me.column ? [gutter, 0] : [0, gutter];
  183. },
  184. // @private Get chart and data boundaries
  185. getBounds: function() {
  186. var me = this,
  187. chart = me.chart,
  188. store = chart.getChartStore(),
  189. data = store.data.items,
  190. i, ln, record,
  191. bars = [].concat(me.yField),
  192. barsLen = bars.length,
  193. groupBarsLen = barsLen,
  194. groupGutter = me.groupGutter / 100,
  195. column = me.column,
  196. xPadding = me.xPadding,
  197. yPadding = me.yPadding,
  198. stacked = me.stacked,
  199. barWidth = me.getBarGirth(),
  200. barWidthProperty = column ? 'width' : 'height',
  201. math = Math,
  202. mmin = math.min,
  203. mmax = math.max,
  204. mabs = math.abs,
  205. boundAxes = me.getAxesForXAndYFields(),
  206. boundYAxis = boundAxes.yAxis,
  207. ends, shrunkBarWidth, groupBarWidth, bbox, minY, maxY, axis, out,
  208. scale, zero, total, rec, j, plus, minus;
  209. me.setBBox(true);
  210. bbox = me.bbox;
  211. //Skip excluded series
  212. if (me.__excludes) {
  213. for (j = 0, total = me.__excludes.length; j &lt; total; j++) {
  214. if (me.__excludes[j]) {
  215. groupBarsLen--;
  216. }
  217. }
  218. }
  219. axis = chart.axes.get(boundYAxis);
  220. if (axis) {
  221. ends = axis.applyData();
  222. minY = ends.from;
  223. maxY = ends.to;
  224. }
  225. if (me.yField &amp;&amp; !Ext.isNumber(minY)) {
  226. out = me.getMinMaxYValues();
  227. minY = out[0];
  228. maxY = out[1];
  229. }
  230. if (!Ext.isNumber(minY)) {
  231. minY = 0;
  232. }
  233. if (!Ext.isNumber(maxY)) {
  234. maxY = 0;
  235. }
  236. scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (maxY - minY);
  237. shrunkBarWidth = barWidth;
  238. groupBarWidth = (barWidth / ((stacked ? 1 : groupBarsLen) * (groupGutter + 1) - groupGutter));
  239. if (barWidthProperty in me.style) {
  240. groupBarWidth = mmin(groupBarWidth, me.style[barWidthProperty]);
  241. shrunkBarWidth = groupBarWidth * ((stacked ? 1 : groupBarsLen) * (groupGutter + 1) - groupGutter);
  242. }
  243. zero = (column) ? bbox.y + bbox.height - yPadding : bbox.x + xPadding;
  244. if (stacked) {
  245. total = [[], []];
  246. for (i = 0, ln = data.length; i &lt; ln; i++) {
  247. record = data[i];
  248. total[0][i] = total[0][i] || 0;
  249. total[1][i] = total[1][i] || 0;
  250. for (j = 0; j &lt; barsLen; j++) {
  251. if (me.__excludes &amp;&amp; me.__excludes[j]) {
  252. continue;
  253. }
  254. rec = record.get(bars[j]);
  255. total[+(rec &gt; 0)][i] += mabs(rec);
  256. }
  257. }
  258. total[+(maxY &gt; 0)].push(mabs(maxY));
  259. total[+(minY &gt; 0)].push(mabs(minY));
  260. minus = mmax.apply(math, total[0]);
  261. plus = mmax.apply(math, total[1]);
  262. scale = (column ? bbox.height - yPadding * 2 : bbox.width - xPadding * 2) / (plus + minus);
  263. zero = zero + minus * scale * (column ? -1 : 1);
  264. }
  265. else if (minY / maxY &lt; 0) {
  266. zero = zero - minY * scale * (column ? -1 : 1);
  267. }
  268. return {
  269. bars: bars,
  270. bbox: bbox,
  271. shrunkBarWidth: shrunkBarWidth,
  272. barsLen: barsLen,
  273. groupBarsLen: groupBarsLen,
  274. barWidth: barWidth,
  275. groupBarWidth: groupBarWidth,
  276. scale: scale,
  277. zero: zero,
  278. xPadding: xPadding,
  279. yPadding: yPadding,
  280. signed: minY / maxY &lt; 0,
  281. minY: minY,
  282. maxY: maxY
  283. };
  284. },
  285. // @private Build an array of paths for the chart
  286. getPaths: function() {
  287. var me = this,
  288. chart = me.chart,
  289. store = chart.getChartStore(),
  290. data = store.data.items,
  291. i, total, record,
  292. bounds = me.bounds = me.getBounds(),
  293. items = me.items = [],
  294. yFields = me.yField,
  295. gutter = me.gutter / 100,
  296. groupGutter = me.groupGutter / 100,
  297. animate = chart.animate,
  298. column = me.column,
  299. group = me.group,
  300. enableShadows = chart.shadow,
  301. shadowGroups = me.shadowGroups,
  302. shadowAttributes = me.shadowAttributes,
  303. shadowGroupsLn = shadowGroups.length,
  304. bbox = bounds.bbox,
  305. barWidth = bounds.barWidth,
  306. shrunkBarWidth = bounds.shrunkBarWidth,
  307. xPadding = me.xPadding,
  308. yPadding = me.yPadding,
  309. stacked = me.stacked,
  310. barsLen = bounds.barsLen,
  311. colors = me.colorArrayStyle,
  312. colorLength = colors &amp;&amp; colors.length || 0,
  313. math = Math,
  314. mmax = math.max,
  315. mmin = math.min,
  316. mabs = math.abs,
  317. j, yValue, height, totalDim, totalNegDim, bottom, top, hasShadow, barAttr, attrs, counter,
  318. shadowIndex, shadow, sprite, offset, floorY;
  319. for (i = 0, total = data.length; i &lt; total; i++) {
  320. record = data[i];
  321. bottom = bounds.zero;
  322. top = bounds.zero;
  323. totalDim = 0;
  324. totalNegDim = 0;
  325. hasShadow = false;
  326. for (j = 0, counter = 0; j &lt; barsLen; j++) {
  327. // Excluded series
  328. if (me.__excludes &amp;&amp; me.__excludes[j]) {
  329. continue;
  330. }
  331. yValue = record.get(bounds.bars[j]);
  332. height = Math.round((yValue - mmax(bounds.minY, 0)) * bounds.scale);
  333. barAttr = {
  334. fill: colors[(barsLen &gt; 1 ? j : 0) % colorLength]
  335. };
  336. if (column) {
  337. Ext.apply(barAttr, {
  338. height: height,
  339. width: mmax(bounds.groupBarWidth, 0),
  340. x: (bbox.x + xPadding + (barWidth - shrunkBarWidth) * 0.5 + i * barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked),
  341. y: bottom - height
  342. });
  343. }
  344. else {
  345. // draw in reverse order
  346. offset = (total - 1) - i;
  347. Ext.apply(barAttr, {
  348. height: mmax(bounds.groupBarWidth, 0),
  349. width: height + (bottom == bounds.zero),
  350. x: bottom + (bottom != bounds.zero),
  351. y: (bbox.y + yPadding + (barWidth - shrunkBarWidth) * 0.5 + offset * barWidth * (1 + gutter) + counter * bounds.groupBarWidth * (1 + groupGutter) * !stacked + 1)
  352. });
  353. }
  354. if (height &lt; 0) {
  355. if (column) {
  356. barAttr.y = top;
  357. barAttr.height = mabs(height);
  358. } else {
  359. barAttr.x = top + height;
  360. barAttr.width = mabs(height);
  361. }
  362. }
  363. if (stacked) {
  364. if (height &lt; 0) {
  365. top += height * (column ? -1 : 1);
  366. } else {
  367. bottom += height * (column ? -1 : 1);
  368. }
  369. totalDim += mabs(height);
  370. if (height &lt; 0) {
  371. totalNegDim += mabs(height);
  372. }
  373. }
  374. barAttr.x = Math.floor(barAttr.x) + 1;
  375. floorY = Math.floor(barAttr.y);
  376. if (!Ext.isIE9 &amp;&amp; barAttr.y &gt; floorY) {
  377. floorY--;
  378. }
  379. barAttr.y = floorY;
  380. barAttr.width = Math.floor(barAttr.width);
  381. barAttr.height = Math.floor(barAttr.height);
  382. items.push({
  383. series: me,
  384. yField: yFields[j],
  385. storeItem: record,
  386. value: [record.get(me.xField), yValue],
  387. attr: barAttr,
  388. point: column ? [barAttr.x + barAttr.width / 2, yValue &gt;= 0 ? barAttr.y : barAttr.y + barAttr.height] :
  389. [yValue &gt;= 0 ? barAttr.x + barAttr.width : barAttr.x, barAttr.y + barAttr.height / 2]
  390. });
  391. // When resizing, reset before animating
  392. if (animate &amp;&amp; chart.resizing) {
  393. attrs = column ? {
  394. x: barAttr.x,
  395. y: bounds.zero,
  396. width: barAttr.width,
  397. height: 0
  398. } : {
  399. x: bounds.zero,
  400. y: barAttr.y,
  401. width: 0,
  402. height: barAttr.height
  403. };
  404. if (enableShadows &amp;&amp; (stacked &amp;&amp; !hasShadow || !stacked)) {
  405. hasShadow = true;
  406. //update shadows
  407. for (shadowIndex = 0; shadowIndex &lt; shadowGroupsLn; shadowIndex++) {
  408. shadow = shadowGroups[shadowIndex].getAt(stacked ? i : (i * barsLen + j));
  409. if (shadow) {
  410. shadow.setAttributes(attrs, true);
  411. }
  412. }
  413. }
  414. //update sprite position and width/height
  415. sprite = group.getAt(i * barsLen + j);
  416. if (sprite) {
  417. sprite.setAttributes(attrs, true);
  418. }
  419. }
  420. counter++;
  421. }
  422. if (stacked &amp;&amp; items.length) {
  423. items[i * counter].totalDim = totalDim;
  424. items[i * counter].totalNegDim = totalNegDim;
  425. }
  426. }
  427. if (stacked &amp;&amp; counter == 0) {
  428. // Remove ghost shadow ref: EXTJSIV-5982
  429. for (i = 0, total = data.length; i &lt; total; i++) {
  430. for (shadowIndex = 0; shadowIndex &lt; shadowGroupsLn; shadowIndex++) {
  431. shadow = shadowGroups[shadowIndex].getAt(i);
  432. if (shadow) {
  433. shadow.hide(true);
  434. }
  435. }
  436. }
  437. }
  438. },
  439. // @private render/setAttributes on the shadows
  440. renderShadows: function(i, barAttr, baseAttrs, bounds) {
  441. var me = this,
  442. chart = me.chart,
  443. surface = chart.surface,
  444. animate = chart.animate,
  445. stacked = me.stacked,
  446. shadowGroups = me.shadowGroups,
  447. shadowAttributes = me.shadowAttributes,
  448. shadowGroupsLn = shadowGroups.length,
  449. store = chart.getChartStore(),
  450. column = me.column,
  451. items = me.items,
  452. shadows = [],
  453. zero = bounds.zero,
  454. shadowIndex, shadowBarAttr, shadow, totalDim, totalNegDim, j, rendererAttributes;
  455. if ((stacked &amp;&amp; (i % bounds.groupBarsLen === 0)) || !stacked) {
  456. j = i / bounds.groupBarsLen;
  457. //create shadows
  458. for (shadowIndex = 0; shadowIndex &lt; shadowGroupsLn; shadowIndex++) {
  459. shadowBarAttr = Ext.apply({}, shadowAttributes[shadowIndex]);
  460. shadow = shadowGroups[shadowIndex].getAt(stacked ? j : i);
  461. Ext.copyTo(shadowBarAttr, barAttr, 'x,y,width,height');
  462. if (!shadow) {
  463. shadow = surface.add(Ext.apply({
  464. type: 'rect',
  465. group: shadowGroups[shadowIndex]
  466. }, Ext.apply({}, baseAttrs, shadowBarAttr)));
  467. }
  468. if (stacked) {
  469. totalDim = items[i].totalDim;
  470. totalNegDim = items[i].totalNegDim;
  471. if (column) {
  472. shadowBarAttr.y = zero + totalNegDim - totalDim - 1;
  473. shadowBarAttr.height = totalDim;
  474. }
  475. else {
  476. shadowBarAttr.x = zero - totalNegDim;
  477. shadowBarAttr.width = totalDim;
  478. }
  479. }
  480. rendererAttributes = me.renderer(shadow, store.getAt(j), shadowBarAttr, i, store);
  481. rendererAttributes.hidden = !!barAttr.hidden;
  482. if (animate) {
  483. me.onAnimate(shadow, { to: rendererAttributes });
  484. }
  485. else {
  486. shadow.setAttributes(rendererAttributes, true);
  487. }
  488. shadows.push(shadow);
  489. }
  490. }
  491. return shadows;
  492. },
  493. <span id='Ext-chart-series-Bar-method-drawSeries'> /**
  494. </span> * Draws the series for the current chart.
  495. */
  496. drawSeries: function() {
  497. var me = this,
  498. chart = me.chart,
  499. store = chart.getChartStore(),
  500. surface = chart.surface,
  501. animate = chart.animate,
  502. stacked = me.stacked,
  503. column = me.column,
  504. enableShadows = chart.shadow,
  505. shadowGroups = me.shadowGroups,
  506. shadowGroupsLn = shadowGroups.length,
  507. group = me.group,
  508. seriesStyle = me.seriesStyle,
  509. items, ln, i, j, baseAttrs, sprite, rendererAttributes, shadowIndex, shadowGroup,
  510. bounds, endSeriesStyle, barAttr, attrs, anim;
  511. if (!store || !store.getCount() || me.seriesIsHidden) {
  512. me.hide();
  513. me.items = [];
  514. return;
  515. }
  516. //fill colors are taken from the colors array.
  517. endSeriesStyle = Ext.apply({}, this.style, seriesStyle);
  518. delete endSeriesStyle.fill;
  519. delete endSeriesStyle.x;
  520. delete endSeriesStyle.y;
  521. delete endSeriesStyle.width;
  522. delete endSeriesStyle.height;
  523. me.unHighlightItem();
  524. me.cleanHighlights();
  525. me.getPaths();
  526. bounds = me.bounds;
  527. items = me.items;
  528. baseAttrs = column ? {
  529. y: bounds.zero,
  530. height: 0
  531. } : {
  532. x: bounds.zero,
  533. width: 0
  534. };
  535. ln = items.length;
  536. // Create new or reuse sprites and animate/display
  537. for (i = 0; i &lt; ln; i++) {
  538. sprite = group.getAt(i);
  539. barAttr = items[i].attr;
  540. if (enableShadows) {
  541. items[i].shadows = me.renderShadows(i, barAttr, baseAttrs, bounds);
  542. }
  543. // Create a new sprite if needed (no height)
  544. if (!sprite) {
  545. attrs = Ext.apply({}, baseAttrs, barAttr);
  546. attrs = Ext.apply(attrs, endSeriesStyle || {});
  547. sprite = surface.add(Ext.apply({}, {
  548. type: 'rect',
  549. group: group
  550. }, attrs));
  551. }
  552. if (animate) {
  553. rendererAttributes = me.renderer(sprite, store.getAt(i), barAttr, i, store);
  554. sprite._to = rendererAttributes;
  555. anim = me.onAnimate(sprite, { to: Ext.apply(rendererAttributes, endSeriesStyle) });
  556. if (enableShadows &amp;&amp; stacked &amp;&amp; (i % bounds.barsLen === 0)) {
  557. j = i / bounds.barsLen;
  558. for (shadowIndex = 0; shadowIndex &lt; shadowGroupsLn; shadowIndex++) {
  559. anim.on('afteranimate', function() {
  560. this.show(true);
  561. }, shadowGroups[shadowIndex].getAt(j));
  562. }
  563. }
  564. }
  565. else {
  566. rendererAttributes = me.renderer(sprite, store.getAt(i), Ext.apply(barAttr, { hidden: false }), i, store);
  567. sprite.setAttributes(Ext.apply(rendererAttributes, endSeriesStyle), true);
  568. }
  569. items[i].sprite = sprite;
  570. }
  571. // Hide unused sprites
  572. ln = group.getCount();
  573. for (j = i; j &lt; ln; j++) {
  574. group.getAt(j).hide(true);
  575. }
  576. if (me.stacked) {
  577. // If stacked, we have only store.getCount() shadows.
  578. i = store.getCount();
  579. }
  580. // Hide unused shadows
  581. if (enableShadows) {
  582. for (shadowIndex = 0; shadowIndex &lt; shadowGroupsLn; shadowIndex++) {
  583. shadowGroup = shadowGroups[shadowIndex];
  584. ln = shadowGroup.getCount();
  585. for (j = i; j &lt; ln; j++) {
  586. shadowGroup.getAt(j).hide(true);
  587. }
  588. }
  589. }
  590. me.renderLabels();
  591. },
  592. // @private handled when creating a label.
  593. onCreateLabel: function(storeItem, item, i, display) {
  594. var me = this,
  595. surface = me.chart.surface,
  596. group = me.labelsGroup,
  597. config = me.label,
  598. endLabelStyle = Ext.apply({}, config, me.seriesLabelStyle || {}),
  599. sprite;
  600. return surface.add(Ext.apply({
  601. type: 'text',
  602. group: group
  603. }, endLabelStyle || {}));
  604. },
  605. // @private callback used when placing a label.
  606. onPlaceLabel: function(label, storeItem, item, i, display, animate, j, index) {
  607. // Determine the label's final position. Starts with the configured preferred value but
  608. // may get flipped from inside to outside or vice-versa depending on space.
  609. var me = this,
  610. opt = me.bounds,
  611. groupBarWidth = opt.groupBarWidth,
  612. column = me.column,
  613. chart = me.chart,
  614. chartBBox = chart.chartBBox,
  615. resizing = chart.resizing,
  616. xValue = item.value[0],
  617. yValue = item.value[1],
  618. attr = item.attr,
  619. config = me.label,
  620. rotate = config.orientation == 'vertical',
  621. field = [].concat(config.field),
  622. format = config.renderer,
  623. text = format(storeItem.get(field[index])),
  624. size = me.getLabelSize(text),
  625. width = size.width,
  626. height = size.height,
  627. zero = opt.zero,
  628. outside = 'outside',
  629. insideStart = 'insideStart',
  630. insideEnd = 'insideEnd',
  631. offsetX = 10,
  632. offsetY = 6,
  633. signed = opt.signed,
  634. x, y, finalAttr;
  635. label.setAttributes({
  636. text: text
  637. });
  638. label.isOutside = false;
  639. if (column) {
  640. if (display == outside) {
  641. if (height + offsetY + attr.height &gt; (yValue &gt;= 0 ? zero - chartBBox.y : chartBBox.y + chartBBox.height - zero)) {
  642. display = insideEnd;
  643. }
  644. } else {
  645. if (height + offsetY &gt; attr.height) {
  646. display = outside;
  647. label.isOutside = true;
  648. }
  649. }
  650. x = attr.x + groupBarWidth / 2;
  651. y = display == insideStart ?
  652. (zero + ((height / 2 + 3) * (yValue &gt;= 0 ? -1 : 1))) :
  653. (yValue &gt;= 0 ? (attr.y + ((height / 2 + 3) * (display == outside ? -1 : 1))) :
  654. (attr.y + attr.height + ((height / 2 + 3) * (display === outside ? 1 : -1))));
  655. }
  656. else {
  657. if (display == outside) {
  658. if (width + offsetX + attr.width &gt; (yValue &gt;= 0 ? chartBBox.x + chartBBox.width - zero : zero - chartBBox.x)) {
  659. display = insideEnd;
  660. }
  661. }
  662. else {
  663. if (width + offsetX &gt; attr.width) {
  664. display = outside;
  665. label.isOutside = true;
  666. }
  667. }
  668. x = display == insideStart ?
  669. (zero + ((width / 2 + 5) * (yValue &gt;= 0 ? 1 : -1))) :
  670. (yValue &gt;= 0 ? (attr.x + attr.width + ((width / 2 + 5) * (display === outside ? 1 : -1))) :
  671. (attr.x + ((width / 2 + 5) * (display === outside ? -1 : 1))));
  672. y = attr.y + groupBarWidth / 2;
  673. }
  674. //set position
  675. finalAttr = {
  676. x: x,
  677. y: y
  678. };
  679. //rotate
  680. if (rotate) {
  681. finalAttr.rotate = {
  682. x: x,
  683. y: y,
  684. degrees: 270
  685. };
  686. }
  687. //check for resizing
  688. if (animate &amp;&amp; resizing) {
  689. if (column) {
  690. x = attr.x + attr.width / 2;
  691. y = zero;
  692. } else {
  693. x = zero;
  694. y = attr.y + attr.height / 2;
  695. }
  696. label.setAttributes({
  697. x: x,
  698. y: y
  699. }, true);
  700. if (rotate) {
  701. label.setAttributes({
  702. rotate: {
  703. x: x,
  704. y: y,
  705. degrees: 270
  706. }
  707. }, true);
  708. }
  709. }
  710. //handle animation
  711. if (animate) {
  712. me.onAnimate(label, { to: finalAttr });
  713. }
  714. else {
  715. label.setAttributes(Ext.apply(finalAttr, {
  716. hidden: false
  717. }), true);
  718. }
  719. },
  720. /* @private
  721. * Gets the dimensions of a given bar label. Uses a single hidden sprite to avoid
  722. * changing visible sprites.
  723. * @param value
  724. */
  725. getLabelSize: function(value) {
  726. var tester = this.testerLabel,
  727. config = this.label,
  728. endLabelStyle = Ext.apply({}, config, this.seriesLabelStyle || {}),
  729. rotated = config.orientation === 'vertical',
  730. bbox, w, h,
  731. undef;
  732. if (!tester) {
  733. tester = this.testerLabel = this.chart.surface.add(Ext.apply({
  734. type: 'text',
  735. opacity: 0
  736. }, endLabelStyle));
  737. }
  738. tester.setAttributes({
  739. text: value
  740. }, true);
  741. // Flip the width/height if rotated, as getBBox returns the pre-rotated dimensions
  742. bbox = tester.getBBox();
  743. w = bbox.width;
  744. h = bbox.height;
  745. return {
  746. width: rotated ? h : w,
  747. height: rotated ? w : h
  748. };
  749. },
  750. // @private used to animate label, markers and other sprites.
  751. onAnimate: function(sprite, attr) {
  752. sprite.show();
  753. return this.callParent(arguments);
  754. },
  755. isItemInPoint: function(x, y, item) {
  756. var bbox = item.sprite.getBBox();
  757. return bbox.x &lt;= x &amp;&amp; bbox.y &lt;= y
  758. &amp;&amp; (bbox.x + bbox.width) &gt;= x
  759. &amp;&amp; (bbox.y + bbox.height) &gt;= y;
  760. },
  761. // @private hide all markers
  762. hideAll: function(index) {
  763. var axes = this.chart.axes,
  764. axesItems = axes.items,
  765. ln = axesItems.length,
  766. i = 0;
  767. index = (isNaN(this._index) ? index : this._index) || 0;
  768. if (!this.__excludes) {
  769. this.__excludes = [];
  770. }
  771. this.__excludes[index] = true;
  772. this.drawSeries();
  773. for (i; i &lt; ln; i++) {
  774. axesItems[i].drawAxis();
  775. }
  776. },
  777. // @private show all markers
  778. showAll: function(index) {
  779. var axes = this.chart.axes,
  780. axesItems = axes.items,
  781. ln = axesItems.length,
  782. i = 0;
  783. index = (isNaN(this._index) ? index : this._index) || 0;
  784. if (!this.__excludes) {
  785. this.__excludes = [];
  786. }
  787. this.__excludes[index] = false;
  788. this.drawSeries();
  789. for (i; i &lt; ln; i++) {
  790. axesItems[i].drawAxis();
  791. }
  792. },
  793. <span id='Ext-chart-series-Bar-method-getLegendColor'> /**
  794. </span> * Returns a string with the color to be used for the series legend item.
  795. * @param index
  796. */
  797. getLegendColor: function(index) {
  798. var me = this,
  799. colorLength = me.colorArrayStyle.length;
  800. if (me.style &amp;&amp; me.style.fill) {
  801. return me.style.fill;
  802. } else {
  803. return me.colorArrayStyle[index % colorLength];
  804. }
  805. },
  806. highlightItem: function(item) {
  807. this.callParent(arguments);
  808. this.renderLabels();
  809. },
  810. unHighlightItem: function() {
  811. this.callParent(arguments);
  812. this.renderLabels();
  813. },
  814. cleanHighlights: function() {
  815. this.callParent(arguments);
  816. this.renderLabels();
  817. }
  818. });
  819. </pre>
  820. </body>
  821. </html>