@lobehub/charts 1.4.1 → 1.5.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.
@@ -4,8 +4,10 @@ import { CurveType } from "../types";
4
4
  export interface AreaChartProps extends BaseChartProps {
5
5
  connectNulls?: boolean;
6
6
  curveType?: CurveType;
7
+ loading?: boolean;
7
8
  showGradient?: boolean;
8
9
  stack?: boolean;
10
+ yAxisAlign?: 'left' | 'right';
9
11
  }
10
12
  declare const AreaChart: import("react").ForwardRefExoticComponent<AreaChartProps & import("react").RefAttributes<HTMLDivElement>>;
11
13
  export default AreaChart;
@@ -5,17 +5,20 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
6
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
7
7
  var _templateObject, _templateObject2, _templateObject3, _templateObject4, _templateObject5;
8
- var _excluded = ["data", "categories", "index", "stack", "colors", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "showAnimation", "animationDuration", "showTooltip", "showLegend", "showGridLines", "showGradient", "autoMinValue", "curveType", "minValue", "maxValue", "connectNulls", "allowDecimals", "noDataText", "className", "onValueChange", "enableLegendSlider", "customTooltip", "rotateLabelX", "tickGap", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories"];
8
+ var _excluded = ["data", "categories", "index", "stack", "colors", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisAlign", "yAxisWidth", "intervalType", "showAnimation", "animationDuration", "showTooltip", "showLegend", "showGridLines", "showGradient", "autoMinValue", "curveType", "minValue", "maxValue", "connectNulls", "allowDecimals", "noDataText", "className", "onValueChange", "enableLegendSlider", "customTooltip", "rotateLabelX", "tickGap", "loading", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories"];
9
+ import { Skeleton } from 'antd';
9
10
  import { css } from 'antd-style';
10
- import { Fragment, forwardRef, useState } from 'react';
11
+ import { Fragment, forwardRef, useMemo, useState } from 'react';
11
12
  import { Flexbox } from 'react-layout-kit';
12
13
  import { Area, CartesianGrid, Dot, Label, Legend, Line, AreaChart as ReChartsAreaChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
13
14
  import ChartLegend from "../common/ChartLegend";
14
15
  import ChartTooltip from "../common/ChartTooltip";
16
+ import CustomYAxisTick from "../common/CustomYAxisTick";
15
17
  import NoData from "../common/NoData";
16
18
  import { constructCategoryColors, getYAxisDomain, hasOnlyOneValueForThisKey } from "../common/utils";
17
19
  import { useThemeColorRange } from "../hooks/useThemeColorRange";
18
20
  import { defaultValueFormatter } from "../utils";
21
+ import { getMaxLabelLength } from "../utils/getMaxLabelLength";
19
22
  import { useStyles } from "./styles";
20
23
  import { jsx as _jsx } from "react/jsx-runtime";
21
24
  import { jsxs as _jsxs } from "react/jsx-runtime";
@@ -42,8 +45,9 @@ var AreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
42
45
  showXAxis = _props$showXAxis === void 0 ? true : _props$showXAxis,
43
46
  _props$showYAxis = props.showYAxis,
44
47
  showYAxis = _props$showYAxis === void 0 ? true : _props$showYAxis,
45
- _props$yAxisWidth = props.yAxisWidth,
46
- yAxisWidth = _props$yAxisWidth === void 0 ? 56 : _props$yAxisWidth,
48
+ _props$yAxisAlign = props.yAxisAlign,
49
+ yAxisAlign = _props$yAxisAlign === void 0 ? 'left' : _props$yAxisAlign,
50
+ yAxisWidth = props.yAxisWidth,
47
51
  _props$intervalType = props.intervalType,
48
52
  intervalType = _props$intervalType === void 0 ? 'equidistantPreserveStart' : _props$intervalType,
49
53
  _props$showAnimation = props.showAnimation,
@@ -77,6 +81,7 @@ var AreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
77
81
  rotateLabelX = props.rotateLabelX,
78
82
  _props$tickGap = props.tickGap,
79
83
  tickGap = _props$tickGap === void 0 ? 5 : _props$tickGap,
84
+ loading = props.loading,
80
85
  xAxisLabel = props.xAxisLabel,
81
86
  yAxisLabel = props.yAxisLabel,
82
87
  _props$width = props.width,
@@ -86,8 +91,6 @@ var AreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
86
91
  style = props.style,
87
92
  customCategories = props.customCategories,
88
93
  rest = _objectWithoutProperties(props, _excluded);
89
- var CustomTooltip = customTooltip;
90
- var paddingValue = !showXAxis && !showYAxis || startEndOnly && !showYAxis ? 0 : 20;
91
94
  var _useState = useState(60),
92
95
  _useState2 = _slicedToArray(_useState, 2),
93
96
  legendHeight = _useState2[0],
@@ -100,6 +103,25 @@ var AreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
100
103
  _useState6 = _slicedToArray(_useState5, 2),
101
104
  activeLegend = _useState6[0],
102
105
  setActiveLegend = _useState6[1];
106
+ var calculatedYAxisWidth = useMemo(function () {
107
+ if (yAxisWidth) return yAxisWidth;
108
+ return getMaxLabelLength({
109
+ data: data,
110
+ index: index,
111
+ margin: 16,
112
+ valueFormatter: valueFormatter
113
+ });
114
+ }, [yAxisWidth, data, valueFormatter, index]);
115
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
116
+ active: true,
117
+ block: true,
118
+ style: {
119
+ height: height,
120
+ width: width
121
+ }
122
+ });
123
+ var CustomTooltip = customTooltip;
124
+ var paddingValue = !showXAxis && !showYAxis || startEndOnly && !showYAxis ? 0 : 20;
103
125
  var categoryColors = constructCategoryColors(categories, colors);
104
126
  var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
105
127
  var hasOnValueChange = !!onValueChange;
@@ -198,13 +220,18 @@ var AreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
198
220
  fill: "",
199
221
  hide: !showYAxis,
200
222
  stroke: "",
201
- tick: {
202
- transform: 'translate(-3, 0)'
223
+ tick: function tick(props) {
224
+ return /*#__PURE__*/_jsx(CustomYAxisTick, _objectSpread(_objectSpread({}, props), {}, {
225
+ align: yAxisAlign,
226
+ formatter: valueFormatter,
227
+ textAnchor: yAxisAlign === 'left' ? 'start' : 'end',
228
+ yAxisLabel: Boolean(yAxisLabel)
229
+ }));
203
230
  },
204
231
  tickFormatter: valueFormatter,
205
232
  tickLine: false,
206
233
  type: "number",
207
- width: yAxisWidth,
234
+ width: calculatedYAxisWidth,
208
235
  children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
209
236
  angle: -90,
210
237
  className: cx(styles.strongLabel, styles.emphasis),
@@ -3,8 +3,10 @@ import BaseChartProps from "../common/BaseChartProps";
3
3
  export interface BarChartProps extends BaseChartProps {
4
4
  barCategoryGap?: string | number;
5
5
  layout?: 'vertical' | 'horizontal';
6
+ loading?: boolean;
6
7
  relative?: boolean;
7
8
  stack?: boolean;
9
+ yAxisAlign?: 'left' | 'right';
8
10
  }
9
11
  declare const BarChart: import("react").ForwardRefExoticComponent<BarChartProps & import("react").RefAttributes<HTMLDivElement>>;
10
12
  export default BarChart;
@@ -5,17 +5,20 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
6
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
7
7
  var _templateObject;
8
- var _excluded = ["data", "categories", "customCategories", "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", "style"];
8
+ var _excluded = ["data", "categories", "customCategories", "index", "yAxisAlign", "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", "loading", "xAxisLabel", "yAxisLabel", "className", "width", "height", "style"];
9
+ import { Skeleton } from 'antd';
9
10
  import { css } from 'antd-style';
10
- import { forwardRef, useState } from 'react';
11
+ import { forwardRef, useMemo, useState } from 'react';
11
12
  import { Flexbox } from 'react-layout-kit';
12
13
  import { Bar, CartesianGrid, Label, Legend, BarChart as ReChartsBarChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
13
14
  import ChartLegend from "../common/ChartLegend";
14
15
  import ChartTooltip from "../common/ChartTooltip";
16
+ import CustomYAxisTick from "../common/CustomYAxisTick";
15
17
  import NoData from "../common/NoData";
16
18
  import { constructCategoryColors, deepEqual, getYAxisDomain } from "../common/utils";
17
19
  import { useThemeColorRange } from "../hooks/useThemeColorRange";
18
20
  import { defaultValueFormatter } from "../utils";
21
+ import { getMaxLabelLength } from "../utils/getMaxLabelLength";
19
22
  import { renderShape } from "./renderShape";
20
23
  import { useStyles } from "./styles";
21
24
  import { jsx as _jsx } from "react/jsx-runtime";
@@ -32,6 +35,8 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
32
35
  categories = _props$categories === void 0 ? [] : _props$categories,
33
36
  customCategories = props.customCategories,
34
37
  index = props.index,
38
+ _props$yAxisAlign = props.yAxisAlign,
39
+ yAxisAlign = _props$yAxisAlign === void 0 ? 'left' : _props$yAxisAlign,
35
40
  _props$colors = props.colors,
36
41
  colors = _props$colors === void 0 ? themeColorRange : _props$colors,
37
42
  _props$valueFormatter = props.valueFormatter,
@@ -52,8 +57,7 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
52
57
  showXAxis = _props$showXAxis === void 0 ? true : _props$showXAxis,
53
58
  _props$showYAxis = props.showYAxis,
54
59
  showYAxis = _props$showYAxis === void 0 ? true : _props$showYAxis,
55
- _props$yAxisWidth = props.yAxisWidth,
56
- yAxisWidth = _props$yAxisWidth === void 0 ? 56 : _props$yAxisWidth,
60
+ yAxisWidth = props.yAxisWidth,
57
61
  _props$intervalType = props.intervalType,
58
62
  intervalType = _props$intervalType === void 0 ? 'equidistantPreserveStart' : _props$intervalType,
59
63
  _props$showTooltip = props.showTooltip,
@@ -77,6 +81,7 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
77
81
  barCategoryGap = props.barCategoryGap,
78
82
  _props$tickGap = props.tickGap,
79
83
  tickGap = _props$tickGap === void 0 ? 5 : _props$tickGap,
84
+ loading = props.loading,
80
85
  xAxisLabel = props.xAxisLabel,
81
86
  yAxisLabel = props.yAxisLabel,
82
87
  className = props.className,
@@ -86,21 +91,39 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
86
91
  height = _props$height === void 0 ? '20rem' : _props$height,
87
92
  style = props.style,
88
93
  rest = _objectWithoutProperties(props, _excluded);
89
- var CustomTooltip = customTooltip;
90
- var paddingValue = !showXAxis && !showYAxis ? 0 : 20;
91
- var _useState = useState(60),
94
+ var _useState = useState(),
92
95
  _useState2 = _slicedToArray(_useState, 2),
93
- legendHeight = _useState2[0],
94
- setLegendHeight = _useState2[1];
95
- var categoryColors = constructCategoryColors(categories, colors);
96
+ activeBar = _useState2[0],
97
+ setActiveBar = _useState2[1];
96
98
  var _useState3 = useState(),
97
99
  _useState4 = _slicedToArray(_useState3, 2),
98
- activeBar = _useState4[0],
99
- setActiveBar = _useState4[1];
100
- var _useState5 = useState(),
100
+ activeLegend = _useState4[0],
101
+ setActiveLegend = _useState4[1];
102
+ var _useState5 = useState(60),
101
103
  _useState6 = _slicedToArray(_useState5, 2),
102
- activeLegend = _useState6[0],
103
- setActiveLegend = _useState6[1];
104
+ legendHeight = _useState6[0],
105
+ setLegendHeight = _useState6[1];
106
+ var calculatedYAxisWidth = useMemo(function () {
107
+ if (yAxisWidth) return yAxisWidth;
108
+ return getMaxLabelLength({
109
+ data: data,
110
+ index: index,
111
+ layout: layout,
112
+ relative: relative,
113
+ valueFormatter: valueFormatter
114
+ });
115
+ }, [yAxisWidth, layout, data, valueFormatter, relative, index]);
116
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
117
+ active: true,
118
+ block: true,
119
+ style: {
120
+ height: height,
121
+ width: width
122
+ }
123
+ });
124
+ var CustomTooltip = customTooltip;
125
+ var paddingValue = !showXAxis && !showYAxis ? 0 : 20;
126
+ var categoryColors = constructCategoryColors(categories, colors);
104
127
  var hasOnValueChange = !!onValueChange;
105
128
  var onBarClick = function onBarClick(data, idx, event) {
106
129
  event.stopPropagation();
@@ -237,13 +260,20 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
237
260
  hide: !showYAxis,
238
261
  interval: "preserveStartEnd",
239
262
  stroke: "",
240
- tick: {
241
- transform: 'translate(0, 6)'
263
+ tick: function tick(props) {
264
+ return /*#__PURE__*/_jsx(CustomYAxisTick, _objectSpread(_objectSpread({}, props), {}, {
265
+ align: yAxisAlign,
266
+ formatter: function formatter(v) {
267
+ return v;
268
+ },
269
+ textAnchor: yAxisAlign === 'left' ? 'start' : 'end',
270
+ yAxisLabel: Boolean(yAxisLabel)
271
+ }));
242
272
  },
243
273
  tickLine: false,
244
274
  ticks: startEndOnly ? [data[0][index], data.at(-1)[index]] : undefined,
245
275
  type: "category",
246
- width: yAxisWidth,
276
+ width: calculatedYAxisWidth,
247
277
  children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
248
278
  angle: -90,
249
279
  className: styles.emphasis,
@@ -264,15 +294,22 @@ var BarChart = /*#__PURE__*/forwardRef(function (props, ref) {
264
294
  fill: "",
265
295
  hide: !showYAxis,
266
296
  stroke: "",
267
- tick: {
268
- transform: 'translate(-3, 0)'
297
+ tick: function tick(props) {
298
+ return /*#__PURE__*/_jsx(CustomYAxisTick, _objectSpread(_objectSpread({}, props), {}, {
299
+ align: yAxisAlign,
300
+ formatter: relative ? function (value) {
301
+ return "".concat(Number(value) * 100, "%");
302
+ } : valueFormatter,
303
+ textAnchor: yAxisAlign === 'left' ? 'start' : 'end',
304
+ yAxisLabel: Boolean(yAxisLabel)
305
+ }));
269
306
  },
270
307
  tickFormatter: relative ? function (value) {
271
308
  return "".concat((value * 100).toString(), " %");
272
309
  } : valueFormatter,
273
310
  tickLine: false,
274
311
  type: "number",
275
- width: yAxisWidth,
312
+ width: calculatedYAxisWidth,
276
313
  children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
277
314
  angle: -90,
278
315
  className: styles.emphasis,
@@ -4,6 +4,8 @@ import { CurveType } from "../types";
4
4
  export interface LineChartProps extends BaseChartProps {
5
5
  connectNulls?: boolean;
6
6
  curveType?: CurveType;
7
+ loading?: boolean;
8
+ yAxisAlign?: 'left' | 'right';
7
9
  }
8
10
  declare const LineChart: import("react").ForwardRefExoticComponent<LineChartProps & import("react").RefAttributes<HTMLDivElement>>;
9
11
  export default LineChart;
@@ -5,17 +5,20 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
6
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
7
7
  var _templateObject, _templateObject2, _templateObject3;
8
- var _excluded = ["data", "categories", "index", "colors", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "animationDuration", "showAnimation", "showTooltip", "showLegend", "showGridLines", "autoMinValue", "curveType", "minValue", "maxValue", "connectNulls", "allowDecimals", "noDataText", "className", "onValueChange", "enableLegendSlider", "customTooltip", "rotateLabelX", "tickGap", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories"];
8
+ var _excluded = ["data", "categories", "index", "colors", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "animationDuration", "showAnimation", "yAxisAlign", "showTooltip", "showLegend", "showGridLines", "autoMinValue", "curveType", "minValue", "maxValue", "connectNulls", "allowDecimals", "noDataText", "className", "onValueChange", "enableLegendSlider", "customTooltip", "rotateLabelX", "tickGap", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories", "loading"];
9
+ import { Skeleton } from 'antd';
9
10
  import { css } from 'antd-style';
10
- import { Fragment, forwardRef, useState } from 'react';
11
+ import { Fragment, forwardRef, useMemo, useState } from 'react';
11
12
  import { Flexbox } from 'react-layout-kit';
12
13
  import { CartesianGrid, Dot, Label, Legend, Line, LineChart as ReChartsLineChart, ResponsiveContainer, Tooltip, XAxis, YAxis } from 'recharts';
13
14
  import ChartLegend from "../common/ChartLegend";
14
15
  import ChartTooltip from "../common/ChartTooltip";
16
+ import CustomYAxisTick from "../common/CustomYAxisTick";
15
17
  import NoData from "../common/NoData";
16
18
  import { constructCategoryColors, getYAxisDomain, hasOnlyOneValueForThisKey } from "../common/utils";
17
19
  import { useThemeColorRange } from "../hooks/useThemeColorRange";
18
20
  import { defaultValueFormatter } from "../utils";
21
+ import { getMaxLabelLength } from "../utils/getMaxLabelLength";
19
22
  import { useStyles } from "./styles";
20
23
  import { jsx as _jsx } from "react/jsx-runtime";
21
24
  import { jsxs as _jsxs } from "react/jsx-runtime";
@@ -40,14 +43,15 @@ var LineChart = /*#__PURE__*/forwardRef(function (props, ref) {
40
43
  showXAxis = _props$showXAxis === void 0 ? true : _props$showXAxis,
41
44
  _props$showYAxis = props.showYAxis,
42
45
  showYAxis = _props$showYAxis === void 0 ? true : _props$showYAxis,
43
- _props$yAxisWidth = props.yAxisWidth,
44
- yAxisWidth = _props$yAxisWidth === void 0 ? 56 : _props$yAxisWidth,
46
+ yAxisWidth = props.yAxisWidth,
45
47
  _props$intervalType = props.intervalType,
46
48
  intervalType = _props$intervalType === void 0 ? 'equidistantPreserveStart' : _props$intervalType,
47
49
  _props$animationDurat = props.animationDuration,
48
50
  animationDuration = _props$animationDurat === void 0 ? 900 : _props$animationDurat,
49
51
  _props$showAnimation = props.showAnimation,
50
52
  showAnimation = _props$showAnimation === void 0 ? false : _props$showAnimation,
53
+ _props$yAxisAlign = props.yAxisAlign,
54
+ yAxisAlign = _props$yAxisAlign === void 0 ? 'left' : _props$yAxisAlign,
51
55
  _props$showTooltip = props.showTooltip,
52
56
  showTooltip = _props$showTooltip === void 0 ? true : _props$showTooltip,
53
57
  _props$showLegend = props.showLegend,
@@ -81,9 +85,8 @@ var LineChart = /*#__PURE__*/forwardRef(function (props, ref) {
81
85
  height = _props$height === void 0 ? '20rem' : _props$height,
82
86
  style = props.style,
83
87
  customCategories = props.customCategories,
88
+ loading = props.loading,
84
89
  rest = _objectWithoutProperties(props, _excluded);
85
- var CustomTooltip = customTooltip;
86
- var paddingValue = !showXAxis && !showYAxis ? 0 : 20;
87
90
  var _useState = useState(60),
88
91
  _useState2 = _slicedToArray(_useState, 2),
89
92
  legendHeight = _useState2[0],
@@ -96,6 +99,25 @@ var LineChart = /*#__PURE__*/forwardRef(function (props, ref) {
96
99
  _useState6 = _slicedToArray(_useState5, 2),
97
100
  activeLegend = _useState6[0],
98
101
  setActiveLegend = _useState6[1];
102
+ var calculatedYAxisWidth = useMemo(function () {
103
+ if (yAxisWidth) return yAxisWidth;
104
+ return getMaxLabelLength({
105
+ data: data,
106
+ index: index,
107
+ margin: 16,
108
+ valueFormatter: valueFormatter
109
+ });
110
+ }, [yAxisWidth, data, valueFormatter, index]);
111
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
112
+ active: true,
113
+ block: true,
114
+ style: {
115
+ height: height,
116
+ width: width
117
+ }
118
+ });
119
+ var CustomTooltip = customTooltip;
120
+ var paddingValue = !showXAxis && !showYAxis ? 0 : 20;
99
121
  var categoryColors = constructCategoryColors(categories, colors);
100
122
  var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
101
123
  var hasOnValueChange = !!onValueChange;
@@ -194,13 +216,18 @@ var LineChart = /*#__PURE__*/forwardRef(function (props, ref) {
194
216
  fill: "",
195
217
  hide: !showYAxis,
196
218
  stroke: "",
197
- tick: {
198
- transform: 'translate(-3, 0)'
219
+ tick: function tick(props) {
220
+ return /*#__PURE__*/_jsx(CustomYAxisTick, _objectSpread(_objectSpread({}, props), {}, {
221
+ align: yAxisAlign,
222
+ formatter: valueFormatter,
223
+ textAnchor: yAxisAlign === 'left' ? 'start' : 'end',
224
+ yAxisLabel: Boolean(yAxisLabel)
225
+ }));
199
226
  },
200
227
  tickFormatter: valueFormatter,
201
228
  tickLine: false,
202
229
  type: "number",
203
- width: yAxisWidth,
230
+ width: calculatedYAxisWidth,
204
231
  children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
205
232
  angle: -90,
206
233
  className: cx(styles.strongLabel, styles.emphasis),
@@ -22,6 +22,7 @@ export interface ScatterChartProps extends BaseAnimationTimingProps, HTMLAttribu
22
22
  data: any[];
23
23
  enableLegendSlider?: boolean;
24
24
  intervalType?: IntervalType;
25
+ loading?: boolean;
25
26
  maxXValue?: number;
26
27
  maxYValue?: number;
27
28
  minXValue?: number;
@@ -47,6 +48,7 @@ export interface ScatterChartProps extends BaseAnimationTimingProps, HTMLAttribu
47
48
  x: string;
48
49
  xAxisLabel?: string;
49
50
  y: string;
51
+ yAxisAlign?: 'left' | 'right';
50
52
  yAxisLabel?: string;
51
53
  yAxisWidth?: number;
52
54
  }
@@ -5,16 +5,19 @@ import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
6
6
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
7
7
  var _templateObject, _templateObject2;
8
- var _excluded = ["data", "x", "y", "size", "category", "colors", "showOpacity", "sizeRange", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "animationDuration", "showAnimation", "showTooltip", "showLegend", "showGridLines", "autoMinXValue", "minXValue", "maxXValue", "autoMinYValue", "minYValue", "maxYValue", "allowDecimals", "noDataText", "onValueChange", "customTooltip", "rotateLabelX", "className", "enableLegendSlider", "tickGap", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories"];
8
+ var _excluded = ["data", "x", "y", "size", "category", "colors", "showOpacity", "yAxisAlign", "sizeRange", "valueFormatter", "startEndOnly", "showXAxis", "showYAxis", "yAxisWidth", "intervalType", "animationDuration", "showAnimation", "showTooltip", "showLegend", "loading", "showGridLines", "autoMinXValue", "minXValue", "maxXValue", "autoMinYValue", "minYValue", "maxYValue", "allowDecimals", "noDataText", "onValueChange", "customTooltip", "rotateLabelX", "className", "enableLegendSlider", "tickGap", "xAxisLabel", "yAxisLabel", "width", "height", "style", "customCategories"];
9
+ import { Skeleton } from 'antd';
9
10
  import { css } from 'antd-style';
10
- import { forwardRef, useState } from 'react';
11
+ import { forwardRef, useMemo, useState } from 'react';
11
12
  import { Flexbox } from 'react-layout-kit';
12
13
  import { CartesianGrid, Label, Legend, ScatterChart as ReChartsScatterChart, ResponsiveContainer, Scatter, Tooltip, XAxis, YAxis, ZAxis } from 'recharts';
13
14
  import { renderShape } from "./renderShape";
14
15
  import ChartLegend from "../common/ChartLegend";
16
+ import CustomYAxisTick from "../common/CustomYAxisTick";
15
17
  import { constructCategories, constructCategoryColors, deepEqual, getYAxisDomain } from "../common/utils";
16
18
  import { useThemeColorRange } from "../hooks/useThemeColorRange";
17
19
  import { defaultValueFormatter } from "../utils";
20
+ import { getMaxLabelLength } from "../utils/getMaxLabelLength";
18
21
  import NoData from "../common/NoData";
19
22
  import ScatterChartTooltip from "./ScatterChartTooltip";
20
23
  import { useStyles } from "./styles";
@@ -36,6 +39,8 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
36
39
  colors = _props$colors === void 0 ? themeColorRange : _props$colors,
37
40
  _props$showOpacity = props.showOpacity,
38
41
  showOpacity = _props$showOpacity === void 0 ? false : _props$showOpacity,
42
+ _props$yAxisAlign = props.yAxisAlign,
43
+ yAxisAlign = _props$yAxisAlign === void 0 ? 'left' : _props$yAxisAlign,
39
44
  _props$sizeRange = props.sizeRange,
40
45
  sizeRange = _props$sizeRange === void 0 ? [1, 1000] : _props$sizeRange,
41
46
  _props$valueFormatter = props.valueFormatter,
@@ -50,8 +55,7 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
50
55
  showXAxis = _props$showXAxis === void 0 ? true : _props$showXAxis,
51
56
  _props$showYAxis = props.showYAxis,
52
57
  showYAxis = _props$showYAxis === void 0 ? true : _props$showYAxis,
53
- _props$yAxisWidth = props.yAxisWidth,
54
- yAxisWidth = _props$yAxisWidth === void 0 ? 56 : _props$yAxisWidth,
58
+ yAxisWidth = props.yAxisWidth,
55
59
  _props$intervalType = props.intervalType,
56
60
  intervalType = _props$intervalType === void 0 ? 'equidistantPreserveStart' : _props$intervalType,
57
61
  _props$animationDurat = props.animationDuration,
@@ -62,6 +66,7 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
62
66
  showTooltip = _props$showTooltip === void 0 ? true : _props$showTooltip,
63
67
  _props$showLegend = props.showLegend,
64
68
  showLegend = _props$showLegend === void 0 ? true : _props$showLegend,
69
+ loading = props.loading,
65
70
  _props$showGridLines = props.showGridLines,
66
71
  showGridLines = _props$showGridLines === void 0 ? true : _props$showGridLines,
67
72
  _props$autoMinXValue = props.autoMinXValue,
@@ -92,7 +97,6 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
92
97
  style = props.style,
93
98
  customCategories = props.customCategories,
94
99
  rest = _objectWithoutProperties(props, _excluded);
95
- var CustomTooltip = customTooltip;
96
100
  var _useState = useState(60),
97
101
  _useState2 = _slicedToArray(_useState, 2),
98
102
  legendHeight = _useState2[0],
@@ -105,6 +109,25 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
105
109
  _useState6 = _slicedToArray(_useState5, 2),
106
110
  activeLegend = _useState6[0],
107
111
  setActiveLegend = _useState6[1];
112
+ var calculatedYAxisWidth = useMemo(function () {
113
+ if (yAxisWidth) return yAxisWidth;
114
+ return getMaxLabelLength({
115
+ data: data,
116
+ index: y,
117
+ isScatterChart: true,
118
+ margin: 0,
119
+ valueFormatter: valueFormatter === null || valueFormatter === void 0 ? void 0 : valueFormatter.y
120
+ });
121
+ }, [yAxisWidth, data, valueFormatter, y]);
122
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
123
+ active: true,
124
+ block: true,
125
+ style: {
126
+ height: height,
127
+ width: width
128
+ }
129
+ });
130
+ var CustomTooltip = customTooltip;
108
131
  var hasOnValueChange = !!onValueChange;
109
132
  var onNodeClick = function onNodeClick(data, index, event) {
110
133
  event.stopPropagation();
@@ -207,13 +230,18 @@ var ScatterChart = /*#__PURE__*/forwardRef(function (props, ref) {
207
230
  hide: !showYAxis,
208
231
  name: y,
209
232
  stroke: "",
210
- tick: {
211
- transform: 'translate(-3, 0)'
233
+ tick: function tick(props) {
234
+ return /*#__PURE__*/_jsx(CustomYAxisTick, _objectSpread(_objectSpread({}, props), {}, {
235
+ align: yAxisAlign,
236
+ formatter: valueFormatter.y,
237
+ textAnchor: yAxisAlign === 'left' ? 'start' : 'end',
238
+ yAxisLabel: Boolean(yAxisLabel)
239
+ }));
212
240
  },
213
241
  tickFormatter: valueFormatter.y,
214
242
  tickLine: false,
215
243
  type: "number",
216
- width: yAxisWidth,
244
+ width: calculatedYAxisWidth,
217
245
  children: yAxisLabel && /*#__PURE__*/_jsx(Label, {
218
246
  angle: -90,
219
247
  className: cx(styles.strongLabel, styles.emphasis),
@@ -4,6 +4,7 @@ import { CurveType } from "../types";
4
4
  export interface SparkAreaChartProps extends BaseSparkChartProps {
5
5
  connectNulls?: boolean;
6
6
  curveType?: CurveType;
7
+ loading?: boolean;
7
8
  showGradient?: boolean;
8
9
  stack?: boolean;
9
10
  }
@@ -4,7 +4,8 @@ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLit
4
4
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
6
6
  var _templateObject, _templateObject2, _templateObject3;
7
- var _excluded = ["data", "categories", "index", "stack", "colors", "showAnimation", "animationDuration", "showGradient", "curveType", "connectNulls", "noDataText", "autoMinValue", "minValue", "maxValue", "className", "width", "height", "style"];
7
+ var _excluded = ["data", "categories", "index", "stack", "colors", "showAnimation", "animationDuration", "showGradient", "curveType", "connectNulls", "noDataText", "autoMinValue", "loading", "minValue", "maxValue", "className", "width", "height", "style"];
8
+ import { Skeleton } from 'antd';
8
9
  import { css } from 'antd-style';
9
10
  import { forwardRef } from 'react';
10
11
  import { Flexbox } from 'react-layout-kit';
@@ -43,6 +44,7 @@ var SparkAreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
43
44
  noDataText = _props$noDataText === void 0 ? /*#__PURE__*/_jsx("div", {}) : _props$noDataText,
44
45
  _props$autoMinValue = props.autoMinValue,
45
46
  autoMinValue = _props$autoMinValue === void 0 ? false : _props$autoMinValue,
47
+ loading = props.loading,
46
48
  minValue = props.minValue,
47
49
  maxValue = props.maxValue,
48
50
  className = props.className,
@@ -52,6 +54,14 @@ var SparkAreaChart = /*#__PURE__*/forwardRef(function (props, ref) {
52
54
  height = _props$height === void 0 ? 48 : _props$height,
53
55
  style = props.style,
54
56
  rest = _objectWithoutProperties(props, _excluded);
57
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
58
+ active: true,
59
+ block: true,
60
+ style: {
61
+ height: height,
62
+ width: width
63
+ }
64
+ });
55
65
  var categoryColors = constructCategoryColors(categories, colors);
56
66
  var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
57
67
  return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
@@ -1,6 +1,7 @@
1
1
  /// <reference types="react" />
2
2
  import BaseSparkChartProps from "../common/BaseSparkChartProps";
3
3
  export interface SparkBarChartProps extends BaseSparkChartProps {
4
+ loading?: boolean;
4
5
  relative?: boolean;
5
6
  stack?: boolean;
6
7
  }
@@ -4,7 +4,8 @@ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLit
4
4
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
6
6
  var _templateObject;
7
- var _excluded = ["data", "categories", "index", "colors", "stack", "relative", "animationDuration", "showAnimation", "noDataText", "autoMinValue", "minValue", "maxValue", "className", "width", "height", "style"];
7
+ var _excluded = ["data", "categories", "index", "colors", "stack", "relative", "animationDuration", "showAnimation", "noDataText", "loading", "autoMinValue", "minValue", "maxValue", "className", "width", "height", "style"];
8
+ import { Skeleton } from 'antd';
8
9
  import { css } from 'antd-style';
9
10
  import { forwardRef } from 'react';
10
11
  import { Flexbox } from 'react-layout-kit';
@@ -37,6 +38,7 @@ var SparkBarChart = /*#__PURE__*/forwardRef(function (props, ref) {
37
38
  showAnimation = _props$showAnimation === void 0 ? false : _props$showAnimation,
38
39
  _props$noDataText = props.noDataText,
39
40
  noDataText = _props$noDataText === void 0 ? /*#__PURE__*/_jsx("div", {}) : _props$noDataText,
41
+ loading = props.loading,
40
42
  _props$autoMinValue = props.autoMinValue,
41
43
  autoMinValue = _props$autoMinValue === void 0 ? false : _props$autoMinValue,
42
44
  minValue = props.minValue,
@@ -48,6 +50,14 @@ var SparkBarChart = /*#__PURE__*/forwardRef(function (props, ref) {
48
50
  height = _props$height === void 0 ? 48 : _props$height,
49
51
  style = props.style,
50
52
  rest = _objectWithoutProperties(props, _excluded);
53
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
54
+ active: true,
55
+ block: true,
56
+ style: {
57
+ height: height,
58
+ width: width
59
+ }
60
+ });
51
61
  var categoryColors = constructCategoryColors(categories, colors);
52
62
  var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
53
63
  return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
@@ -4,6 +4,7 @@ import { CurveType } from "../types";
4
4
  export interface SparkLineChartProps extends BaseSparkChartProps {
5
5
  connectNulls?: boolean;
6
6
  curveType?: CurveType;
7
+ loading?: boolean;
7
8
  }
8
9
  declare const SparkLineChart: import("react").ForwardRefExoticComponent<SparkLineChartProps & import("react").RefAttributes<HTMLDivElement>>;
9
10
  export default SparkLineChart;
@@ -4,7 +4,8 @@ import _taggedTemplateLiteral from "@babel/runtime/helpers/esm/taggedTemplateLit
4
4
  import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
5
5
  import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
6
6
  var _templateObject;
7
- var _excluded = ["data", "categories", "index", "colors", "animationDuration", "showAnimation", "curveType", "connectNulls", "noDataText", "autoMinValue", "minValue", "maxValue", "className", "width", "height", "style"];
7
+ var _excluded = ["data", "categories", "index", "colors", "animationDuration", "showAnimation", "curveType", "connectNulls", "noDataText", "autoMinValue", "minValue", "maxValue", "className", "width", "height", "style", "loading"];
8
+ import { Skeleton } from 'antd';
8
9
  import { css } from 'antd-style';
9
10
  import { forwardRef } from 'react';
10
11
  import { Flexbox } from 'react-layout-kit';
@@ -47,7 +48,16 @@ var SparkLineChart = /*#__PURE__*/forwardRef(function (props, ref) {
47
48
  _props$height = props.height,
48
49
  height = _props$height === void 0 ? 48 : _props$height,
49
50
  style = props.style,
51
+ loading = props.loading,
50
52
  rest = _objectWithoutProperties(props, _excluded);
53
+ if (loading || !data) return /*#__PURE__*/_jsx(Skeleton.Button, {
54
+ active: true,
55
+ block: true,
56
+ style: {
57
+ height: height,
58
+ width: width
59
+ }
60
+ });
51
61
  var categoryColors = constructCategoryColors(categories, colors);
52
62
  var yAxisDomain = getYAxisDomain(autoMinValue, minValue, maxValue);
53
63
  return /*#__PURE__*/_jsx(Flexbox, _objectSpread(_objectSpread({
@@ -0,0 +1,14 @@
1
+ import React from 'react';
2
+ import { ValueFormatter } from "../types";
3
+ interface CustomYAxisTickProps {
4
+ align: 'left' | 'right';
5
+ formatter: ValueFormatter;
6
+ payload: {
7
+ value: number;
8
+ };
9
+ x: number;
10
+ y: number;
11
+ yAxisLabel?: boolean;
12
+ }
13
+ declare const CustomYAxisTick: React.NamedExoticComponent<CustomYAxisTickProps>;
14
+ export default CustomYAxisTick;
@@ -0,0 +1,26 @@
1
+ import { useTheme } from 'antd-style';
2
+ import React, { memo } from 'react';
3
+ import { jsx as _jsx } from "react/jsx-runtime";
4
+ var CustomYAxisTick = /*#__PURE__*/memo(function (_ref) {
5
+ var yAxisLabel = _ref.yAxisLabel,
6
+ x = _ref.x,
7
+ y = _ref.y,
8
+ payload = _ref.payload,
9
+ align = _ref.align,
10
+ formatter = _ref.formatter;
11
+ var theme = useTheme();
12
+ var yAxisLabelWidth = yAxisLabel ? 24 : 0;
13
+ return /*#__PURE__*/_jsx("g", {
14
+ transform: "translate(".concat(align === 'left' ? yAxisLabelWidth : x + yAxisLabelWidth, ",").concat(y, ")"),
15
+ children: /*#__PURE__*/_jsx("text", {
16
+ dy: 4,
17
+ fill: theme.colorTextDescription,
18
+ fontSize: 14,
19
+ textAnchor: align === 'left' ? 'start' : 'end',
20
+ x: 0,
21
+ y: 0,
22
+ children: formatter(payload.value)
23
+ })
24
+ });
25
+ });
26
+ export default CustomYAxisTick;
@@ -0,0 +1,11 @@
1
+ import { ValueFormatter } from "../types";
2
+ export declare const getTextWidth: (text: string) => number;
3
+ export declare const getMaxLabelLength: ({ data, valueFormatter, relative, index, layout, margin, isScatterChart, }: {
4
+ data: Array<Record<string, any>>;
5
+ index: string;
6
+ isScatterChart?: boolean | undefined;
7
+ layout?: "horizontal" | "vertical" | undefined;
8
+ margin?: number | undefined;
9
+ relative?: boolean | undefined;
10
+ valueFormatter?: ValueFormatter | undefined;
11
+ }) => number;
@@ -0,0 +1,108 @@
1
+ import _createForOfIteratorHelper from "@babel/runtime/helpers/esm/createForOfIteratorHelper";
2
+ import { defaultValueFormatter } from "./index";
3
+ export var getTextWidth = function getTextWidth(text) {
4
+ // 创建临时 span 元素
5
+ var span = document.createElement('span');
6
+
7
+ // 设置样式使其不可见
8
+ span.style.fontSize = '14px';
9
+ span.style.position = 'absolute';
10
+ span.style.visibility = 'hidden';
11
+ span.style.whiteSpace = 'nowrap';
12
+
13
+ // 设置文本内容
14
+ span.textContent = text;
15
+
16
+ // 添加到 body
17
+ document.body.append(span);
18
+
19
+ // 获取宽度
20
+ var width = span.getBoundingClientRect().width;
21
+
22
+ // 移除临时元素
23
+ span.remove();
24
+ return width;
25
+ };
26
+ export var getMaxLabelLength = function getMaxLabelLength(_ref) {
27
+ var data = _ref.data,
28
+ _ref$valueFormatter = _ref.valueFormatter,
29
+ valueFormatter = _ref$valueFormatter === void 0 ? defaultValueFormatter : _ref$valueFormatter,
30
+ relative = _ref.relative,
31
+ index = _ref.index,
32
+ _ref$layout = _ref.layout,
33
+ layout = _ref$layout === void 0 ? 'horizontal' : _ref$layout,
34
+ _ref$margin = _ref.margin,
35
+ margin = _ref$margin === void 0 ? 24 : _ref$margin,
36
+ isScatterChart = _ref.isScatterChart;
37
+ var maxLength = 0;
38
+ var maxLabel = '';
39
+ if (isScatterChart) {
40
+ var _iterator = _createForOfIteratorHelper(data),
41
+ _step;
42
+ try {
43
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
44
+ var item = _step.value;
45
+ var formattedValue = valueFormatter(item[index]);
46
+ if (formattedValue.length > maxLength) {
47
+ maxLength = formattedValue.length;
48
+ maxLabel = formattedValue;
49
+ }
50
+ }
51
+ } catch (err) {
52
+ _iterator.e(err);
53
+ } finally {
54
+ _iterator.f();
55
+ }
56
+ } else if (layout === 'vertical') {
57
+ var _iterator2 = _createForOfIteratorHelper(data),
58
+ _step2;
59
+ try {
60
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
61
+ var _item = _step2.value;
62
+ if (_item[index].length > maxLength) {
63
+ maxLength = _item[index].length;
64
+ maxLabel = _item[index];
65
+ }
66
+ }
67
+ } catch (err) {
68
+ _iterator2.e(err);
69
+ } finally {
70
+ _iterator2.f();
71
+ }
72
+ } else {
73
+ var _iterator3 = _createForOfIteratorHelper(data),
74
+ _step3;
75
+ try {
76
+ for (_iterator3.s(); !(_step3 = _iterator3.n()).done;) {
77
+ var _item2 = _step3.value;
78
+ var categories = Object.keys(_item2).filter(function (key) {
79
+ return key !== index;
80
+ });
81
+ var _iterator4 = _createForOfIteratorHelper(categories),
82
+ _step4;
83
+ try {
84
+ for (_iterator4.s(); !(_step4 = _iterator4.n()).done;) {
85
+ var category = _step4.value;
86
+ var value = _item2[category];
87
+ if (value === undefined) continue; // Skip null or undefined values
88
+ var _formattedValue = relative ? "".concat((value * 100).toFixed(2), "%") : valueFormatter(value);
89
+ if (_formattedValue.length > maxLength) {
90
+ maxLength = _formattedValue.length;
91
+ maxLabel = _formattedValue;
92
+ }
93
+ }
94
+ } catch (err) {
95
+ _iterator4.e(err);
96
+ } finally {
97
+ _iterator4.f();
98
+ }
99
+ }
100
+ } catch (err) {
101
+ _iterator3.e(err);
102
+ } finally {
103
+ _iterator3.f();
104
+ }
105
+ }
106
+ console.log(maxLabel);
107
+ return getTextWidth(maxLabel) + margin;
108
+ };
package/es/utils/index.js CHANGED
@@ -1,7 +1,5 @@
1
1
  import { DeltaTypes } from "../types";
2
- export var defaultValueFormatter = function defaultValueFormatter(value) {
3
- return value.toString();
4
- };
2
+ export var defaultValueFormatter = String;
5
3
  export var sumNumericArray = function sumNumericArray(arr) {
6
4
  return arr.reduce(function (prefixSum, num) {
7
5
  return prefixSum + num;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lobehub/charts",
3
- "version": "1.4.1",
3
+ "version": "1.5.0",
4
4
  "description": "React modern charts components built on recharts",
5
5
  "keywords": [
6
6
  "lobehub",