@amaui/ui-react
Version:
UI for React
228 lines (217 loc) • 10.2 kB
JavaScript
import _extends from "@babel/runtime/helpers/extends";
import _objectWithoutProperties from "@babel/runtime/helpers/objectWithoutProperties";
import _defineProperty from "@babel/runtime/helpers/defineProperty";
const _excluded = ["tonal", "color", "name", "values", "refs", "rects", "updateDefs", "updateLegend", "minMax", "animate", "animateTimeout", "smooth", "smoothRatio", "PathProps", "BackgroundProps", "BorderProps", "LegendItemProps", "className"];
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); 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 = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
import React from 'react';
import { is, percentageFromValueWithinRange, unique, valueFromPercentageWithinRange } from '@amaui/utils';
import { classNames, style as styleMethod, useAmauiTheme } from '@amaui/style-react';
import PathElement from '../Path';
import LineElement from '../Line';
import TypeElement from '../Type';
import useMediaQuery from '../useMediaQuery';
import { controlPoint, staticClassName, valueBreakpoints } from '../utils';
const useStyle = styleMethod(theme => ({
root: {},
legend_item: {
userSelect: 'none'
},
legend_icon: {
width: '10px',
height: '2px'
}
}), {
name: 'amaui-LineChartItem'
});
const LineChartItem = /*#__PURE__*/React.forwardRef((props_, ref) => {
const theme = useAmauiTheme();
const props = React.useMemo(() => _objectSpread(_objectSpread(_objectSpread({}, theme?.ui?.elements?.all?.props?.default), theme?.ui?.elements?.amauiLineChartItem?.props?.default), props_), [props_]);
const Line = React.useMemo(() => theme?.elements?.Line || LineElement, [theme]);
const Path = React.useMemo(() => theme?.elements?.Path || PathElement, [theme]);
const Type = React.useMemo(() => theme?.elements?.Type || TypeElement, [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 = _objectWithoutProperties(props, _excluded);
const {
classes
} = useStyle();
const refs = {
minMax: React.useRef(undefined),
smooth: React.useRef(undefined),
theme: React.useRef(undefined),
path: React.useRef(),
pathStyle: React.useRef({}),
animate: React.useRef(undefined),
animateTimeout: React.useRef(),
init: React.useRef(undefined)
};
const keys = React.useMemo(() => {
const result = [];
const items = [animate_, animateTimeout_];
items.forEach(item => {
if (is('object', item)) Object.keys(item).filter(key => theme.breakpoints.media[key]).forEach(key => result.push(key));
});
return unique(result);
}, [animate_, animateTimeout_]);
const breakpoints = {};
keys.forEach(key => {
breakpoints[key] = useMediaQuery(theme.breakpoints.media[key], {
element: refs.path.current
});
});
const animate = valueBreakpoints(animate_, true, breakpoints, theme);
const animateTimeout = valueBreakpoints(animateTimeout_, 140, breakpoints, theme);
const [value, setValue] = React.useState();
const [init, setInit] = React.useState();
refs.theme.current = theme;
refs.smooth.current = smooth;
refs.animate.current = animate;
refs.animateTimeout.current = animateTimeout;
refs.init.current = init;
const LegendItem = React.useCallback(legendItemProps => {
return /*#__PURE__*/React.createElement(Line, _extends({
gap: 1,
direction: "row",
align: "center"
}, legendItemProps, LegendItemProps, {
className: classNames([staticClassName('LineChartItem', theme) && ['amaui-LineChartItem-legend-item'], legendItemProps?.className, LegendItemProps?.className, classes.legend_item])
}), /*#__PURE__*/React.createElement("span", {
className: classNames([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']
}
}), /*#__PURE__*/React.createElement(Type, {
version: "b3"
}, 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?.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: percentageFromValueWithinRange(x, minMax.min.x, minMax.max.x),
y: percentageFromValueWithinRange(y, minMax.min.y, minMax.max.y)
};
values__.x = valueFromPercentageWithinRange(values__.x, 0, width);
values__.y = 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] = controlPoint(array[index - 1], array[index - 2], itemValue, false, smoothRatio);
const [x2, y2] = controlPoint(itemValue, array[index - 1], array[index + 1], true, smoothRatio);
return result += `C ${x1} ${y1} ${x2} ${y2} ${x} ${y}`;
}, '');
}
const element = /*#__PURE__*/React.createElement("g", null, /*#__PURE__*/React.createElement(Path, _extends({
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?.style), refs.animate.current && refs.init.current !== 'animated' && {
opacity: 0
}), refs.pathStyle.current)
})));
// Legend item
if (is('function', updateLegend)) {
updateLegend(previous => {
const newValues = [...(previous || [])];
if (!newValues.find(itemLegend => itemLegend.item?.name === name)) {
newValues.push({
item: {
name,
values
},
element: /*#__PURE__*/React.createElement(LegendItem, null)
});
}
return newValues;
});
}
// Value
setValue(element);
}
};
const initMethod = React.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 = _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);
}
}
}, [init, animate]);
React.useEffect(() => {
make();
initMethod();
}, [values, theme, animate, init, !!refs.path.current]);
React.useEffect(() => {
make();
}, [rects?.wrapper]);
return value && /*#__PURE__*/React.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 = 'amaui-LineChartItem';
export default LineChartItem;