UNPKG

@lobehub/charts

Version:

React modern charts components built on recharts

254 lines (253 loc) 15.8 kB
'use client'; function _typeof(o) { "@babel/helpers - typeof"; return _typeof = "function" == typeof Symbol && "symbol" == typeof Symbol.iterator ? function (o) { return typeof o; } : function (o) { return o && "function" == typeof Symbol && o.constructor === Symbol && o !== Symbol.prototype ? "symbol" : typeof o; }, _typeof(o); } var _excluded = ["data", "categories", "customCategories", "index", "colors", "valueFormatter", "animationDuration", "showAnimation", "showTooltip", "showLegend", "showGridLines", "autoMinValue", "minValue", "maxValue", "allowDecimals", "noDataText", "onValueChange", "enableLegendSlider", "customTooltip", "loading", "className", "width", "height", "style", "shape", "fillOpacity", "showRadiusAxis", "dimSecondarySeries", "secondaryOpacity", "angleAxisLabelFormatter", "xAxisLabelFormatter", "yAxisDomain"]; function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); } function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); } function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } 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) { _defineProperty(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; } function _defineProperty(obj, key, value) { key = _toPropertyKey(key); if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } function _toPropertyKey(t) { var i = _toPrimitive(t, "string"); return "symbol" == _typeof(i) ? i : String(i); } function _toPrimitive(t, r) { if ("object" != _typeof(t) || !t) return t; var e = t[Symbol.toPrimitive]; if (void 0 !== e) { var i = e.call(t, r || "default"); if ("object" != _typeof(i)) return i; throw new TypeError("@@toPrimitive must return a primitive value."); } return ("string" === r ? String : Number)(t); } function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); } function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; } function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } } function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; } function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; } import { Flexbox, Skeleton } from '@lobehub/ui'; import { cssVar } from 'antd-style'; import { forwardRef, useState } from 'react'; import { Legend, PolarAngleAxis, PolarGrid, PolarRadiusAxis, Radar, RadarChart as ReChartsRadarChart, ResponsiveContainer, Tooltip } from 'recharts'; import ChartLegend from "../common/ChartLegend"; import ChartTooltip from "../common/ChartTooltip"; import NoData from "../common/NoData"; import { constructCategoryColors, getYAxisDomain } from "../common/utils"; import { useThemeColorRange } from "../hooks/useThemeColorRange"; import { defaultValueFormatter } from "../utils"; import { styles } from "./styles"; import { jsx as _jsx } from "react/jsx-runtime"; import { jsxs as _jsxs } from "react/jsx-runtime"; var RadarChart = /*#__PURE__*/forwardRef(function (props, ref) { var themeColorRange = useThemeColorRange(); var _props$data = props.data, data = _props$data === void 0 ? [] : _props$data, _props$categories = props.categories, categories = _props$categories === void 0 ? [] : _props$categories, customCategories = props.customCategories, index = props.index, _props$colors = props.colors, colors = _props$colors === void 0 ? themeColorRange : _props$colors, _props$valueFormatter = props.valueFormatter, valueFormatter = _props$valueFormatter === void 0 ? defaultValueFormatter : _props$valueFormatter, _props$animationDurat = props.animationDuration, animationDuration = _props$animationDurat === void 0 ? 900 : _props$animationDurat, _props$showAnimation = props.showAnimation, showAnimation = _props$showAnimation === void 0 ? false : _props$showAnimation, _props$showTooltip = props.showTooltip, showTooltip = _props$showTooltip === void 0 ? true : _props$showTooltip, _props$showLegend = props.showLegend, showLegend = _props$showLegend === void 0 ? true : _props$showLegend, _props$showGridLines = props.showGridLines, showGridLines = _props$showGridLines === void 0 ? true : _props$showGridLines, _props$autoMinValue = props.autoMinValue, autoMinValue = _props$autoMinValue === void 0 ? false : _props$autoMinValue, minValue = props.minValue, maxValue = props.maxValue, _props$allowDecimals = props.allowDecimals, allowDecimals = _props$allowDecimals === void 0 ? true : _props$allowDecimals, noDataText = props.noDataText, onValueChange = props.onValueChange, _props$enableLegendSl = props.enableLegendSlider, enableLegendSlider = _props$enableLegendSl === void 0 ? false : _props$enableLegendSl, customTooltip = props.customTooltip, loading = props.loading, className = props.className, _props$width = props.width, width = _props$width === void 0 ? '100%' : _props$width, _props$height = props.height, height = _props$height === void 0 ? 280 : _props$height, style = props.style, _props$shape = props.shape, shape = _props$shape === void 0 ? 'polygon' : _props$shape, _props$fillOpacity = props.fillOpacity, fillOpacity = _props$fillOpacity === void 0 ? 0.33 : _props$fillOpacity, _props$showRadiusAxis = props.showRadiusAxis, showRadiusAxis = _props$showRadiusAxis === void 0 ? false : _props$showRadiusAxis, _props$dimSecondarySe = props.dimSecondarySeries, dimSecondarySeries = _props$dimSecondarySe === void 0 ? true : _props$dimSecondarySe, _props$secondaryOpaci = props.secondaryOpacity, secondaryOpacity = _props$secondaryOpaci === void 0 ? 0.25 : _props$secondaryOpaci, angleAxisLabelFormatter = props.angleAxisLabelFormatter, xAxisLabelFormatter = props.xAxisLabelFormatter, yAxisDomainOverride = props.yAxisDomain, rest = _objectWithoutProperties(props, _excluded); var _useState = useState(), _useState2 = _slicedToArray(_useState, 2), activeLegend = _useState2[0], setActiveLegend = _useState2[1]; var _useState3 = useState(60), _useState4 = _slicedToArray(_useState3, 2), legendHeight = _useState4[0], setLegendHeight = _useState4[1]; if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Block, { active: true, height: height, width: width }); var CustomTooltip = customTooltip; var categoryColors = constructCategoryColors(categories, colors); var hasOnValueChange = !!onValueChange; var yAxisDomain = yAxisDomainOverride !== null && yAxisDomainOverride !== void 0 ? yAxisDomainOverride : getYAxisDomain(autoMinValue, minValue, maxValue); var onCategoryClick = function onCategoryClick(dataKey) { if (!hasOnValueChange) return; if (dataKey === activeLegend) { setActiveLegend(undefined); onValueChange === null || onValueChange === void 0 || onValueChange(null); } else { setActiveLegend(dataKey); onValueChange === null || onValueChange === void 0 || onValueChange({ categoryClicked: dataKey, eventType: 'category' }); } }; return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({ className: className, height: height, ref: ref, style: _objectSpread({ position: 'relative' }, style), width: width }, rest), {}, { children: /*#__PURE__*/_jsx(ResponsiveContainer, { children: data !== null && data !== void 0 && data.length ? /*#__PURE__*/_jsxs(ReChartsRadarChart, { data: data, margin: { bottom: 10, left: 10, right: 10, top: 10 }, onClick: hasOnValueChange && activeLegend ? function () { setActiveLegend(undefined); onValueChange === null || onValueChange === void 0 || onValueChange(null); } : undefined, children: [showGridLines ? /*#__PURE__*/_jsx(PolarGrid, { className: styles.gridLines, fill: "none", gridType: shape, stroke: cssVar.colorBorderSecondary }) : undefined, /*#__PURE__*/_jsx(PolarAngleAxis, { axisLine: false, className: styles.label, dataKey: index, tick: function tick(tickProps) { var x = tickProps.x, y = tickProps.y, textAnchor = tickProps.textAnchor, tickPayload = tickProps.payload; var value = tickPayload === null || tickPayload === void 0 ? void 0 : tickPayload.value; var row = data.find(function (item) { return item[index] === value; }); var content = value; if (angleAxisLabelFormatter) { content = angleAxisLabelFormatter(value, row); } else if (xAxisLabelFormatter) { content = xAxisLabelFormatter(value); } return /*#__PURE__*/_jsx("text", { className: "recharts-polar-angle-axis-tick-value", fill: cssVar.colorTextDescription, fontSize: 12, textAnchor: textAnchor, x: x, y: y, children: content }); }, tickLine: false }), /*#__PURE__*/_jsx(PolarRadiusAxis, { allowDecimals: allowDecimals, axisLine: false, className: styles.radiusLabel, domain: yAxisDomain, stroke: cssVar.colorTextQuaternary, tick: showRadiusAxis, tickFormatter: valueFormatter, tickLine: false }), showTooltip ? /*#__PURE__*/_jsx(Tooltip, { content: function content(_ref) { var active = _ref.active, payload = _ref.payload, label = _ref.label; return CustomTooltip ? /*#__PURE__*/_jsx(CustomTooltip, { active: active, customCategories: customCategories, label: label, payload: payload === null || payload === void 0 ? void 0 : payload.map(function (payloadItem) { var _categoryColors$get; return _objectSpread(_objectSpread({}, payloadItem), {}, { color: (_categoryColors$get = categoryColors.get(payloadItem.dataKey)) !== null && _categoryColors$get !== void 0 ? _categoryColors$get : cssVar.colorPrimary }); }), valueFormatter: valueFormatter }) : /*#__PURE__*/_jsx(ChartTooltip, { active: active, categoryColors: categoryColors, customCategories: customCategories, label: label, payload: payload, valueFormatter: valueFormatter }); }, isAnimationActive: false, wrapperStyle: { outline: 'none' } }) : undefined, showLegend ? /*#__PURE__*/_jsx(Legend, { content: function content(_ref2) { var payload = _ref2.payload; return ChartLegend({ payload: payload === null || payload === void 0 ? void 0 : payload.slice().reverse() }, categoryColors, setLegendHeight, activeLegend, hasOnValueChange ? function (clickedLegendItem) { return onCategoryClick(clickedLegendItem); } : undefined, enableLegendSlider, customCategories); }, height: legendHeight, verticalAlign: "top" }) : undefined, _toConsumableArray(categories).reverse().map(function (category) { var _categoryColors$get2; var isInactive = Boolean(activeLegend && activeLegend !== category); var isSecondary = dimSecondarySeries && category !== categories[0]; var seriesFillOpacity = isSecondary ? fillOpacity * secondaryOpacity : fillOpacity; var seriesStrokeOpacity = isSecondary ? secondaryOpacity : 1; var color = (_categoryColors$get2 = categoryColors.get(category)) !== null && _categoryColors$get2 !== void 0 ? _categoryColors$get2 : cssVar.colorPrimary; return /*#__PURE__*/_jsx(Radar, { animationDuration: animationDuration, dataKey: category, fill: color, fillOpacity: isInactive ? seriesFillOpacity * 0.3 : seriesFillOpacity, isAnimationActive: showAnimation, name: category, stroke: color, strokeOpacity: isInactive ? seriesStrokeOpacity * 0.3 : seriesStrokeOpacity, strokeWidth: 2, style: { cursor: onValueChange ? 'pointer' : undefined } }, category); })] }) : /*#__PURE__*/_jsx(NoData, { noDataText: noDataText }) }) })); }); RadarChart.displayName = 'RadarChart'; export default RadarChart;