UNPKG

@onesy/ui-react

Version:
243 lines (231 loc) 12.1 kB
"use strict"; var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault"); Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties")); var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty")); var _react = _interopRequireDefault(require("react")); var _utils = require("@onesy/utils"); var _styleReact = require("@onesy/style-react"); var _Path = _interopRequireDefault(require("../Path")); var _Line = _interopRequireDefault(require("../Line")); var _Type = _interopRequireDefault(require("../Type")); var _useMediaQuery = _interopRequireDefault(require("../useMediaQuery")); var _utils2 = require("../utils"); var _jsxRuntime = require("react/jsx-runtime"); const _excluded = ["tonal", "color", "name", "values", "refs", "rects", "updateDefs", "updateLegend", "minMax", "animate", "animateTimeout", "smooth", "smoothRatio", "PathProps", "BackgroundProps", "BorderProps", "LegendItemProps", "className"]; function ownKeys(e, r) { var t = Object.keys(e); if (Object.getOwnPropertySymbols) { var o = Object.getOwnPropertySymbols(e); r && (o = o.filter(function (r) { return Object.getOwnPropertyDescriptor(e, r).enumerable; })), t.push.apply(t, o); } return t; } function _objectSpread(e) { for (var r = 1; r < arguments.length; r++) { var t = null != arguments[r] ? arguments[r] : {}; r % 2 ? ownKeys(Object(t), !0).forEach(function (r) { (0, _defineProperty2.default)(e, r, t[r]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(e, Object.getOwnPropertyDescriptors(t)) : ownKeys(Object(t)).forEach(function (r) { Object.defineProperty(e, r, Object.getOwnPropertyDescriptor(t, r)); }); } return e; } const useStyle = (0, _styleReact.style)(theme => ({ root: {}, legend_item: { userSelect: 'none' }, legend_icon: { width: '10px', height: '2px' } }), { name: 'onesy-LineChartItem' }); const LineChartItem = props_ => { var _theme$ui, _theme$ui2, _theme$elements, _theme$elements2, _theme$elements3; const theme = (0, _styleReact.useOnesyTheme)(); const props = _objectSpread(_objectSpread(_objectSpread({}, theme === null || theme === void 0 || (_theme$ui = theme.ui) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.elements) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.all) === null || _theme$ui === void 0 || (_theme$ui = _theme$ui.props) === null || _theme$ui === void 0 ? void 0 : _theme$ui.default), theme === null || theme === void 0 || (_theme$ui2 = theme.ui) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.elements) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.onesyLineChartItem) === null || _theme$ui2 === void 0 || (_theme$ui2 = _theme$ui2.props) === null || _theme$ui2 === void 0 ? void 0 : _theme$ui2.default), props_); const Line = (theme === null || theme === void 0 || (_theme$elements = theme.elements) === null || _theme$elements === void 0 ? void 0 : _theme$elements.Line) || _Line.default; const Path = (theme === null || theme === void 0 || (_theme$elements2 = theme.elements) === null || _theme$elements2 === void 0 ? void 0 : _theme$elements2.Path) || _Path.default; const Type = (theme === null || theme === void 0 || (_theme$elements3 = theme.elements) === null || _theme$elements3 === void 0 ? void 0 : _theme$elements3.Type) || _Type.default; const { tonal = true, color = 'primary', name, values, refs: refs_, rects, updateDefs, updateLegend, minMax, animate: animate_, animateTimeout: animateTimeout_, smooth = true, smoothRatio = 0.14, PathProps, BackgroundProps, BorderProps, LegendItemProps, className } = props, other = (0, _objectWithoutProperties2.default)(props, _excluded); const { classes } = useStyle(); const refs = { minMax: _react.default.useRef(null), smooth: _react.default.useRef(null), theme: _react.default.useRef(null), path: _react.default.useRef(null), pathStyle: _react.default.useRef({}), animate: _react.default.useRef(null), animateTimeout: _react.default.useRef(null), init: _react.default.useRef(null) }; const keys = _react.default.useMemo(() => { const result = []; const items = [animate_, animateTimeout_]; items.forEach(item => { if ((0, _utils.is)('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key_0 => result.push(key_0)); }); return (0, _utils.unique)(result); }, [animate_, animateTimeout_]); const breakpoints = {}; keys.forEach(key_1 => { breakpoints[key_1] = (0, _useMediaQuery.default)(theme.breakpoints.media[key_1], { element: refs.path.current }); }); const animate = (0, _utils2.valueBreakpoints)(animate_, true, breakpoints, theme); const animateTimeout = (0, _utils2.valueBreakpoints)(animateTimeout_, 140, breakpoints, theme); const [value, setValue] = _react.default.useState(); const [init, setInit] = _react.default.useState(); refs.theme.current = theme; refs.smooth.current = smooth; refs.animate.current = animate; refs.animateTimeout.current = animateTimeout; refs.init.current = init; const LegendItem = legendItemProps => { return /*#__PURE__*/(0, _jsxRuntime.jsxs)(Line, _objectSpread(_objectSpread(_objectSpread({ gap: 1, direction: "row", align: "center" }, legendItemProps), LegendItemProps), {}, { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('LineChartItem', theme) && ['onesy-LineChartItem-legend-item'], legendItemProps === null || legendItemProps === void 0 ? void 0 : legendItemProps.className, LegendItemProps === null || LegendItemProps === void 0 ? void 0 : LegendItemProps.className, classes.legend_item]), children: [/*#__PURE__*/(0, _jsxRuntime.jsx)("span", { className: (0, _styleReact.classNames)([(0, _utils2.staticClassName)('LineChartItem', theme) && ['onesy-LineChartItem-legend-icon'], classes.legend_icon]), style: { background: !refs.theme.current.palette.color[color] ? color : refs.theme.current.palette.color[color]['main'] } }), /*#__PURE__*/(0, _jsxRuntime.jsx)(Type, { version: "b3", children: name })] })); }; const make = () => { // Make values into x, y, coordinates // normalized in rect width, height values // invert y so 0, 0 is at bottom left if (rects !== null && rects !== void 0 && rects.wrapper && values) { const { width, height } = rects.wrapper; const values_ = values // Sort for x from smallest to largest .sort((a, b) => a[0] - b[0]).map(itemValue => { const [x, y] = itemValue; const values__ = { x: (0, _utils.percentageFromValueWithinRange)(x, minMax.min.x, minMax.max.x), y: (0, _utils.percentageFromValueWithinRange)(y, minMax.min.y, minMax.max.y) }; values__.x = (0, _utils.valueFromPercentageWithinRange)(values__.x, 0, width); values__.y = (0, _utils.valueFromPercentageWithinRange)(values__.y, 0, height); return [values__.x, height - values__.y].map(item_ => Math.abs(item_)); }); let d = ''; if (!refs.smooth.current) { d = values_.reduce((result_0, itemValue_0, index) => { const [x_0, y_0] = itemValue_0; // Move if (index === 0) return result_0 += `M ${x_0} ${y_0}`; return result_0 += `L ${x_0} ${y_0}`; }, ''); } else { d = values_.reduce((result_1, itemValue_1, index_0, array) => { const [x_1, y_1] = itemValue_1; // Move if (index_0 === 0) return result_1 += `M ${x_1} ${y_1}`; const [x1, y1] = (0, _utils2.controlPoint)(array[index_0 - 1], array[index_0 - 2], itemValue_1, false, smoothRatio); const [x2, y2] = (0, _utils2.controlPoint)(itemValue_1, array[index_0 - 1], array[index_0 + 1], true, smoothRatio); return result_1 += `C ${x1} ${y1} ${x2} ${y2} ${x_1} ${y_1}`; }, ''); } const element = /*#__PURE__*/(0, _jsxRuntime.jsx)("g", { children: /*#__PURE__*/(0, _jsxRuntime.jsx)(Path, _objectSpread(_objectSpread({ ref: refs.path, d: d, fill: "none", stroke: !refs.theme.current.palette.color[color] ? color : refs.theme.current.palette.color[color]['main'], strokeWidth: "2px" }, PathProps), {}, { style: _objectSpread(_objectSpread(_objectSpread({}, PathProps === null || PathProps === void 0 ? void 0 : PathProps.style), refs.animate.current && refs.init.current !== 'animated' && { opacity: 0 }), refs.pathStyle.current) })) }); // Legend item if ((0, _utils.is)('function', updateLegend)) { updateLegend(previous => { const newValues = [...(previous || [])]; if (!newValues.find(itemLegend => { var _itemLegend$item; return ((_itemLegend$item = itemLegend.item) === null || _itemLegend$item === void 0 ? void 0 : _itemLegend$item.name) === name; })) { newValues.push({ item: { name, values }, element: /*#__PURE__*/(0, _jsxRuntime.jsx)(LegendItem, {}) }); } return newValues; }); } // Value setValue(element); } }; const initMethod = () => { if (animate) { if (!init && refs.path.current) { const total = refs.path.current.getTotalLength(); refs.pathStyle.current = { strokeDasharray: total, strokeDashoffset: total }; setInit(true); setTimeout(() => { refs.pathStyle.current = _objectSpread(_objectSpread({}, refs.pathStyle.current), {}, { transition: theme.methods.transitions.make('stroke-dashoffset', { duration: 2400, timing_function: 'decelerated' }), opacity: 1, strokeDashoffset: 0 }); setInit('animated'); }, refs.animateTimeout.current); } } }; _react.default.useEffect(() => { make(); initMethod(); }, [values, theme, animate, init, !!refs.path.current]); _react.default.useEffect(() => { make(); }, [rects === null || rects === void 0 ? void 0 : rects.wrapper]); return value && /*#__PURE__*/_react.default.cloneElement(value, _objectSpread({ className }, other)); }; // Parts of the logic done thanks to // https://francoisromain.medium.com/smooth-a-svg-path-with-cubic-bezier-curves-e37b49d46c74 // Copyright (c) 2022 by François Romain (https://codepen.io/francoisromain/pen/YxyEQL) // Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: // The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. LineChartItem.displayName = 'onesy-LineChartItem'; var _default = exports.default = LineChartItem;