UNPKG

wix-style-react

Version:
197 lines (167 loc) • 8.14 kB
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 { Line, defaults } from 'react-chartjs-2'; import throttle from 'lodash/throttle'; import { formatToCompactNumber, calcPrecision } from '../utils/numberFormatters'; import { DATASET_PROPS, GRIDLINE_PROPS, OPTIONS_PROPS, TOOLTIP_PROPS, Y_AXES_TICKS_PROPS } from './chartOptions'; import { st, classes, stVars } from './AreaChart.st.css'; defaults.global.defaultFontFamily = 'Madefor, Helvetica Neue'; /** An area chart is a way of plotting data points on a line. Often, it is used to show trend data */ var AreaChart = /*#__PURE__*/function (_React$PureComponent) { _inherits(AreaChart, _React$PureComponent); var _super = _createSuper(AreaChart); function AreaChart() { var _this; _classCallCheck(this, AreaChart); for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) { args[_key] = arguments[_key]; } _this = _super.call.apply(_super, [this].concat(args)); _defineProperty(_assertThisInitialized(_this), "cursorLine", null); _defineProperty(_assertThisInitialized(_this), "handleHover", function (_, activeItems) { var tooltipItem = activeItems[0]; if (tooltipItem) { var providedTooltipItem = _this.props.data[tooltipItem._index]; var onTooltipShow = _this.props.onTooltipShow; onTooltipShow && onTooltipShow(providedTooltipItem); } }); _defineProperty(_assertThisInitialized(_this), "handleHoverThrottled", throttle(_this.handleHover, 100)); _defineProperty(_assertThisInitialized(_this), "onMouseMove", function (tooltip) { if (tooltip.y < 0) { tooltip.y = tooltip.y + tooltip.height + tooltip.caretSize + tooltip.xPadding; tooltip.yAlign = 'top'; } if (tooltip.opacity) { _this.cursorLine.style.opacity = '1'; _this.cursorLine.style.left = "".concat(tooltip.caretX, "px"); return; } _this.cursorLine.style.opacity = '0'; }); return _this; } _createClass(AreaChart, [{ key: "render", value: function render() { var _this2 = this; var _this$props = this.props, _this$props$data = _this$props.data, data = _this$props$data === void 0 ? [] : _this$props$data, tooltipContent = _this$props.tooltipContent, maxYTicksLimit = _this$props.maxYTicksLimit, dataHook = _this$props.dataHook, className = _this$props.className; var labels = data.map(function (i) { return i.label; }); var dataset = data.map(function (i) { return i.value; }); return /*#__PURE__*/React.createElement("section", { className: st(classes.lineChart, className), "data-hook": dataHook }, /*#__PURE__*/React.createElement(Line, { redraw: true, data: { labels: labels, datasets: [_objectSpread(_objectSpread({}, DATASET_PROPS), {}, { data: dataset })] }, options: _objectSpread(_objectSpread({}, OPTIONS_PROPS), {}, { scales: { xAxes: [{ gridLines: _objectSpread(_objectSpread({}, GRIDLINE_PROPS), {}, { tickMarkLength: 0 }), ticks: { padding: 5, fontColor: stVars.gridLineZeroLineColor, maxRotation: 0 } }], yAxes: [{ ticks: _objectSpread(_objectSpread({}, Y_AXES_TICKS_PROPS), {}, { maxTicksLimit: maxYTicksLimit, fontColor: stVars.gridLineZeroLineColor, callback: function () { var precision; return function (value, index, values) { if (index === values.length - 1) { return; } if (!precision) { precision = calcPrecision(values); } return formatToCompactNumber(value, precision).toLocaleLowerCase(); }; }() }), gridLines: _objectSpread({ tickMarkLength: -1 }, GRIDLINE_PROPS) }] }, tooltips: _objectSpread(_objectSpread({}, TOOLTIP_PROPS), {}, { enabled: !!tooltipContent, custom: this.onMouseMove, callbacks: { title: function title() { return ''; }, label: function label(tooltipItem) { return tooltipContent && tooltipContent(_toConsumableArray(data)[tooltipItem.index], tooltipItem.index); } } }), onHover: this.handleHoverThrottled }) }), /*#__PURE__*/React.createElement("div", { ref: function ref(line) { return _this2.cursorLine = line; }, className: classes.lineChartCursor })); } }]); return AreaChart; }(React.PureComponent); AreaChart.displayName = 'AreaChart'; AreaChart.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, /** * Array of Areat Chart items * * `value` - Item's value. * * `label` - A Short label under the value. */ data: PropTypes.arrayOf(PropTypes.shape({ value: PropTypes.number.isRequired, label: PropTypes.string.isRequired })).isRequired, /** Tooltip content template function*/ tooltipContent: PropTypes.func, /** Callback on tooltip content show event */ onTooltipShow: PropTypes.func, /** Maximum ticks allowed in Y axis */ maxYTicksLimit: PropTypes.number }; AreaChart.defaultProps = { maxYTicksLimit: 5 }; export default AreaChart;