UNPKG

5.62 kBJavaScriptView Raw
1"use strict";
2var __extends = (this && this.__extends) || (function () {
3 var extendStatics = Object.setPrototypeOf ||
4 ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
5 function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
6 return function (d, b) {
7 extendStatics(d, b);
8 function __() { this.constructor = d; }
9 d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
10 };
11})();
12Object.defineProperty(exports, "__esModule", { value: true });
13var React = require("react");
14var d3array = require("d3-array");
15var d3scale = require("d3-scale");
16var _ = require("lodash");
17var constants_1 = require("./constants");
18var helpers_1 = require("./helpers");
19var HKGrid_1 = require("./HKGrid");
20var HKBarChartData = /** @class */ (function (_super) {
21 __extends(HKBarChartData, _super);
22 function HKBarChartData(props) {
23 var _this = _super.call(this, props) || this;
24 _this.handleMouseMove = function (e) {
25 if (!_this.ref || !_this.props.onHover) {
26 return null;
27 }
28 var _a = _this.state, data = _a.data, keys = _a.keys, width = _a.width;
29 var hoverIndex = e.clientX - _this.ref.getBoundingClientRect().left;
30 var interpolateIndex = hoverIndex * data.length / width;
31 var index = Math.min(interpolateIndex, data.length - 1);
32 var values = data[Math.floor(index)];
33 _this.props.onHover(values, keys);
34 };
35 _this.handleMouseLeave = function (e) {
36 var onHover = _this.props.onHover;
37 var _a = _this.state, data = _a.data, keys = _a.keys;
38 if (onHover) {
39 onHover(helpers_1.getMaxValues(data, 'bar'), keys);
40 }
41 };
42 _this.createBar = function (rowIdx, colVal, colIdx) {
43 var _a = _this.state, height = _a.height, keys = _a.keys, x0Scale = _a.x0Scale, x1Scale = _a.x1Scale, yScale = _a.yScale;
44 return (React.createElement("rect", { key: "row" + rowIdx + "-column" + colIdx, x: x0Scale(rowIdx) + x1Scale(colIdx), y: height - yScale(colVal), height: yScale(colVal), width: x1Scale.bandwidth(), fill: constants_1.colours[keys[colIdx]], className: 'dim' }));
45 };
46 _this.state = {
47 data: null,
48 height: 0,
49 keys: [],
50 width: 0,
51 hoverIndex: -1,
52 x0Scale: null,
53 x1Scale: null,
54 yScale: null,
55 };
56 return _this;
57 }
58 HKBarChartData.getDerivedStateFromProps = function (newProps, prevState) {
59 if (['data', 'width', 'height', 'toggleInfo'].every(function (o) { return newProps[o] === prevState[o]; })) {
60 return null;
61 }
62 var data = newProps.data, height = newProps.height, width = newProps.width, toggleInfo = newProps.toggleInfo;
63 var chartHeight = height - constants_1.ChartPadding.Vertical;
64 var chartWidth = width - constants_1.ChartPadding.Horizontal;
65 // keys: an array of the indexes from bar chart data that are toggled on
66 var keys = Object.keys(toggleInfo).filter(function (k) { return toggleInfo[k]; }).map(Number);
67 var shownData = data.map(function (rowData) { return rowData.filter(function (colData, i) { return _.includes(keys, i); }); });
68 var minValues = d3array.min(shownData, (function (arr) { return d3array.min(arr); }));
69 var maxValues = d3array.max(shownData, (function (arr) { return d3array.max(arr); }));
70 var x0Scale = d3scale.scaleBand()
71 .range([0, chartWidth])
72 .domain(shownData.map(function (d, i) { return i; }))
73 .paddingInner(0.1);
74 var x1Scale = d3scale.scaleBand()
75 .range([0, x0Scale.bandwidth()])
76 .domain(shownData[0].map(function (d, i) { return i; }))
77 .padding(0.08);
78 var yScale = d3scale.scaleLinear()
79 .rangeRound([0, chartHeight])
80 .domain([Math.min(0, minValues), maxValues]);
81 return {
82 data: shownData,
83 height: height,
84 keys: keys,
85 width: width,
86 x0Scale: x0Scale,
87 x1Scale: x1Scale,
88 yScale: yScale,
89 };
90 };
91 HKBarChartData.prototype.render = function () {
92 var _this = this;
93 var _a = this.state, data = _a.data, height = _a.height, width = _a.width, x0Scale = _a.x0Scale, yScale = _a.yScale;
94 var bars = data.map(function (rowData, rowIdx) { return (React.createElement("g", { key: "row-" + rowIdx, className: 'dim' }, rowData.map(function (colVal, colIdx) { return _this.createBar(rowIdx, colVal, colIdx); }))); });
95 return (React.createElement("svg", { preserveAspectRatio: 'none', onMouseMove: this.handleMouseMove, onMouseLeave: this.handleMouseLeave, width: width, height: height, viewBox: "0 0 " + width + " " + height, ref: function (ref) { return _this.ref = ref; } },
96 React.createElement("g", { transform: "translate(" + constants_1.ChartPadding.Horizontal + ", 0)" },
97 React.createElement("rect", { x: '0', y: '0', width: width - constants_1.ChartPadding.Horizontal, height: height, className: 'br0 ba b--silver z-1', fill: 'none', stroke: 'silver' }),
98 React.createElement(HKGrid_1.default, { type: 'bar', height: height, width: width, xScale: x0Scale, yScale: yScale }),
99 bars)));
100 };
101 return HKBarChartData;
102}(React.PureComponent));
103exports.default = HKBarChartData;
104//# sourceMappingURL=HKBarChartData.js.map
\No newline at end of file