@lobehub/charts 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +218 -0
  3. package/es/AreaChart/index.d.ts +11 -0
  4. package/es/AreaChart/index.js +388 -0
  5. package/es/AreaChart/styles.d.ts +6 -0
  6. package/es/AreaChart/styles.js +13 -0
  7. package/es/BarChart/index.d.ts +10 -0
  8. package/es/BarChart/index.js +354 -0
  9. package/es/BarChart/renderShape.d.ts +1 -0
  10. package/es/BarChart/renderShape.js +29 -0
  11. package/es/BarChart/styles.d.ts +6 -0
  12. package/es/BarChart/styles.js +13 -0
  13. package/es/DonutChart/DonutChartTooltip.d.ts +7 -0
  14. package/es/DonutChart/DonutChartTooltip.js +25 -0
  15. package/es/DonutChart/index.d.ts +25 -0
  16. package/es/DonutChart/index.js +186 -0
  17. package/es/DonutChart/inputParser.d.ts +2 -0
  18. package/es/DonutChart/inputParser.js +9 -0
  19. package/es/DonutChart/renderInactiveShape.d.ts +1 -0
  20. package/es/DonutChart/renderInactiveShape.js +27 -0
  21. package/es/DonutChart/styles.d.ts +6 -0
  22. package/es/DonutChart/styles.js +13 -0
  23. package/es/LineChart/index.d.ts +9 -0
  24. package/es/LineChart/index.js +347 -0
  25. package/es/LineChart/styles.d.ts +6 -0
  26. package/es/LineChart/styles.js +13 -0
  27. package/es/ScatterChart/ScatterChartTooltip.d.ts +13 -0
  28. package/es/ScatterChart/ScatterChartTooltip.js +83 -0
  29. package/es/ScatterChart/ScatterChartTooltipRow.d.ts +7 -0
  30. package/es/ScatterChart/ScatterChartTooltipRow.js +43 -0
  31. package/es/ScatterChart/index.d.ts +51 -0
  32. package/es/ScatterChart/index.js +304 -0
  33. package/es/ScatterChart/renderShape.d.ts +1 -0
  34. package/es/ScatterChart/renderShape.js +18 -0
  35. package/es/ScatterChart/styles.d.ts +6 -0
  36. package/es/ScatterChart/styles.js +13 -0
  37. package/es/common/BaseAnimationTimingProps.d.ts +7 -0
  38. package/es/common/BaseAnimationTimingProps.js +1 -0
  39. package/es/common/BaseChartProps.d.ts +47 -0
  40. package/es/common/BaseChartProps.js +1 -0
  41. package/es/common/ChartLegend.d.ts +3 -0
  42. package/es/common/ChartLegend.js +39 -0
  43. package/es/common/ChartTooltip/ChartTooltipFrame.d.ts +7 -0
  44. package/es/common/ChartTooltip/ChartTooltipFrame.js +28 -0
  45. package/es/common/ChartTooltip/ChartTooltipRow.d.ts +8 -0
  46. package/es/common/ChartTooltip/ChartTooltipRow.js +58 -0
  47. package/es/common/ChartTooltip/index.d.ts +10 -0
  48. package/es/common/ChartTooltip/index.js +65 -0
  49. package/es/common/CustomTooltipProps.d.ts +6 -0
  50. package/es/common/CustomTooltipProps.js +1 -0
  51. package/es/common/NoData.d.ts +10 -0
  52. package/es/common/NoData.js +43 -0
  53. package/es/common/utils.d.ts +5 -0
  54. package/es/common/utils.js +77 -0
  55. package/es/components/Legend/LegendItem.d.ts +9 -0
  56. package/es/components/Legend/LegendItem.js +65 -0
  57. package/es/components/Legend/ScrollButton.d.ts +9 -0
  58. package/es/components/Legend/ScrollButton.js +58 -0
  59. package/es/components/Legend/index.d.ts +11 -0
  60. package/es/components/Legend/index.js +173 -0
  61. package/es/hooks/useOnWindowResize.d.ts +3 -0
  62. package/es/hooks/useOnWindowResize.js +14 -0
  63. package/es/hooks/useThemeColorRange.d.ts +1 -0
  64. package/es/hooks/useThemeColorRange.js +5 -0
  65. package/es/index.d.ts +8 -0
  66. package/es/index.js +7 -0
  67. package/es/types.d.ts +20 -0
  68. package/es/types.js +1 -0
  69. package/es/utils/index.d.ts +3 -0
  70. package/es/utils/index.js +8 -0
  71. package/package.json +119 -0
@@ -0,0 +1,354 @@
1
+ 'use client';
2
+
3
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
4
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
7
+ var _templateObject;
8
+ var _excluded = ["data", "categories", "index", "colors", "valueFormatter", "layout", "stack", "relative", "startEndOnly", "animationDuration", "showAnimation", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "showTooltip", "showLegend", "showGridLines", "autoMinValue", "minValue", "maxValue", "allowDecimals", "noDataText", "onValueChange", "enableLegendSlider", "customTooltip", "rotateLabelX", "barCategoryGap", "tickGap", "xAxisLabel", "yAxisLabel", "className", "width", "height"];
9
+ import { css } from 'antd-style';
10
+ import { forwardRef, useState } from 'react';
11
+ import { Flexbox } from 'react-layout-kit';
12
+ import { Bar, CartesianGrid, Label, Legend, BarChart as ReChartsBarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
13
+ import ChartLegend from "../common/ChartLegend";
14
+ import ChartTooltip from "../common/ChartTooltip";
15
+ import NoData from "../common/NoData";
16
+ import { constructCategoryColors, deepEqual, getYAxisDomain } from "../common/utils";
17
+ import { useThemeColorRange } from "../hooks/useThemeColorRange";
18
+ import { defaultValueFormatter } from "../utils";
19
+ import { renderShape } from "./renderShape";
20
+ import { useStyles } from "./styles";
21
+ import { jsx as _jsx } from "react/jsx-runtime";
22
+ import { jsxs as _jsxs } from "react/jsx-runtime";
23
+ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
24
+ var _useStyles = useStyles(),
25
+ cx = _useStyles.cx,
26
+ theme = _useStyles.theme,
27
+ styles = _useStyles.styles;
28
+ var themeColorRange = useThemeColorRange();
29
+ var _props$data = props.data,
30
+ data = _props$data === void 0 ? [] : _props$data,
31
+ _props$categories = props.categories,
32
+ categories = _props$categories === void 0 ? [] : _props$categories,
33
+ index = props.index,
34
+ _props$colors = props.colors,
35
+ colors = _props$colors === void 0 ? themeColorRange : _props$colors,
36
+ _props$valueFormatter = props.valueFormatter,
37
+ valueFormatter = _props$valueFormatter === void 0 ? defaultValueFormatter : _props$valueFormatter,
38
+ _props$layout = props.layout,
39
+ layout = _props$layout === void 0 ? 'horizontal' : _props$layout,
40
+ _props$stack = props.stack,
41
+ stack = _props$stack === void 0 ? false : _props$stack,
42
+ _props$relative = props.relative,
43
+ relative = _props$relative === void 0 ? false : _props$relative,
44
+ _props$startEndOnly = props.startEndOnly,
45
+ startEndOnly = _props$startEndOnly === void 0 ? false : _props$startEndOnly,
46
+ _props$animationDurat = props.animationDuration,
47
+ animationDuration = _props$animationDurat === void 0 ? 900 : _props$animationDurat,
48
+ _props$showAnimation = props.showAnimation,
49
+ showAnimation = _props$showAnimation === void 0 ? false : _props$showAnimation,
50
+ _props$showXAxis = props.showXAxis,
51
+ showXAxis = _props$showXAxis === void 0 ? true : _props$showXAxis,
52
+ _props$showYAxis = props.showYAxis,
53
+ showYAxis = _props$showYAxis === void 0 ? true : _props$showYAxis,
54
+ _props$yAxisWidth = props.yAxisWidth,
55
+ yAxisWidth = _props$yAxisWidth === void 0 ? 56 : _props$yAxisWidth,
56
+ _props$intervalType = props.intervalType,
57
+ intervalType = _props$intervalType === void 0 ? 'equidistantPreserveStart' : _props$intervalType,
58
+ _props$showTooltip = props.showTooltip,
59
+ showTooltip = _props$showTooltip === void 0 ? true : _props$showTooltip,
60
+ _props$showLegend = props.showLegend,
61
+ showLegend = _props$showLegend === void 0 ? true : _props$showLegend,
62
+ _props$showGridLines = props.showGridLines,
63
+ showGridLines = _props$showGridLines === void 0 ? true : _props$showGridLines,
64
+ _props$autoMinValue = props.autoMinValue,
65
+ autoMinValue = _props$autoMinValue === void 0 ? false : _props$autoMinValue,
66
+ minValue = props.minValue,
67
+ maxValue = props.maxValue,
68
+ _props$allowDecimals = props.allowDecimals,
69
+ allowDecimals = _props$allowDecimals === void 0 ? true : _props$allowDecimals,
70
+ noDataText = props.noDataText,
71
+ onValueChange = props.onValueChange,
72
+ _props$enableLegendSl = props.enableLegendSlider,
73
+ enableLegendSlider = _props$enableLegendSl === void 0 ? false : _props$enableLegendSl,
74
+ customTooltip = props.customTooltip,
75
+ rotateLabelX = props.rotateLabelX,
76
+ barCategoryGap = props.barCategoryGap,
77
+ _props$tickGap = props.tickGap,
78
+ tickGap = _props$tickGap === void 0 ? 5 : _props$tickGap,
79
+ xAxisLabel = props.xAxisLabel,
80
+ yAxisLabel = props.yAxisLabel,
81
+ className = props.className,
82
+ _props$width = props.width,
83
+ width = _props$width === void 0 ? '100%' : _props$width,
84
+ _props$height = props.height,
85
+ height = _props$height === void 0 ? '20rem' : _props$height,
86
+ rest = _objectWithoutProperties(props, _excluded);
87
+ var CustomTooltip = customTooltip;
88
+ var paddingValue = !showXAxis && !showYAxis ? 0 : 20;
89
+ var _useState = useState(60),
90
+ _useState2 = _slicedToArray(_useState, 2),
91
+ legendHeight = _useState2[0],
92
+ setLegendHeight = _useState2[1];
93
+ var categoryColors = constructCategoryColors(categories, colors);
94
+ var _useState3 = useState(),
95
+ _useState4 = _slicedToArray(_useState3, 2),
96
+ activeBar = _useState4[0],
97
+ setActiveBar = _useState4[1];
98
+ var _useState5 = useState(),
99
+ _useState6 = _slicedToArray(_useState5, 2),
100
+ activeLegend = _useState6[0],
101
+ setActiveLegend = _useState6[1];
102
+ var hasOnValueChange = !!onValueChange;
103
+ var onBarClick = function onBarClick(data, idx, event) {
104
+ event.stopPropagation();
105
+ if (!onValueChange) return;
106
+ if (deepEqual(activeBar, _objectSpread(_objectSpread({}, data.payload), {}, {
107
+ value: data.value
108
+ }))) {
109
+ setActiveLegend(undefined);
110
+ setActiveBar(undefined);
111
+ onValueChange === null || onValueChange === void 0 || onValueChange(null);
112
+ } else {
113
+ var _data$tooltipPayload, _data$tooltipPayload2;
114
+ setActiveLegend((_data$tooltipPayload = data.tooltipPayload) === null || _data$tooltipPayload === void 0 || (_data$tooltipPayload = _data$tooltipPayload[0]) === null || _data$tooltipPayload === void 0 ? void 0 : _data$tooltipPayload.dataKey);
115
+ setActiveBar(_objectSpread(_objectSpread({}, data.payload), {}, {
116
+ value: data.value
117
+ }));
118
+ onValueChange === null || onValueChange === void 0 || onValueChange(_objectSpread({
119
+ categoryClicked: (_data$tooltipPayload2 = data.tooltipPayload) === null || _data$tooltipPayload2 === void 0 || (_data$tooltipPayload2 = _data$tooltipPayload2[0]) === null || _data$tooltipPayload2 === void 0 ? void 0 : _data$tooltipPayload2.dataKey,
120
+ eventType: 'bar'
121
+ }, data.payload));
122
+ }
123
+ };
124
+ var onCategoryClick = function onCategoryClick(dataKey) {
125
+ if (!hasOnValueChange) return;
126
+ if (dataKey === activeLegend && !activeBar) {
127
+ setActiveLegend(undefined);
128
+ onValueChange === null || onValueChange === void 0 || onValueChange(null);
129
+ } else {
130
+ setActiveLegend(dataKey);
131
+ onValueChange === null || onValueChange === void 0 || onValueChange({
132
+ categoryClicked: dataKey,
133
+ eventType: 'category'
134
+ });
135
+ }
136
+ setActiveBar(undefined);
137
+ };
138
+ var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
139
+ return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
140
+ className: className,
141
+ height: height,
142
+ ref: ref,
143
+ width: width
144
+ }, rest), {}, {
145
+ children: /*#__PURE__*/_jsx(ResponsiveContainer, {
146
+ children: data !== null && data !== void 0 && data.length ? /*#__PURE__*/_jsxs(ReChartsBarChart, {
147
+ barCategoryGap: barCategoryGap,
148
+ data: data,
149
+ layout: layout === 'vertical' ? 'vertical' : 'horizontal',
150
+ margin: {
151
+ bottom: xAxisLabel ? 30 : undefined,
152
+ left: yAxisLabel ? 20 : undefined,
153
+ right: yAxisLabel ? 5 : undefined,
154
+ top: 5
155
+ },
156
+ onClick: hasOnValueChange && (activeLegend || activeBar) ? function () {
157
+ setActiveBar(undefined);
158
+ setActiveLegend(undefined);
159
+ onValueChange === null || onValueChange === void 0 || onValueChange(null);
160
+ } : undefined,
161
+ stackOffset: stack ? 'sign' : relative ? 'expand' : 'none',
162
+ children: [showGridLines ? /*#__PURE__*/_jsx(CartesianGrid, {
163
+ className: styles.gridLines,
164
+ horizontal: layout !== 'vertical',
165
+ vertical: layout === 'vertical'
166
+ }) : undefined, layout === 'vertical' ? /*#__PURE__*/_jsx(XAxis, {
167
+ allowDecimals: allowDecimals,
168
+ angle: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.angle,
169
+ axisLine: false,
170
+ className: styles.label,
171
+ domain: yAxisDomain,
172
+ dy: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.verticalShift,
173
+ fill: "",
174
+ height: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.xAxisHeight,
175
+ hide: !showXAxis,
176
+ minTickGap: tickGap,
177
+ stroke: "",
178
+ tick: {
179
+ transform: 'translate(-3, 0)'
180
+ },
181
+ tickFormatter: valueFormatter,
182
+ tickLine: false,
183
+ type: "number",
184
+ children: xAxisLabel && /*#__PURE__*/_jsx(Label, {
185
+ className: cx(styles.strongLabel, styles.emphasis),
186
+ offset: -20,
187
+ position: "insideBottom",
188
+ style: {
189
+ fill: theme.colorTextSecondary,
190
+ fontWeight: 500,
191
+ textAnchor: 'middle'
192
+ },
193
+ children: xAxisLabel
194
+ })
195
+ }) : /*#__PURE__*/_jsx(XAxis, {
196
+ angle: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.angle,
197
+ axisLine: false,
198
+ className: styles.label,
199
+ dataKey: index,
200
+ dy: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.verticalShift,
201
+ fill: "",
202
+ height: rotateLabelX === null || rotateLabelX === void 0 ? void 0 : rotateLabelX.xAxisHeight,
203
+ hide: !showXAxis,
204
+ interval: startEndOnly ? 'preserveStartEnd' : intervalType,
205
+ minTickGap: tickGap,
206
+ padding: {
207
+ left: paddingValue,
208
+ right: paddingValue
209
+ },
210
+ stroke: "",
211
+ tick: {
212
+ transform: 'translate(0, 6)'
213
+ },
214
+ tickLine: false,
215
+ ticks: startEndOnly ? [data[0][index], data.at(-1)[index]] : undefined,
216
+ children: xAxisLabel && /*#__PURE__*/_jsx(Label, {
217
+ className: cx(styles.strongLabel, styles.emphasis),
218
+ offset: -20,
219
+ position: "insideBottom",
220
+ style: {
221
+ fill: theme.colorTextSecondary,
222
+ fontWeight: 500,
223
+ textAnchor: 'middle'
224
+ },
225
+ children: xAxisLabel
226
+ })
227
+ }), layout === 'vertical' ? /*#__PURE__*/_jsx(YAxis, {
228
+ axisLine: false,
229
+ className: styles.label,
230
+ dataKey: index,
231
+ fill: "",
232
+ hide: !showYAxis,
233
+ interval: "preserveStartEnd",
234
+ stroke: "",
235
+ tick: {
236
+ transform: 'translate(0, 6)'
237
+ },
238
+ tickLine: false,
239
+ ticks: startEndOnly ? [data[0][index], data.at(-1)[index]] : undefined,
240
+ type: "category",
241
+ width: yAxisWidth,
242
+ children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
243
+ angle: -90,
244
+ className: styles.emphasis,
245
+ offset: -15,
246
+ position: "insideLeft",
247
+ style: {
248
+ fill: theme.colorTextSecondary,
249
+ fontWeight: 500,
250
+ textAnchor: 'middle'
251
+ },
252
+ children: yAxisLabel
253
+ })
254
+ }) : /*#__PURE__*/_jsx(YAxis, {
255
+ allowDecimals: allowDecimals,
256
+ axisLine: false,
257
+ className: styles.label,
258
+ domain: yAxisDomain,
259
+ fill: "",
260
+ hide: !showYAxis,
261
+ stroke: "",
262
+ tick: {
263
+ transform: 'translate(-3, 0)'
264
+ },
265
+ tickFormatter: relative ? function (value) {
266
+ return "".concat((value * 100).toString(), " %");
267
+ } : valueFormatter,
268
+ tickLine: false,
269
+ type: "number",
270
+ width: yAxisWidth,
271
+ children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
272
+ angle: -90,
273
+ className: styles.emphasis,
274
+ offset: -15,
275
+ position: "insideLeft",
276
+ style: {
277
+ fill: theme.colorTextSecondary,
278
+ fontWeight: 500,
279
+ textAnchor: 'middle'
280
+ },
281
+ children: yAxisLabel
282
+ })
283
+ }), /*#__PURE__*/_jsx(Tooltip, {
284
+ content: showTooltip ? function (_ref) {
285
+ var active = _ref.active,
286
+ payload = _ref.payload,
287
+ label = _ref.label;
288
+ return CustomTooltip ? /*#__PURE__*/_jsx(CustomTooltip, {
289
+ active: active,
290
+ label: label,
291
+ payload: payload === null || payload === void 0 ? void 0 : payload.map(function (payloadItem) {
292
+ var _categoryColors$get;
293
+ return _objectSpread(_objectSpread({}, payloadItem), {}, {
294
+ color: (_categoryColors$get = categoryColors.get(payloadItem.dataKey)) !== null && _categoryColors$get !== void 0 ? _categoryColors$get : theme.colorPrimary
295
+ });
296
+ })
297
+ }) : /*#__PURE__*/_jsx(ChartTooltip, {
298
+ active: active,
299
+ categoryColors: categoryColors,
300
+ label: label,
301
+ payload: payload,
302
+ valueFormatter: valueFormatter
303
+ });
304
+ } : undefined,
305
+ cursor: {
306
+ fill: theme.colorFillTertiary
307
+ },
308
+ isAnimationActive: false,
309
+ position: {
310
+ y: 0
311
+ },
312
+ wrapperStyle: {
313
+ outline: 'none'
314
+ }
315
+ }), showLegend ? /*#__PURE__*/_jsx(Legend, {
316
+ content: function content(_ref2) {
317
+ var payload = _ref2.payload;
318
+ return ChartLegend({
319
+ payload: payload
320
+ }, categoryColors, setLegendHeight, activeLegend, hasOnValueChange ? function (clickedLegendItem) {
321
+ return onCategoryClick(clickedLegendItem);
322
+ } : undefined, enableLegendSlider);
323
+ },
324
+ height: legendHeight,
325
+ verticalAlign: "top"
326
+ }) : undefined, categories.map(function (category) {
327
+ var _categoryColors$get2;
328
+ console.log(categoryColors.get(category));
329
+ return /*#__PURE__*/_jsx(Bar, {
330
+ animationDuration: animationDuration,
331
+ className: cx(css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n fill: ", ";\n "])), (_categoryColors$get2 = categoryColors.get(category)) !== null && _categoryColors$get2 !== void 0 ? _categoryColors$get2 : theme.colorPrimary)),
332
+ dataKey: category,
333
+ fill: '',
334
+ isAnimationActive: showAnimation,
335
+ name: category,
336
+ onClick: onBarClick,
337
+ shape: function shape(props) {
338
+ return renderShape(props, activeBar, activeLegend, layout);
339
+ },
340
+ stackId: stack || relative ? 'a' : undefined,
341
+ style: {
342
+ cursor: onValueChange ? 'pointer' : undefined
343
+ },
344
+ type: "linear"
345
+ }, category);
346
+ })]
347
+ }) : /*#__PURE__*/_jsx(NoData, {
348
+ noDataText: noDataText
349
+ })
350
+ })
351
+ }));
352
+ });
353
+ BarChart.displayName = 'BarChart';
354
+ export default BarChart;
@@ -0,0 +1 @@
1
+ export declare const renderShape: (props: any, activeBar: any | undefined, activeLegend: string | undefined, layout: string) => import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,29 @@
1
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
2
+ import { deepEqual } from "../common/utils";
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ export var renderShape = function renderShape(props, activeBar, activeLegend, layout) {
5
+ var fillOpacity = props.fillOpacity,
6
+ name = props.name,
7
+ payload = props.payload,
8
+ value = props.value;
9
+ var x = props.x,
10
+ width = props.width,
11
+ y = props.y,
12
+ height = props.height;
13
+ if (layout === 'horizontal' && height < 0) {
14
+ y += height;
15
+ height = Math.abs(height); // height must be a positive number
16
+ } else if (layout === 'vertical' && width < 0) {
17
+ x += width;
18
+ width = Math.abs(width); // width must be a positive number
19
+ }
20
+ return /*#__PURE__*/_jsx("rect", {
21
+ height: height,
22
+ opacity: activeBar || activeLegend && activeLegend !== name ? deepEqual(activeBar, _objectSpread(_objectSpread({}, payload), {}, {
23
+ value: value
24
+ })) ? fillOpacity : 0.3 : fillOpacity,
25
+ width: width,
26
+ x: x,
27
+ y: y
28
+ });
29
+ };
@@ -0,0 +1,6 @@
1
+ export declare const useStyles: (props?: unknown) => import("antd-style").ReturnStyles<{
2
+ emphasis: import("antd-style").SerializedStyles;
3
+ gridLines: import("antd-style").SerializedStyles;
4
+ label: import("antd-style").SerializedStyles;
5
+ strongLabel: import("antd-style").SerializedStyles;
6
+ }>;
@@ -0,0 +1,13 @@
1
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
2
+ var _templateObject, _templateObject2, _templateObject3, _templateObject4;
3
+ import { createStyles } from 'antd-style';
4
+ export var useStyles = createStyles(function (_ref) {
5
+ var css = _ref.css,
6
+ token = _ref.token;
7
+ return {
8
+ emphasis: css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n "]))),
9
+ gridLines: css(_templateObject2 || (_templateObject2 = _taggedTemplateLiteral(["\n stroke: ", ";\n stroke-width: 1;\n "])), token.colorBorderSecondary),
10
+ label: css(_templateObject3 || (_templateObject3 = _taggedTemplateLiteral(["\n font-size: 14px;\n line-height: 16px;\n fill: ", ";\n "])), token.colorTextDescription),
11
+ strongLabel: css(_templateObject4 || (_templateObject4 = _taggedTemplateLiteral(["\n font-size: 14px;\n font-weight: 500;\n line-height: 16px;\n fill: ", ";\n "])), token.colorTextSecondary)
12
+ };
13
+ });
@@ -0,0 +1,7 @@
1
+ import { ValueFormatter } from "../types";
2
+ export interface DonutChartTooltipProps {
3
+ active: boolean | undefined;
4
+ payload: any;
5
+ valueFormatter: ValueFormatter;
6
+ }
7
+ export declare const DonutChartTooltip: ({ active, payload, valueFormatter }: DonutChartTooltipProps) => import("react/jsx-runtime").JSX.Element | null;
@@ -0,0 +1,25 @@
1
+ import React from 'react';
2
+ import { Flexbox } from 'react-layout-kit';
3
+ import ChartTooltipFrame from "../common/ChartTooltip/ChartTooltipFrame";
4
+ import ChartTooltipRow from "../common/ChartTooltip/ChartTooltipRow";
5
+ import { jsx as _jsx } from "react/jsx-runtime";
6
+ export var DonutChartTooltip = function DonutChartTooltip(_ref) {
7
+ var active = _ref.active,
8
+ payload = _ref.payload,
9
+ valueFormatter = _ref.valueFormatter;
10
+ if (active && payload !== null && payload !== void 0 && payload[0]) {
11
+ var payloadRow = payload === null || payload === void 0 ? void 0 : payload[0];
12
+ return /*#__PURE__*/_jsx(ChartTooltipFrame, {
13
+ children: /*#__PURE__*/_jsx(Flexbox, {
14
+ paddingBlock: 8,
15
+ paddingInline: 16,
16
+ children: /*#__PURE__*/_jsx(ChartTooltipRow, {
17
+ color: payloadRow.payload.color,
18
+ name: payloadRow.name,
19
+ value: valueFormatter(payloadRow.value)
20
+ })
21
+ })
22
+ });
23
+ }
24
+ return null;
25
+ };
@@ -0,0 +1,25 @@
1
+ import { ComponentType } from 'react';
2
+ import type BaseAnimationTimingProps from "../common/BaseAnimationTimingProps";
3
+ import { CustomTooltipProps } from "../common/CustomTooltipProps";
4
+ import { type NoDataProps } from "../common/NoData";
5
+ import type { EventProps } from "../types";
6
+ import { ValueFormatter } from "../types";
7
+ type DonutChartVariant = 'donut' | 'pie';
8
+ export interface DonutChartProps extends BaseAnimationTimingProps {
9
+ category?: string;
10
+ className?: string;
11
+ colors?: string[];
12
+ customTooltip?: ComponentType<CustomTooltipProps>;
13
+ data: any[];
14
+ index?: string;
15
+ label?: string;
16
+ noDataText?: NoDataProps['noDataText'];
17
+ onValueChange?: (value: EventProps) => void;
18
+ showAnimation?: boolean;
19
+ showLabel?: boolean;
20
+ showTooltip?: boolean;
21
+ valueFormatter?: ValueFormatter;
22
+ variant?: DonutChartVariant;
23
+ }
24
+ declare const DonutChart: import("react").ForwardRefExoticComponent<DonutChartProps & import("react").RefAttributes<HTMLDivElement>>;
25
+ export default DonutChart;
@@ -0,0 +1,186 @@
1
+ 'use client';
2
+
3
+ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLiteral";
4
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
5
+ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
6
+ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
7
+ import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
8
+ var _templateObject;
9
+ var _excluded = ["data", "category", "index", "colors", "variant", "valueFormatter", "label", "showLabel", "animationDuration", "showAnimation", "showTooltip", "noDataText", "onValueChange", "customTooltip", "className", "width", "height"];
10
+ import { css } from 'antd-style';
11
+ import { forwardRef, useEffect, useState } from 'react';
12
+ import { Flexbox } from 'react-layout-kit';
13
+ import { Pie, PieChart as ReChartsDonutChart, ResponsiveContainer, Tooltip } from 'recharts';
14
+ import NoData from "../common/NoData";
15
+ import { useThemeColorRange } from "../hooks/useThemeColorRange";
16
+ import { defaultValueFormatter } from "../utils";
17
+ import { DonutChartTooltip } from "./DonutChartTooltip";
18
+ import { parseLabelInput } from "./inputParser";
19
+ import { renderInactiveShape } from "./renderInactiveShape";
20
+ import { useStyles } from "./styles";
21
+ import { jsx as _jsx } from "react/jsx-runtime";
22
+ import { jsxs as _jsxs } from "react/jsx-runtime";
23
+ var DonutChart = /*#__PURE__*/forwardRef(function (props, ref) {
24
+ var _useStyles = useStyles(),
25
+ cx = _useStyles.cx,
26
+ theme = _useStyles.theme,
27
+ styles = _useStyles.styles;
28
+ var themeColorRange = useThemeColorRange();
29
+ var _props$data = props.data,
30
+ data = _props$data === void 0 ? [] : _props$data,
31
+ _props$category = props.category,
32
+ category = _props$category === void 0 ? 'value' : _props$category,
33
+ _props$index = props.index,
34
+ index = _props$index === void 0 ? 'name' : _props$index,
35
+ _props$colors = props.colors,
36
+ colors = _props$colors === void 0 ? themeColorRange : _props$colors,
37
+ _props$variant = props.variant,
38
+ variant = _props$variant === void 0 ? 'donut' : _props$variant,
39
+ _props$valueFormatter = props.valueFormatter,
40
+ valueFormatter = _props$valueFormatter === void 0 ? defaultValueFormatter : _props$valueFormatter,
41
+ label = props.label,
42
+ _props$showLabel = props.showLabel,
43
+ showLabel = _props$showLabel === void 0 ? true : _props$showLabel,
44
+ _props$animationDurat = props.animationDuration,
45
+ animationDuration = _props$animationDurat === void 0 ? 900 : _props$animationDurat,
46
+ _props$showAnimation = props.showAnimation,
47
+ showAnimation = _props$showAnimation === void 0 ? false : _props$showAnimation,
48
+ _props$showTooltip = props.showTooltip,
49
+ showTooltip = _props$showTooltip === void 0 ? true : _props$showTooltip,
50
+ noDataText = props.noDataText,
51
+ onValueChange = props.onValueChange,
52
+ customTooltip = props.customTooltip,
53
+ className = props.className,
54
+ _props$width = props.width,
55
+ width = _props$width === void 0 ? '100%' : _props$width,
56
+ _props$height = props.height,
57
+ height = _props$height === void 0 ? '10rem' : _props$height,
58
+ rest = _objectWithoutProperties(props, _excluded);
59
+ var CustomTooltip = customTooltip;
60
+ var isDonut = variant === 'donut';
61
+ var parsedLabelInput = parseLabelInput(label, valueFormatter, data, category);
62
+ var _useState = useState(),
63
+ _useState2 = _slicedToArray(_useState, 2),
64
+ activeIndex = _useState2[0],
65
+ setActiveIndex = _useState2[1];
66
+ var hasOnValueChange = !!onValueChange;
67
+ function onShapeClick(data, index, event) {
68
+ event.stopPropagation();
69
+ if (!hasOnValueChange) return;
70
+ if (activeIndex === index) {
71
+ setActiveIndex(undefined);
72
+ onValueChange === null || onValueChange === void 0 || onValueChange(null);
73
+ } else {
74
+ setActiveIndex(index);
75
+ onValueChange === null || onValueChange === void 0 || onValueChange(_objectSpread({
76
+ eventType: 'slice'
77
+ }, data.payload.payload));
78
+ }
79
+ }
80
+ useEffect(function () {
81
+ var pieSectors = document.querySelectorAll('.recharts-pie-sector');
82
+ if (pieSectors) {
83
+ var _iterator = _createForOfIteratorHelper(pieSectors),
84
+ _step;
85
+ try {
86
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
87
+ var sector = _step.value;
88
+ sector.setAttribute('style', 'outline: none');
89
+ }
90
+ } catch (err) {
91
+ _iterator.e(err);
92
+ } finally {
93
+ _iterator.f();
94
+ }
95
+ }
96
+ }, [activeIndex]);
97
+ var parseData = function parseData(data, colors) {
98
+ return data.map(function (dataPoint, idx) {
99
+ var baseColor = idx < colors.length ? colors[idx] : theme.colorPrimary;
100
+ return _objectSpread(_objectSpread({}, dataPoint), {}, {
101
+ className: cx(css(_templateObject || (_templateObject = _taggedTemplateLiteral(["\n fill: ", ";\n "])), baseColor !== null && baseColor !== void 0 ? baseColor : theme.colorPrimary)),
102
+ color: baseColor,
103
+ fill: ''
104
+ });
105
+ });
106
+ };
107
+ return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
108
+ className: className,
109
+ height: height,
110
+ ref: ref,
111
+ width: width
112
+ }, rest), {}, {
113
+ children: /*#__PURE__*/_jsx(ResponsiveContainer, {
114
+ children: data !== null && data !== void 0 && data.length ? /*#__PURE__*/_jsxs(ReChartsDonutChart, {
115
+ margin: {
116
+ bottom: 0,
117
+ left: 0,
118
+ right: 0,
119
+ top: 0
120
+ },
121
+ onClick: hasOnValueChange && activeIndex ? function () {
122
+ setActiveIndex(undefined);
123
+ onValueChange === null || onValueChange === void 0 || onValueChange(null);
124
+ } : undefined,
125
+ children: [showLabel && isDonut ? /*#__PURE__*/_jsx("text", {
126
+ className: cx(styles.strongLabel, styles.emphasis),
127
+ dominantBaseline: "middle",
128
+ textAnchor: "middle",
129
+ x: "50%",
130
+ y: "50%",
131
+ children: parsedLabelInput
132
+ }) : null, /*#__PURE__*/_jsx(Pie, {
133
+ activeIndex: activeIndex,
134
+ animationDuration: animationDuration,
135
+ cx: "50%",
136
+ cy: "50%",
137
+ data: parseData(data, colors),
138
+ dataKey: category,
139
+ endAngle: -270,
140
+ inactiveShape: renderInactiveShape,
141
+ innerRadius: isDonut ? '75%' : '0%',
142
+ isAnimationActive: showAnimation,
143
+ nameKey: index,
144
+ onClick: onShapeClick,
145
+ outerRadius: "100%",
146
+ startAngle: 90,
147
+ stroke: "",
148
+ strokeLinejoin: "round",
149
+ style: {
150
+ cursor: onValueChange ? 'pointer' : undefined,
151
+ outline: 'none',
152
+ stroke: theme.colorBgContainer
153
+ }
154
+ }), /*#__PURE__*/_jsx(Tooltip, {
155
+ content: showTooltip ? function (_ref) {
156
+ var _payload$;
157
+ var active = _ref.active,
158
+ payload = _ref.payload;
159
+ return CustomTooltip ? /*#__PURE__*/_jsx(CustomTooltip, {
160
+ active: active,
161
+ label: payload === null || payload === void 0 || (_payload$ = payload[0]) === null || _payload$ === void 0 ? void 0 : _payload$.name,
162
+ payload: payload === null || payload === void 0 ? void 0 : payload.map(function (payloadItem) {
163
+ var _payload$0$payload$co, _payload$2;
164
+ return _objectSpread(_objectSpread({}, payloadItem), {}, {
165
+ color: (_payload$0$payload$co = payload === null || payload === void 0 || (_payload$2 = payload[0]) === null || _payload$2 === void 0 || (_payload$2 = _payload$2.payload) === null || _payload$2 === void 0 ? void 0 : _payload$2.color) !== null && _payload$0$payload$co !== void 0 ? _payload$0$payload$co : theme.colorPrimary
166
+ });
167
+ })
168
+ }) : /*#__PURE__*/_jsx(DonutChartTooltip, {
169
+ active: active,
170
+ payload: payload,
171
+ valueFormatter: valueFormatter
172
+ });
173
+ } : undefined,
174
+ isAnimationActive: false,
175
+ wrapperStyle: {
176
+ outline: 'none'
177
+ }
178
+ })]
179
+ }) : /*#__PURE__*/_jsx(NoData, {
180
+ noDataText: noDataText
181
+ })
182
+ })
183
+ }));
184
+ });
185
+ DonutChart.displayName = 'DonutChart';
186
+ export default DonutChart;
@@ -0,0 +1,2 @@
1
+ import { ValueFormatter } from "../types";
2
+ export declare const parseLabelInput: (labelInput: string | undefined, valueFormatter: ValueFormatter, data: any[], category: string) => string;
@@ -0,0 +1,9 @@
1
+ import { sumNumericArray } from "../utils";
2
+ var calculateDefaultLabel = function calculateDefaultLabel(data, category) {
3
+ return sumNumericArray(data.map(function (dataPoint) {
4
+ return dataPoint[category];
5
+ }));
6
+ };
7
+ export var parseLabelInput = function parseLabelInput(labelInput, valueFormatter, data, category) {
8
+ return labelInput ? labelInput : valueFormatter(calculateDefaultLabel(data, category));
9
+ };
@@ -0,0 +1 @@
1
+ export declare const renderInactiveShape: (props: any) => import("react/jsx-runtime").JSX.Element;