You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
14 KiB

2 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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-grid-PivotGrid'>/**
  19. </span> * @class Ext.grid.PivotGrid
  20. * @extends Ext.grid.GridPanel
  21. * &lt;p&gt;The PivotGrid component enables rapid summarization of large data sets. It provides a way to reduce a large set of
  22. * data down into a format where trends and insights become more apparent. A classic example is in sales data; a company
  23. * will often have a record of all sales it makes for a given period - this will often encompass thousands of rows of
  24. * data. The PivotGrid allows you to see how well each salesperson performed, which cities generate the most revenue,
  25. * how products perform between cities and so on.&lt;/p&gt;
  26. * &lt;p&gt;A PivotGrid is composed of two axes (left and top), one {@link #measure} and one {@link #aggregator aggregation}
  27. * function. Each axis can contain one or more {@link #dimension}, which are ordered into a hierarchy. Dimensions on the
  28. * left axis can also specify a width. Each dimension in each axis can specify its sort ordering, defaulting to &quot;ASC&quot;,
  29. * and must specify one of the fields in the {@link Ext.data.Record Record} used by the PivotGrid's
  30. * {@link Ext.data.Store Store}.&lt;/p&gt;
  31. &lt;pre&gt;&lt;code&gt;
  32. // This is the record representing a single sale
  33. var SaleRecord = Ext.data.Record.create([
  34. {name: 'person', type: 'string'},
  35. {name: 'product', type: 'string'},
  36. {name: 'city', type: 'string'},
  37. {name: 'state', type: 'string'},
  38. {name: 'year', type: 'int'},
  39. {name: 'value', type: 'int'}
  40. ]);
  41. // A simple store that loads SaleRecord data from a url
  42. var myStore = new Ext.data.Store({
  43. url: 'data.json',
  44. autoLoad: true,
  45. reader: new Ext.data.JsonReader({
  46. root: 'rows',
  47. idProperty: 'id'
  48. }, SaleRecord)
  49. });
  50. // Create the PivotGrid itself, referencing the store
  51. var pivot = new Ext.grid.PivotGrid({
  52. store : myStore,
  53. aggregator: 'sum',
  54. measure : 'value',
  55. leftAxis: [
  56. {
  57. width: 60,
  58. dataIndex: 'product'
  59. },
  60. {
  61. width: 120,
  62. dataIndex: 'person',
  63. direction: 'DESC'
  64. }
  65. ],
  66. topAxis: [
  67. {
  68. dataIndex: 'year'
  69. }
  70. ]
  71. });
  72. &lt;/code&gt;&lt;/pre&gt;
  73. * &lt;p&gt;The specified {@link #measure} is the field from SaleRecord that is extracted from each combination
  74. * of product and person (on the left axis) and year on the top axis. There may be several SaleRecords in the
  75. * data set that share this combination, so an array of measure fields is produced. This array is then
  76. * aggregated using the {@link #aggregator} function.&lt;/p&gt;
  77. * &lt;p&gt;The default aggregator function is sum, which simply adds up all of the extracted measure values. Other
  78. * built-in aggregator functions are count, avg, min and max. In addition, you can specify your own function.
  79. * In this example we show the code used to sum the measures, but you can return any value you like. See
  80. * {@link #aggregator} for more details.&lt;/p&gt;
  81. &lt;pre&gt;&lt;code&gt;
  82. new Ext.grid.PivotGrid({
  83. aggregator: function(records, measure) {
  84. var length = records.length,
  85. total = 0,
  86. i;
  87. for (i = 0; i &lt; length; i++) {
  88. total += records[i].get(measure);
  89. }
  90. return total;
  91. },
  92. renderer: function(value) {
  93. return Math.round(value);
  94. },
  95. //your normal config here
  96. });
  97. &lt;/code&gt;&lt;/pre&gt;
  98. * &lt;p&gt;&lt;u&gt;Renderers&lt;/u&gt;&lt;/p&gt;
  99. * &lt;p&gt;PivotGrid optionally accepts a {@link #renderer} function which can modify the data in each cell before it
  100. * is rendered. The renderer is passed the value that would usually be placed in the cell and is expected to return
  101. * the new value. For example let's imagine we had height data expressed as a decimal - here's how we might use a
  102. * renderer to display the data in feet and inches notation:&lt;/p&gt;
  103. &lt;pre&gt;&lt;code&gt;
  104. new Ext.grid.PivotGrid({
  105. //in each case the value is a decimal number of feet
  106. renderer : function(value) {
  107. var feet = Math.floor(value),
  108. inches = Math.round((value - feet) * 12);
  109. return String.format(&quot;{0}' {1}\&quot;&quot;, feet, inches);
  110. },
  111. //normal config here
  112. });
  113. &lt;/code&gt;&lt;/pre&gt;
  114. * &lt;p&gt;&lt;u&gt;Reconfiguring&lt;/u&gt;&lt;/p&gt;
  115. * &lt;p&gt;All aspects PivotGrid's configuration can be updated at runtime. It is easy to change the {@link #setMeasure measure},
  116. * {@link #setAggregator aggregation function}, {@link #setLeftAxis left} and {@link #setTopAxis top} axes and refresh the grid.&lt;/p&gt;
  117. * &lt;p&gt;In this case we reconfigure the PivotGrid to have city and year as the top axis dimensions, rendering the average sale
  118. * value into the cells:&lt;/p&gt;
  119. &lt;pre&gt;&lt;code&gt;
  120. //the left axis can also be changed
  121. pivot.topAxis.setDimensions([
  122. {dataIndex: 'city', direction: 'DESC'},
  123. {dataIndex: 'year', direction: 'ASC'}
  124. ]);
  125. pivot.setMeasure('value');
  126. pivot.setAggregator('avg');
  127. pivot.view.refresh(true);
  128. &lt;/code&gt;&lt;/pre&gt;
  129. * &lt;p&gt;See the {@link Ext.grid.PivotAxis PivotAxis} documentation for further detail on reconfiguring axes.&lt;/p&gt;
  130. */
  131. Ext.grid.PivotGrid = Ext.extend(Ext.grid.GridPanel, {
  132. <span id='Ext-grid-PivotGrid-cfg-aggregator'> /**
  133. </span> * @cfg {String|Function} aggregator The aggregation function to use to combine the measures extracted
  134. * for each dimension combination. Can be any of the built-in aggregators (sum, count, avg, min, max).
  135. * Can also be a function which accepts two arguments (an array of Records to aggregate, and the measure
  136. * to aggregate them on) and should return a String.
  137. */
  138. aggregator: 'sum',
  139. <span id='Ext-grid-PivotGrid-cfg-renderer'> /**
  140. </span> * @cfg {Function} renderer Optional renderer to pass values through before they are rendered to the dom. This
  141. * gives an opportunity to modify cell contents after the value has been computed.
  142. */
  143. renderer: undefined,
  144. <span id='Ext-grid-PivotGrid-cfg-measure'> /**
  145. </span> * @cfg {String} measure The field to extract from each Record when pivoting around the two axes. See the class
  146. * introduction docs for usage
  147. */
  148. <span id='Ext-grid-PivotGrid-cfg-leftAxis'> /**
  149. </span> * @cfg {Array|Ext.grid.PivotAxis} leftAxis Either and array of {@link #dimension} to use on the left axis, or
  150. * a {@link Ext.grid.PivotAxis} instance. If an array is passed, it is turned into a PivotAxis internally.
  151. */
  152. <span id='Ext-grid-PivotGrid-cfg-topAxis'> /**
  153. </span> * @cfg {Array|Ext.grid.PivotAxis} topAxis Either and array of {@link #dimension} to use on the top axis, or
  154. * a {@link Ext.grid.PivotAxis} instance. If an array is passed, it is turned into a PivotAxis internally.
  155. */
  156. <span id='Ext-grid-PivotGrid-method-initComponent'> //inherit docs
  157. </span> initComponent: function() {
  158. Ext.grid.PivotGrid.superclass.initComponent.apply(this, arguments);
  159. this.initAxes();
  160. //no resizing of columns is allowed yet in PivotGrid
  161. this.enableColumnResize = false;
  162. this.viewConfig = Ext.apply(this.viewConfig || {}, {
  163. forceFit: true
  164. });
  165. //TODO: dummy col model that is never used - GridView is too tightly integrated with ColumnModel
  166. //in 3.x to remove this altogether.
  167. this.colModel = new Ext.grid.ColumnModel({});
  168. },
  169. <span id='Ext-grid-PivotGrid-method-getAggregator'> /**
  170. </span> * Returns the function currently used to aggregate the records in each Pivot cell
  171. * @return {Function} The current aggregator function
  172. */
  173. getAggregator: function() {
  174. if (typeof this.aggregator == 'string') {
  175. return Ext.grid.PivotAggregatorMgr.types[this.aggregator];
  176. } else {
  177. return this.aggregator;
  178. }
  179. },
  180. <span id='Ext-grid-PivotGrid-method-setAggregator'> /**
  181. </span> * Sets the function to use when aggregating data for each cell.
  182. * @param {String|Function} aggregator The new aggregator function or named function string
  183. */
  184. setAggregator: function(aggregator) {
  185. this.aggregator = aggregator;
  186. },
  187. <span id='Ext-grid-PivotGrid-method-setMeasure'> /**
  188. </span> * Sets the field name to use as the Measure in this Pivot Grid
  189. * @param {String} measure The field to make the measure
  190. */
  191. setMeasure: function(measure) {
  192. this.measure = measure;
  193. },
  194. <span id='Ext-grid-PivotGrid-method-setLeftAxis'> /**
  195. </span> * Sets the left axis of this pivot grid. Optionally refreshes the grid afterwards.
  196. * @param {Ext.grid.PivotAxis} axis The pivot axis
  197. * @param {Boolean} refresh True to immediately refresh the grid and its axes (defaults to false)
  198. */
  199. setLeftAxis: function(axis, refresh) {
  200. <span id='Ext-grid-PivotGrid-property-leftAxis'> /**
  201. </span> * The configured {@link Ext.grid.PivotAxis} used as the left Axis for this Pivot Grid
  202. * @property leftAxis
  203. * @type Ext.grid.PivotAxis
  204. */
  205. this.leftAxis = axis;
  206. if (refresh) {
  207. this.view.refresh();
  208. }
  209. },
  210. <span id='Ext-grid-PivotGrid-method-setTopAxis'> /**
  211. </span> * Sets the top axis of this pivot grid. Optionally refreshes the grid afterwards.
  212. * @param {Ext.grid.PivotAxis} axis The pivot axis
  213. * @param {Boolean} refresh True to immediately refresh the grid and its axes (defaults to false)
  214. */
  215. setTopAxis: function(axis, refresh) {
  216. <span id='Ext-grid-PivotGrid-property-topAxis'> /**
  217. </span> * The configured {@link Ext.grid.PivotAxis} used as the top Axis for this Pivot Grid
  218. * @property topAxis
  219. * @type Ext.grid.PivotAxis
  220. */
  221. this.topAxis = axis;
  222. if (refresh) {
  223. this.view.refresh();
  224. }
  225. },
  226. <span id='Ext-grid-PivotGrid-method-initAxes'> /**
  227. </span> * @private
  228. * Creates the top and left axes. Should usually only need to be called once from initComponent
  229. */
  230. initAxes: function() {
  231. var PivotAxis = Ext.grid.PivotAxis;
  232. if (!(this.leftAxis instanceof PivotAxis)) {
  233. this.setLeftAxis(new PivotAxis({
  234. orientation: 'vertical',
  235. dimensions : this.leftAxis || [],
  236. store : this.store
  237. }));
  238. };
  239. if (!(this.topAxis instanceof PivotAxis)) {
  240. this.setTopAxis(new PivotAxis({
  241. orientation: 'horizontal',
  242. dimensions : this.topAxis || [],
  243. store : this.store
  244. }));
  245. };
  246. },
  247. <span id='Ext-grid-PivotGrid-method-extractData'> /**
  248. </span> * @private
  249. * @return {Array} 2-dimensional array of cell data
  250. */
  251. extractData: function() {
  252. var records = this.store.data.items,
  253. recCount = records.length,
  254. cells = [],
  255. record, i, j, k;
  256. if (recCount == 0) {
  257. return [];
  258. }
  259. var leftTuples = this.leftAxis.getTuples(),
  260. leftCount = leftTuples.length,
  261. topTuples = this.topAxis.getTuples(),
  262. topCount = topTuples.length,
  263. aggregator = this.getAggregator();
  264. for (i = 0; i &lt; recCount; i++) {
  265. record = records[i];
  266. for (j = 0; j &lt; leftCount; j++) {
  267. cells[j] = cells[j] || [];
  268. if (leftTuples[j].matcher(record) === true) {
  269. for (k = 0; k &lt; topCount; k++) {
  270. cells[j][k] = cells[j][k] || [];
  271. if (topTuples[k].matcher(record)) {
  272. cells[j][k].push(record);
  273. }
  274. }
  275. }
  276. }
  277. }
  278. var rowCount = cells.length,
  279. colCount, row;
  280. for (i = 0; i &lt; rowCount; i++) {
  281. row = cells[i];
  282. colCount = row.length;
  283. for (j = 0; j &lt; colCount; j++) {
  284. cells[i][j] = aggregator(cells[i][j], this.measure);
  285. }
  286. }
  287. return cells;
  288. },
  289. <span id='Ext-grid-PivotGrid-method-getView'> /**
  290. </span> * Returns the grid's GridView object.
  291. * @return {Ext.grid.PivotGridView} The grid view
  292. */
  293. getView: function() {
  294. if (!this.view) {
  295. this.view = new Ext.grid.PivotGridView(this.viewConfig);
  296. }
  297. return this.view;
  298. }
  299. });
  300. Ext.reg('pivotgrid', Ext.grid.PivotGrid);
  301. Ext.grid.PivotAggregatorMgr = new Ext.AbstractManager();
  302. Ext.grid.PivotAggregatorMgr.registerType('sum', function(records, measure) {
  303. var length = records.length,
  304. total = 0,
  305. i;
  306. for (i = 0; i &lt; length; i++) {
  307. total += records[i].get(measure);
  308. }
  309. return total;
  310. });
  311. Ext.grid.PivotAggregatorMgr.registerType('avg', function(records, measure) {
  312. var length = records.length,
  313. total = 0,
  314. i;
  315. for (i = 0; i &lt; length; i++) {
  316. total += records[i].get(measure);
  317. }
  318. return (total / length) || 'n/a';
  319. });
  320. Ext.grid.PivotAggregatorMgr.registerType('min', function(records, measure) {
  321. var data = [],
  322. length = records.length,
  323. i;
  324. for (i = 0; i &lt; length; i++) {
  325. data.push(records[i].get(measure));
  326. }
  327. return Math.min.apply(this, data) || 'n/a';
  328. });
  329. Ext.grid.PivotAggregatorMgr.registerType('max', function(records, measure) {
  330. var data = [],
  331. length = records.length,
  332. i;
  333. for (i = 0; i &lt; length; i++) {
  334. data.push(records[i].get(measure));
  335. }
  336. return Math.max.apply(this, data) || 'n/a';
  337. });
  338. Ext.grid.PivotAggregatorMgr.registerType('count', function(records, measure) {
  339. return records.length;
  340. });</pre>
  341. </body>
  342. </html>