UNPKG

wix-style-react

Version:
284 lines (251 loc) • 10.9 kB
import _extends from "@babel/runtime/helpers/extends"; import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray"; import _classCallCheck from "@babel/runtime/helpers/classCallCheck"; import _createClass from "@babel/runtime/helpers/createClass"; import _assertThisInitialized from "@babel/runtime/helpers/assertThisInitialized"; import _inherits from "@babel/runtime/helpers/inherits"; import _possibleConstructorReturn from "@babel/runtime/helpers/possibleConstructorReturn"; import _getPrototypeOf from "@babel/runtime/helpers/getPrototypeOf"; import _defineProperty from "@babel/runtime/helpers/defineProperty"; function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) { symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); } keys.push.apply(keys, symbols); } return keys; } function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; } function _createSuper(Derived) { var hasNativeReflectConstruct = _isNativeReflectConstruct(); return function _createSuperInternal() { var Super = _getPrototypeOf(Derived), result; if (hasNativeReflectConstruct) { var NewTarget = _getPrototypeOf(this).constructor; result = Reflect.construct(Super, arguments, NewTarget); } else { result = Super.apply(this, arguments); } return _possibleConstructorReturn(this, result); }; } function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; if (typeof Proxy === "function") return true; try { Boolean.prototype.valueOf.call(Reflect.construct(Boolean, [], function () {})); return true; } catch (e) { return false; } } import React from 'react'; import PropTypes from 'prop-types'; import Tooltip from '../Tooltip'; import { TooltipCommonProps } from '../common/PropTypes/TooltipCommon'; import { st, classes } from './StackedBarChart.st.css'; import { stVars as colors } from '../Foundation/stylable/colors.st.css'; import { scaleLinear, scaleBand } from 'd3-scale'; import { select } from 'd3-selection'; import { axisRight } from 'd3-axis'; import { format } from 'd3-format'; import { dataHooks } from './constants'; import Text from '../Text'; var defaultYAxisTickFormat = format(','); var CONSTANTS = { top: 30, right: 30, bottom: 30, left: 100, gap: 18, barWidth: 48 }; /** StackedBarChart */ var StackedBarChart = /*#__PURE__*/function (_React$PureComponent) { _inherits(StackedBarChart, _React$PureComponent); var _super = _createSuper(StackedBarChart); function StackedBarChart(props) { var _this; _classCallCheck(this, StackedBarChart); _this = _super.call(this, props); _defineProperty(_assertThisInitialized(_this), "_getAmountOfBarsCanRender", function (data) { var width = _this.props.width; var result = []; var availableWidthForBars = width - _this.margin.left; var barWidth = CONSTANTS.barWidth + CONSTANTS.gap; var availableBarsForRender = Math.floor(availableWidthForBars / barWidth); for (var i = 0; i < availableBarsForRender; i++) { if (!!data[i]) { result.push(data[i]); } } return result; }); _defineProperty(_assertThisInitialized(_this), "_update", function () { var _this$props = _this.props, data = _this$props.data, yAxisTickFormat = _this$props.yAxisTickFormat, width = _this$props.width; var _this$state = _this.state, svg = _this$state.svg, x = _this$state.x, y = _this$state.y; // Data var _data = _this._getAmountOfBarsCanRender(data).map(function (d) { return _objectSpread(_objectSpread({}, d), {}, { sum: d.values.reduce(function (a, b) { return a + b; }, 0) }); }); var innerPadding = _data.length > 4 ? 0.8 : 0.75; var scalesPadding = 0.5; var scaleWidth = _data.length * (CONSTANTS.barWidth + CONSTANTS.gap) + CONSTANTS.gap; // Scales var _x = x.domain(_data.map(function (d) { return d.label; })).range([0, scaleWidth]).paddingInner(innerPadding).paddingOuter(scalesPadding).round(true); var _y = y.domain([0, Math.max.apply(Math, _toConsumableArray(_data.map(function (d) { return d.sum; })))]); var yAxis = axisRight(_y).tickSize(width).tickFormat(function (d) { return yAxisTickFormat(d, defaultYAxisTickFormat(d)); }).ticks(4); svg.select("[data-hook=\"".concat(dataHooks.yAxis, "\"]")).call(yAxis).selectAll('.tick text').attr('x', -6).attr('dy', 4); _this.setState({ data: _data, x: _x, y: _y, yAxis: yAxis, width: width }); }); _this.chart = /*#__PURE__*/React.createRef(); window.chart = _assertThisInitialized(_this); _this.data = []; _this.colors = [colors.A1, colors.A6]; _this.margin = Object.assign(CONSTANTS, props.margin); _this.height = props.height; _this.chartHeight = _this.height - _this.margin.top - _this.margin.bottom; _this.state = { data: [], x: scaleBand().range([0, props.weight]), y: scaleLinear().range([_this.chartHeight, 0]), width: props.weight }; return _this; } _createClass(StackedBarChart, [{ key: "componentDidMount", value: function componentDidMount() { this.setState({ svg: select(this.chart.current) }, this._update); } }, { key: "componentDidUpdate", value: function componentDidUpdate(prevProps) { if (prevProps.data !== this.props.data) { this.setState({ svg: select(this.chart.current) }, this._update); return true; } } }, { key: "render", value: function render() { var _this2 = this; var _this$props2 = this.props, tooltipTemplate = _this$props2.tooltipTemplate, tooltipCommonProps = _this$props2.tooltipCommonProps, className = _this$props2.className, dataHook = _this$props2.dataHook; var _this$state2 = this.state, data = _this$state2.data, x = _this$state2.x, y = _this$state2.y, width = _this$state2.width; return /*#__PURE__*/React.createElement("div", { "data-hook": dataHook, className: st(classes.root, {}, className) }, /*#__PURE__*/React.createElement("svg", { ref: this.chart, width: width, height: this.height }, /*#__PURE__*/React.createElement("g", { "data-hook": dataHooks.yAxis, className: classes.yAxis, transform: "translate(".concat(this.margin.left, ", ").concat(this.margin.top, ")") }), /*#__PURE__*/React.createElement("g", { "data-hook": dataHooks.bars, transform: "translate(".concat(this.margin.left, ", ").concat(this.margin.top, ")") }, data.map(function (d, itemIndex) { var stacked = _this2.chartHeight; return /*#__PURE__*/React.createElement("g", { key: itemIndex }, d.values.map(function (value, index) { var height = _this2.chartHeight - y(value); stacked -= height; return /*#__PURE__*/React.createElement("rect", { key: index, fill: _this2.colors[index], height: height, width: CONSTANTS.barWidth, x: x(d.label) - 25, y: stacked }); })); }))), data && data.map(function (d, index) { return /*#__PURE__*/React.createElement("div", { key: index, className: classes.textPosition, style: { left: x(d.label) + _this2.margin.left - 25, top: _this2.height - 15 } }, /*#__PURE__*/React.createElement(Text, { textAlign: "center", skin: "standard", light: true, secondary: true, weight: "normal", size: "small", ellipsis: true, "data-hook": dataHooks.xAxis }, d.label)); }), tooltipTemplate && data.map(function (d, index) { return /*#__PURE__*/React.createElement("div", { key: index, className: classes.tooltip, style: { left: x(d.label) + _this2.margin.left - 25, top: y(d.sum) + _this2.margin.top } }, /*#__PURE__*/React.createElement(Tooltip, _extends({}, tooltipCommonProps, { content: tooltipTemplate(d), dataHook: dataHooks.tooltip }), /*#__PURE__*/React.createElement("button", { className: classes.tooltipInner, style: { height: "".concat(_this2.chartHeight - y(d.sum), "px") } }))); })); } }]); return StackedBarChart; }(React.PureComponent); StackedBarChart.displayName = 'StackedBarChart'; StackedBarChart.propTypes = { /** Applied as data-hook HTML attribute that can be used in the tests */ dataHook: PropTypes.string, /** A css class to be applied to the component's root element */ className: PropTypes.string, /** Chart data */ data: PropTypes.arrayOf(PropTypes.shape({ label: PropTypes.node, values: PropTypes.arrayOf(PropTypes.number) })), /** Tooltip template function */ tooltipTemplate: PropTypes.func, /** Props that modify the Tooltip created from text bar charts */ tooltipCommonProps: PropTypes.shape(TooltipCommonProps), /** Chart height (px) */ height: PropTypes.number, /** Chart width (px) */ width: PropTypes.number, /** Margin (px) for each side of the Chart. For example, in order to render larger number of digits at the yAxis, increase the left margin prop. */ margin: PropTypes.shape({ right: PropTypes.number, left: PropTypes.number, bottom: PropTypes.number, top: PropTypes.number }), /** * ##### Formats Y axis ticks * * `param` {string} `rawValue` - a raw value e.g. 10000 * * `param` {string} `rawValue` - number formatted value, comma separated e.g. 10,000 * * `return` {string} - the value to be shown as Y axis tick */ yAxisTickFormat: PropTypes.func }; StackedBarChart.defaultProps = { data: [], width: 900, height: 350, margin: CONSTANTS, tooltipCommonProps: {}, yAxisTickFormat: defaultYAxisTickFormat }; export default StackedBarChart;