UNPKG

13.5 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, "__esModule", {
4 value: true
5});
6
7var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }();
8
9var _react = require('react');
10
11var _react2 = _interopRequireDefault(_react);
12
13var _propTypes = require('prop-types');
14
15var _propTypes2 = _interopRequireDefault(_propTypes);
16
17var _Utilities = require('./Utilities');
18
19function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
20
21function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
22
23function _possibleConstructorReturn(self, call) { if (!self) { throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); } return call && (typeof call === "object" || typeof call === "function") ? call : self; }
24
25function _inherits(subClass, superClass) { if (typeof superClass !== "function" && superClass !== null) { throw new TypeError("Super expression must either be null or a function, not " + typeof superClass); } subClass.prototype = Object.create(superClass && superClass.prototype, { constructor: { value: subClass, enumerable: false, writable: true, configurable: true } }); if (superClass) Object.setPrototypeOf ? Object.setPrototypeOf(subClass, superClass) : subClass.__proto__ = superClass; }
26
27// helper function for setting row/col-span in pivotTableRenderer
28var spanSize = function spanSize(arr, i, j) {
29 var x = void 0;
30 if (i !== 0) {
31 var asc = void 0,
32 end = void 0;
33 var noDraw = true;
34 for (x = 0, end = j, asc = end >= 0; asc ? x <= end : x >= end; asc ? x++ : x--) {
35 if (arr[i - 1][x] !== arr[i][x]) {
36 noDraw = false;
37 }
38 }
39 if (noDraw) {
40 return -1;
41 }
42 }
43 var len = 0;
44 while (i + len < arr.length) {
45 var asc1 = void 0,
46 end1 = void 0;
47 var stop = false;
48 for (x = 0, end1 = j, asc1 = end1 >= 0; asc1 ? x <= end1 : x >= end1; asc1 ? x++ : x--) {
49 if (arr[i][x] !== arr[i + len][x]) {
50 stop = true;
51 }
52 }
53 if (stop) {
54 break;
55 }
56 len++;
57 }
58 return len;
59};
60
61function redColorScaleGenerator(values) {
62 var min = Math.min.apply(Math, values);
63 var max = Math.max.apply(Math, values);
64 return function (x) {
65 // eslint-disable-next-line no-magic-numbers
66 var nonRed = 255 - Math.round(255 * (x - min) / (max - min));
67 return { backgroundColor: 'rgb(255,' + nonRed + ',' + nonRed + ')' };
68 };
69}
70
71function makeRenderer() {
72 var opts = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
73
74 var TableRenderer = function (_React$PureComponent) {
75 _inherits(TableRenderer, _React$PureComponent);
76
77 function TableRenderer() {
78 _classCallCheck(this, TableRenderer);
79
80 return _possibleConstructorReturn(this, (TableRenderer.__proto__ || Object.getPrototypeOf(TableRenderer)).apply(this, arguments));
81 }
82
83 _createClass(TableRenderer, [{
84 key: 'render',
85 value: function render() {
86 var pivotData = new _Utilities.PivotData(this.props);
87 var colAttrs = pivotData.props.cols;
88 var rowAttrs = pivotData.props.rows;
89 var rowKeys = pivotData.getRowKeys();
90 var colKeys = pivotData.getColKeys();
91 var grandTotalAggregator = pivotData.getAggregator([], []);
92
93 var valueCellColors = function valueCellColors() {};
94 var rowTotalColors = function rowTotalColors() {};
95 var colTotalColors = function colTotalColors() {};
96 if (opts.heatmapMode) {
97 var colorScaleGenerator = this.props.tableColorScaleGenerator;
98 var rowTotalValues = colKeys.map(function (x) {
99 return pivotData.getAggregator([], x).value();
100 });
101 rowTotalColors = colorScaleGenerator(rowTotalValues);
102 var colTotalValues = rowKeys.map(function (x) {
103 return pivotData.getAggregator(x, []).value();
104 });
105 colTotalColors = colorScaleGenerator(colTotalValues);
106
107 if (opts.heatmapMode === 'full') {
108 var allValues = [];
109 rowKeys.map(function (r) {
110 return colKeys.map(function (c) {
111 return allValues.push(pivotData.getAggregator(r, c).value());
112 });
113 });
114 var colorScale = colorScaleGenerator(allValues);
115 valueCellColors = function valueCellColors(r, c, v) {
116 return colorScale(v);
117 };
118 } else if (opts.heatmapMode === 'row') {
119 var rowColorScales = {};
120 rowKeys.map(function (r) {
121 var rowValues = colKeys.map(function (x) {
122 return pivotData.getAggregator(r, x).value();
123 });
124 rowColorScales[r] = colorScaleGenerator(rowValues);
125 });
126 valueCellColors = function valueCellColors(r, c, v) {
127 return rowColorScales[r](v);
128 };
129 } else if (opts.heatmapMode === 'col') {
130 var colColorScales = {};
131 colKeys.map(function (c) {
132 var colValues = rowKeys.map(function (x) {
133 return pivotData.getAggregator(x, c).value();
134 });
135 colColorScales[c] = colorScaleGenerator(colValues);
136 });
137 valueCellColors = function valueCellColors(r, c, v) {
138 return colColorScales[c](v);
139 };
140 }
141 }
142
143 return _react2.default.createElement(
144 'table',
145 { className: 'pvtTable' },
146 _react2.default.createElement(
147 'thead',
148 null,
149 colAttrs.map(function (c, j) {
150 return _react2.default.createElement(
151 'tr',
152 { key: 'colAttr' + j },
153 j === 0 && rowAttrs.length !== 0 && _react2.default.createElement('th', { colSpan: rowAttrs.length, rowSpan: colAttrs.length }),
154 _react2.default.createElement(
155 'th',
156 { className: 'pvtAxisLabel' },
157 c
158 ),
159 colKeys.map(function (colKey, i) {
160 var x = spanSize(colKeys, i, j);
161 if (x === -1) {
162 return null;
163 }
164 return _react2.default.createElement(
165 'th',
166 {
167 className: 'pvtColLabel',
168 key: 'colKey' + i,
169 colSpan: x,
170 rowSpan: j === colAttrs.length - 1 && rowAttrs.length !== 0 ? 2 : 1
171 },
172 colKey[j]
173 );
174 }),
175 j === 0 && _react2.default.createElement(
176 'th',
177 {
178 className: 'pvtTotalLabel',
179 rowSpan: colAttrs.length + (rowAttrs.length === 0 ? 0 : 1)
180 },
181 'Totals'
182 )
183 );
184 }),
185 rowAttrs.length !== 0 && _react2.default.createElement(
186 'tr',
187 null,
188 rowAttrs.map(function (r, i) {
189 return _react2.default.createElement(
190 'th',
191 { className: 'pvtAxisLabel', key: 'rowAttr' + i },
192 r
193 );
194 }),
195 _react2.default.createElement(
196 'th',
197 { className: 'pvtTotalLabel' },
198 colAttrs.length === 0 ? 'Totals' : null
199 )
200 )
201 ),
202 _react2.default.createElement(
203 'tbody',
204 null,
205 rowKeys.map(function (rowKey, i) {
206 var totalAggregator = pivotData.getAggregator(rowKey, []);
207 return _react2.default.createElement(
208 'tr',
209 { key: 'rowKeyRow' + i },
210 rowKey.map(function (txt, j) {
211 var x = spanSize(rowKeys, i, j);
212 if (x === -1) {
213 return null;
214 }
215 return _react2.default.createElement(
216 'th',
217 {
218 key: 'rowKeyLabel' + i + '-' + j,
219 className: 'pvtRowLabel',
220 rowSpan: x,
221 colSpan: j === rowAttrs.length - 1 && colAttrs.length !== 0 ? 2 : 1
222 },
223 txt
224 );
225 }),
226 colKeys.map(function (colKey, j) {
227 var aggregator = pivotData.getAggregator(rowKey, colKey);
228 return _react2.default.createElement(
229 'td',
230 {
231 className: 'pvtVal',
232 key: 'pvtVal' + i + '-' + j,
233 style: valueCellColors(rowKey, colKey, aggregator.value())
234 },
235 aggregator.format(aggregator.value())
236 );
237 }),
238 _react2.default.createElement(
239 'td',
240 {
241 className: 'pvtTotal',
242 style: colTotalColors(totalAggregator.value())
243 },
244 totalAggregator.format(totalAggregator.value())
245 )
246 );
247 }),
248 _react2.default.createElement(
249 'tr',
250 null,
251 _react2.default.createElement(
252 'th',
253 {
254 className: 'pvtTotalLabel',
255 colSpan: rowAttrs.length + (colAttrs.length === 0 ? 0 : 1)
256 },
257 'Totals'
258 ),
259 colKeys.map(function (colKey, i) {
260 var totalAggregator = pivotData.getAggregator([], colKey);
261 return _react2.default.createElement(
262 'td',
263 {
264 className: 'pvtTotal',
265 key: 'total' + i,
266 style: rowTotalColors(totalAggregator.value())
267 },
268 totalAggregator.format(totalAggregator.value())
269 );
270 }),
271 _react2.default.createElement(
272 'td',
273 { className: 'pvtGrandTotal' },
274 grandTotalAggregator.format(grandTotalAggregator.value())
275 )
276 )
277 )
278 );
279 }
280 }]);
281
282 return TableRenderer;
283 }(_react2.default.PureComponent);
284
285 TableRenderer.defaultProps = _Utilities.PivotData.defaultProps;
286 TableRenderer.propTypes = _Utilities.PivotData.propTypes;
287 TableRenderer.defaultProps.tableColorScaleGenerator = redColorScaleGenerator;
288 TableRenderer.propTypes.tableColorScaleGenerator = _propTypes2.default.func;
289 return TableRenderer;
290}
291
292var TSVExportRenderer = function (_React$PureComponent2) {
293 _inherits(TSVExportRenderer, _React$PureComponent2);
294
295 function TSVExportRenderer() {
296 _classCallCheck(this, TSVExportRenderer);
297
298 return _possibleConstructorReturn(this, (TSVExportRenderer.__proto__ || Object.getPrototypeOf(TSVExportRenderer)).apply(this, arguments));
299 }
300
301 _createClass(TSVExportRenderer, [{
302 key: 'render',
303 value: function render() {
304 var pivotData = new _Utilities.PivotData(this.props);
305 var rowKeys = pivotData.getRowKeys();
306 var colKeys = pivotData.getColKeys();
307 if (rowKeys.length === 0) {
308 rowKeys.push([]);
309 }
310 if (colKeys.length === 0) {
311 colKeys.push([]);
312 }
313
314 var headerRow = pivotData.props.rows.map(function (r) {
315 return r;
316 });
317 if (colKeys.length === 1 && colKeys[0].length === 0) {
318 headerRow.push(this.props.aggregatorName);
319 } else {
320 colKeys.map(function (c) {
321 return headerRow.push(c.join('-'));
322 });
323 }
324
325 var result = rowKeys.map(function (r) {
326 var row = r.map(function (x) {
327 return x;
328 });
329 colKeys.map(function (c) {
330 var v = pivotData.getAggregator(r, c).value();
331 row.push(v ? v : '');
332 });
333 return row;
334 });
335
336 result.unshift(headerRow);
337
338 return _react2.default.createElement('textarea', {
339 value: result.map(function (r) {
340 return r.join('\t');
341 }).join('\n'),
342 style: { width: window.innerWidth / 2, height: window.innerHeight / 2 },
343 readOnly: true
344 });
345 }
346 }]);
347
348 return TSVExportRenderer;
349}(_react2.default.PureComponent);
350
351TSVExportRenderer.defaultProps = _Utilities.PivotData.defaultProps;
352TSVExportRenderer.propTypes = _Utilities.PivotData.propTypes;
353
354exports.default = {
355 Table: makeRenderer(),
356 'Table Heatmap': makeRenderer({ heatmapMode: 'full' }),
357 'Table Col Heatmap': makeRenderer({ heatmapMode: 'col' }),
358 'Table Row Heatmap': makeRenderer({ heatmapMode: 'row' }),
359 'Exportable TSV': TSVExportRenderer
360};
361module.exports = exports['default'];
362//# sourceMappingURL=TableRenderers.js.map
\No newline at end of file