Web UI for ARGUS server
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.

398 lines
14 KiB

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