UNPKG

@amaui/ui-react

Version:
189 lines (188 loc) 12.2 kB
"use strict"; var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const jsx_runtime_1 = require("react/jsx-runtime"); const react_1 = __importDefault(require("react")); const utils_1 = require("@amaui/utils"); const style_react_1 = require("@amaui/style-react"); const Path_1 = __importDefault(require("../Path")); const Line_1 = __importDefault(require("../Line")); const Type_1 = __importDefault(require("../Type")); const useMediaQuery_1 = __importDefault(require("../useMediaQuery")); const utils_2 = require("../utils"); const useStyle = (0, style_react_1.style)(theme => ({ root: {}, legend_item: { userSelect: 'none' }, legend_icon: { width: '10px', height: '2px' } }), { name: 'amaui-LineChartItem' }); const LineChartItem = react_1.default.forwardRef((props_, ref) => { const theme = (0, style_react_1.useAmauiTheme)(); const props = react_1.default.useMemo(() => { var _a, _b, _c, _d, _e, _f, _g, _h; return (Object.assign(Object.assign(Object.assign({}, (_d = (_c = (_b = (_a = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _a === void 0 ? void 0 : _a.elements) === null || _b === void 0 ? void 0 : _b.all) === null || _c === void 0 ? void 0 : _c.props) === null || _d === void 0 ? void 0 : _d.default), (_h = (_g = (_f = (_e = theme === null || theme === void 0 ? void 0 : theme.ui) === null || _e === void 0 ? void 0 : _e.elements) === null || _f === void 0 ? void 0 : _f.amauiLineChartItem) === null || _g === void 0 ? void 0 : _g.props) === null || _h === void 0 ? void 0 : _h.default), props_)); }, [props_]); const Line = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Line) || Line_1.default; }, [theme]); const Path = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Path) || Path_1.default; }, [theme]); const Type = react_1.default.useMemo(() => { var _a; return ((_a = theme === null || theme === void 0 ? void 0 : theme.elements) === null || _a === void 0 ? void 0 : _a.Type) || Type_1.default; }, [theme]); 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 = __rest(props, ["tonal", "color", "name", "values", "refs", "rects", "updateDefs", "updateLegend", "minMax", "animate", "animateTimeout", "smooth", "smoothRatio", "PathProps", "BackgroundProps", "BorderProps", "LegendItemProps", "className"]); const { classes } = useStyle(); const refs = { minMax: react_1.default.useRef(undefined), smooth: react_1.default.useRef(undefined), theme: react_1.default.useRef(undefined), path: react_1.default.useRef(), pathStyle: react_1.default.useRef({}), animate: react_1.default.useRef(undefined), animateTimeout: react_1.default.useRef(), init: react_1.default.useRef(undefined) }; const keys = react_1.default.useMemo(() => { const result = []; const items = [animate_, animateTimeout_]; items.forEach(item => { if ((0, utils_1.is)('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key => result.push(key)); }); return (0, utils_1.unique)(result); }, [animate_, animateTimeout_]); const breakpoints = {}; keys.forEach(key => { breakpoints[key] = (0, useMediaQuery_1.default)(theme.breakpoints.media[key], { element: refs.path.current }); }); const animate = (0, utils_2.valueBreakpoints)(animate_, true, breakpoints, theme); const animateTimeout = (0, utils_2.valueBreakpoints)(animateTimeout_, 140, breakpoints, theme); const [value, setValue] = react_1.default.useState(); const [init, setInit] = react_1.default.useState(); refs.theme.current = theme; refs.smooth.current = smooth; refs.animate.current = animate; refs.animateTimeout.current = animateTimeout; refs.init.current = init; const LegendItem = react_1.default.useCallback((legendItemProps) => { return ((0, jsx_runtime_1.jsxs)(Line, Object.assign({ gap: 1, direction: 'row', align: 'center' }, legendItemProps, LegendItemProps, { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('LineChartItem', theme) && [ 'amaui-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: [(0, jsx_runtime_1.jsx)("span", { className: (0, style_react_1.classNames)([ (0, utils_2.staticClassName)('LineChartItem', theme) && [ 'amaui-LineChartItem-legend-icon' ], classes.legend_icon ]), style: { background: !refs.theme.current.palette.color[color] ? color : refs.theme.current.palette.color[color]['main'] } }), (0, jsx_runtime_1.jsx)(Type, Object.assign({ version: 'b3' }, { children: name }))] }))); }, [theme]); 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 ? 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_1.percentageFromValueWithinRange)(x, minMax.min.x, minMax.max.x), y: (0, utils_1.percentageFromValueWithinRange)(y, minMax.min.y, minMax.max.y) }; values__.x = (0, utils_1.valueFromPercentageWithinRange)(values__.x, 0, width); values__.y = (0, utils_1.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, itemValue, index) => { const [x, y] = itemValue; // Move if (index === 0) return result += `M ${x} ${y}`; return result += `L ${x} ${y}`; }, ''); } else { d = values_.reduce((result, itemValue, index, array) => { const [x, y] = itemValue; // Move if (index === 0) return result += `M ${x} ${y}`; const [x1, y1] = (0, utils_2.controlPoint)(array[index - 1], array[index - 2], itemValue, false, smoothRatio); const [x2, y2] = (0, utils_2.controlPoint)(itemValue, array[index - 1], array[index + 1], true, smoothRatio); return result += `C ${x1} ${y1} ${x2} ${y2} ${x} ${y}`; }, ''); } const element = ((0, jsx_runtime_1.jsx)("g", { children: (0, jsx_runtime_1.jsx)(Path, Object.assign({ 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: Object.assign(Object.assign(Object.assign({}, 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_1.is)('function', updateLegend)) { updateLegend(previous => { const newValues = [...(previous || [])]; if (!newValues.find(itemLegend => { var _a; return ((_a = itemLegend.item) === null || _a === void 0 ? void 0 : _a.name) === name; })) { newValues.push({ item: { name, values }, element: (0, jsx_runtime_1.jsx)(LegendItem, {}) }); } return newValues; }); } // Value setValue(element); } }; const initMethod = react_1.default.useCallback(() => { 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 = Object.assign(Object.assign({}, refs.pathStyle.current), { transition: theme.methods.transitions.make('stroke-dashoffset', { duration: 2400, timing_function: 'decelerated' }), opacity: 1, strokeDashoffset: 0 }); setInit('animated'); }, refs.animateTimeout.current); } } }, [init, animate]); react_1.default.useEffect(() => { make(); initMethod(); }, [values, theme, animate, init, !!refs.path.current]); react_1.default.useEffect(() => { make(); }, [rects === null || rects === void 0 ? void 0 : rects.wrapper]); return (value && react_1.default.cloneElement(value, Object.assign({ 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 = 'amaui-LineChartItem'; exports.default = LineChartItem;