@mui/x-charts 8.17.0 → 8.18.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.
@@ -2,12 +2,17 @@ import { AxisId, ChartsRadiusAxisProps, ChartsRotationAxisProps, PolarAxisDefaul
2
2
  /**
3
3
  * Get all the x-axes.
4
4
  *
5
- * - `xAxis` is an object with the shape `{ [axisId]: axis }`.
6
- * - `xAxisIds` is an array of axis IDs.
5
+ * Returns all X axes configured in the chart along with their IDs.
6
+ * This is useful when you need to iterate over multiple axes or access all axis configurations at once.
7
7
  *
8
- * If access to a specific X axis is needed, use the `useXAxis` hook instead.
8
+ * @returns An object containing:
9
+ * - `xAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
10
+ * - `xAxisIds`: An array of all X axis IDs in the chart
9
11
  *
10
- * @returns `{ xAxis, xAxisIds }` - The x-axes and their IDs.
12
+ * @example
13
+ * const { xAxis, xAxisIds } = useXAxes();
14
+ *
15
+ * @see `useXAxis` for accessing a single X axis
11
16
  */
12
17
  export declare function useXAxes(): {
13
18
  xAxis: import("../internals/index.js").ComputedAxisConfig<ChartsXAxisProps>;
@@ -16,36 +21,131 @@ export declare function useXAxes(): {
16
21
  /**
17
22
  * Get all the y-axes.
18
23
  *
19
- * - `yAxis` is an object with the shape `{ [axisId]: axis }`.
20
- * - `yAxisIds` is an array of axis IDs.
24
+ * Returns all Y axes configured in the chart along with their IDs.
25
+ * This is useful when you need to iterate over multiple axes or access all axis configurations at once.
26
+ *
27
+ * @returns An object containing:
28
+ * - `yAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
29
+ * - `yAxisIds`: An array of all Y axis IDs in the chart
21
30
  *
22
- * If access to a specific Y axis is needed, use the `useYAxis` hook instead.
31
+ * @example
32
+ * const { yAxis, yAxisIds } = useYAxes();
23
33
  *
24
- * @returns `{ yAxis, yAxisIds }` - The y-axes and their IDs.
34
+ * @see `useYAxis` for accessing a single Y axis
25
35
  */
26
36
  export declare function useYAxes(): {
27
37
  yAxis: import("../internals/index.js").ComputedAxisConfig<ChartsYAxisProps>;
28
38
  yAxisIds: AxisId[];
29
39
  };
30
40
  /**
31
- * Get the X axis.
32
- * @param {AxisId | undefined} axisId - If provided returns the x axis with axisId, else returns the values for the default x axis.
33
- * @returns The X axis.
41
+ * Get a specific X axis or the default X axis.
42
+ *
43
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
44
+ * - A string or number matching the axis ID defined in the chart's `xAxis` prop
45
+ * - Undefined to get the default (first) X axis
46
+ * @returns The configuration for a single X axis.
47
+ *
48
+ * @example
49
+ * // Get the default X axis
50
+ * const xAxis = useXAxis();
51
+ *
52
+ * @example
53
+ * // Get a specific X axis by string ID
54
+ * const xAxis = useXAxis('revenue');
34
55
  */
35
56
  export declare function useXAxis<T extends keyof AxisScaleConfig>(axisId?: AxisId): ComputedAxis<T, any, ChartsXAxisProps>;
36
57
  /**
37
- * Get the Y axis.
38
- * @param {AxisId | undefined} axisId - If provided returns the y axis with axisId, else returns the values for the default y axis.
39
- * @returns The Y axis.
58
+ * Get a specific Y axis or the default Y axis.
59
+ *
60
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
61
+ * - A string or number matching the axis ID defined in the chart's `yAxis` prop
62
+ * - Undefined to get the default (first) Y axis
63
+ * @returns The configuration for a single Y axis.
64
+ *
65
+ * @example
66
+ * // Get the default Y axis
67
+ * const yAxis = useYAxis();
68
+ *
69
+ * @example
70
+ * // Get a specific Y axis by string ID
71
+ * const yAxis = useYAxis('temperature');
40
72
  */
41
73
  export declare function useYAxis<T extends keyof AxisScaleConfig>(axisId?: AxisId): ComputedAxis<T, any, ChartsYAxisProps>;
74
+ /**
75
+ * Get all the rotation axes for polar charts.
76
+ *
77
+ * Returns all rotation axes configured in polar charts along with their IDs.
78
+ * Rotation axes are used in charts like `RadarChart` to define angular positioning.
79
+ *
80
+ * @returns An object containing:
81
+ * - `rotationAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
82
+ * - `rotationAxisIds`: An array of all rotation axis IDs in the chart
83
+ *
84
+ * @example
85
+ * const { rotationAxis, rotationAxisIds } = useRotationAxes();
86
+ *
87
+ * @see `useRotationAxis` for accessing a single rotation axis
88
+ */
42
89
  export declare function useRotationAxes(): {
43
90
  rotationAxis: import("../internals/plugins/featurePlugins/useChartPolarAxis/computeAxisValue.js").DefaultizedAxisConfig<ChartsRotationAxisProps>;
44
91
  rotationAxisIds: string[];
45
92
  };
93
+ /**
94
+ * Get all the radius axes for polar charts.
95
+ *
96
+ * Returns all radial axes configured in polar charts along with their IDs.
97
+ * Radius axes are used in charts like `RadarChart` to define radial positioning and scaling.
98
+ *
99
+ * @returns An object containing:
100
+ * - `radiusAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
101
+ * - `radiusAxisIds`: An array of all radius axis IDs in the chart
102
+ *
103
+ * @example
104
+ * const { radiusAxis, radiusAxisIds } = useRadiusAxes();
105
+ *
106
+ * @see `useRadiusAxis` for accessing a single radius axis
107
+ */
46
108
  export declare function useRadiusAxes(): {
47
109
  radiusAxis: import("../internals/plugins/featurePlugins/useChartPolarAxis/computeAxisValue.js").DefaultizedAxisConfig<ChartsRadiusAxisProps>;
48
110
  radiusAxisIds: string[];
49
111
  };
50
- export declare function useRotationAxis(identifier?: number | string): PolarAxisDefaultized<ScaleName, any, ChartsRotationAxisProps> | undefined;
51
- export declare function useRadiusAxis(identifier?: number | string): PolarAxisDefaultized<ScaleName, any, ChartsRadiusAxisProps> | undefined;
112
+ /**
113
+ * Get a specific rotation axis or the default rotation axis for polar charts.
114
+ *
115
+ * Returns the configuration and scale for a rotation axis in polar charts.
116
+ * The rotation axis controls the angular positioning of data points around the circle.
117
+ *
118
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
119
+ * - A string or number matching the axis ID defined in the chart's rotation axis configuration
120
+ * - Undefined to get the default (first) rotation axis
121
+ * @returns The rotation axis configuration, or undefined if not found
122
+ *
123
+ * @example
124
+ * // Get the default rotation axis
125
+ * const rotationAxis = useRotationAxis();
126
+ *
127
+ * @example
128
+ * // Get a specific rotation axis by string ID
129
+ * const rotationAxis = useRotationAxis('categories');
130
+ */
131
+ export declare function useRotationAxis(axisId?: AxisId): PolarAxisDefaultized<ScaleName, any, ChartsRotationAxisProps> | undefined;
132
+ /**
133
+ * Get a specific radius axis or the default radius axis for polar charts.
134
+ *
135
+ * Returns the configuration and scale for a radial axis in polar charts.
136
+ * The radius axis controls the radial distance of data points from the center of the circle.
137
+ *
138
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
139
+ * - A string or number matching the axis ID defined in the chart's radius axis configuration
140
+ * - Undefined to get the default (first) radius axis
141
+ * @returns The radius axis configuration, or undefined if not found
142
+ *
143
+ * @example
144
+ * // Get the default radius axis
145
+ * const radiusAxis = useRadiusAxis();
146
+ *
147
+ * @example
148
+ * // Get a specific radius axis by string ID
149
+ * const radiusAxis = useRadiusAxis('magnitude');
150
+ */
151
+ export declare function useRadiusAxis(axisId?: AxisId): PolarAxisDefaultized<ScaleName, any, ChartsRadiusAxisProps> | undefined;
@@ -7,12 +7,17 @@ import { useStore } from "../internals/store/useStore.js";
7
7
  /**
8
8
  * Get all the x-axes.
9
9
  *
10
- * - `xAxis` is an object with the shape `{ [axisId]: axis }`.
11
- * - `xAxisIds` is an array of axis IDs.
10
+ * Returns all X axes configured in the chart along with their IDs.
11
+ * This is useful when you need to iterate over multiple axes or access all axis configurations at once.
12
12
  *
13
- * If access to a specific X axis is needed, use the `useXAxis` hook instead.
13
+ * @returns An object containing:
14
+ * - `xAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
15
+ * - `xAxisIds`: An array of all X axis IDs in the chart
14
16
  *
15
- * @returns `{ xAxis, xAxisIds }` - The x-axes and their IDs.
17
+ * @example
18
+ * const { xAxis, xAxisIds } = useXAxes();
19
+ *
20
+ * @see `useXAxis` for accessing a single X axis
16
21
  */
17
22
  export function useXAxes() {
18
23
  const store = useStore();
@@ -29,12 +34,17 @@ export function useXAxes() {
29
34
  /**
30
35
  * Get all the y-axes.
31
36
  *
32
- * - `yAxis` is an object with the shape `{ [axisId]: axis }`.
33
- * - `yAxisIds` is an array of axis IDs.
37
+ * Returns all Y axes configured in the chart along with their IDs.
38
+ * This is useful when you need to iterate over multiple axes or access all axis configurations at once.
39
+ *
40
+ * @returns An object containing:
41
+ * - `yAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
42
+ * - `yAxisIds`: An array of all Y axis IDs in the chart
34
43
  *
35
- * If access to a specific Y axis is needed, use the `useYAxis` hook instead.
44
+ * @example
45
+ * const { yAxis, yAxisIds } = useYAxes();
36
46
  *
37
- * @returns `{ yAxis, yAxisIds }` - The y-axes and their IDs.
47
+ * @see `useYAxis` for accessing a single Y axis
38
48
  */
39
49
  export function useYAxes() {
40
50
  const store = useStore();
@@ -49,9 +59,20 @@ export function useYAxes() {
49
59
  }
50
60
 
51
61
  /**
52
- * Get the X axis.
53
- * @param {AxisId | undefined} axisId - If provided returns the x axis with axisId, else returns the values for the default x axis.
54
- * @returns The X axis.
62
+ * Get a specific X axis or the default X axis.
63
+ *
64
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
65
+ * - A string or number matching the axis ID defined in the chart's `xAxis` prop
66
+ * - Undefined to get the default (first) X axis
67
+ * @returns The configuration for a single X axis.
68
+ *
69
+ * @example
70
+ * // Get the default X axis
71
+ * const xAxis = useXAxis();
72
+ *
73
+ * @example
74
+ * // Get a specific X axis by string ID
75
+ * const xAxis = useXAxis('revenue');
55
76
  */
56
77
  export function useXAxis(axisId) {
57
78
  const store = useStore();
@@ -64,9 +85,20 @@ export function useXAxis(axisId) {
64
85
  }
65
86
 
66
87
  /**
67
- * Get the Y axis.
68
- * @param {AxisId | undefined} axisId - If provided returns the y axis with axisId, else returns the values for the default y axis.
69
- * @returns The Y axis.
88
+ * Get a specific Y axis or the default Y axis.
89
+ *
90
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
91
+ * - A string or number matching the axis ID defined in the chart's `yAxis` prop
92
+ * - Undefined to get the default (first) Y axis
93
+ * @returns The configuration for a single Y axis.
94
+ *
95
+ * @example
96
+ * // Get the default Y axis
97
+ * const yAxis = useYAxis();
98
+ *
99
+ * @example
100
+ * // Get a specific Y axis by string ID
101
+ * const yAxis = useYAxis('temperature');
70
102
  */
71
103
  export function useYAxis(axisId) {
72
104
  const store = useStore();
@@ -77,6 +109,22 @@ export function useYAxis(axisId) {
77
109
  const id = axisId ?? yAxisIds[0];
78
110
  return yAxis[id];
79
111
  }
112
+
113
+ /**
114
+ * Get all the rotation axes for polar charts.
115
+ *
116
+ * Returns all rotation axes configured in polar charts along with their IDs.
117
+ * Rotation axes are used in charts like `RadarChart` to define angular positioning.
118
+ *
119
+ * @returns An object containing:
120
+ * - `rotationAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
121
+ * - `rotationAxisIds`: An array of all rotation axis IDs in the chart
122
+ *
123
+ * @example
124
+ * const { rotationAxis, rotationAxisIds } = useRotationAxes();
125
+ *
126
+ * @see `useRotationAxis` for accessing a single rotation axis
127
+ */
80
128
  export function useRotationAxes() {
81
129
  const store = useStore();
82
130
  const {
@@ -88,6 +136,22 @@ export function useRotationAxes() {
88
136
  rotationAxisIds
89
137
  };
90
138
  }
139
+
140
+ /**
141
+ * Get all the radius axes for polar charts.
142
+ *
143
+ * Returns all radial axes configured in polar charts along with their IDs.
144
+ * Radius axes are used in charts like `RadarChart` to define radial positioning and scaling.
145
+ *
146
+ * @returns An object containing:
147
+ * - `radiusAxis`: An object mapping axis IDs to their configurations `{ [axisId]: axis }`
148
+ * - `radiusAxisIds`: An array of all radius axis IDs in the chart
149
+ *
150
+ * @example
151
+ * const { radiusAxis, radiusAxisIds } = useRadiusAxes();
152
+ *
153
+ * @see `useRadiusAxis` for accessing a single radius axis
154
+ */
91
155
  export function useRadiusAxes() {
92
156
  const store = useStore();
93
157
  const {
@@ -99,21 +163,61 @@ export function useRadiusAxes() {
99
163
  radiusAxisIds
100
164
  };
101
165
  }
102
- export function useRotationAxis(identifier) {
166
+
167
+ /**
168
+ * Get a specific rotation axis or the default rotation axis for polar charts.
169
+ *
170
+ * Returns the configuration and scale for a rotation axis in polar charts.
171
+ * The rotation axis controls the angular positioning of data points around the circle.
172
+ *
173
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
174
+ * - A string or number matching the axis ID defined in the chart's rotation axis configuration
175
+ * - Undefined to get the default (first) rotation axis
176
+ * @returns The rotation axis configuration, or undefined if not found
177
+ *
178
+ * @example
179
+ * // Get the default rotation axis
180
+ * const rotationAxis = useRotationAxis();
181
+ *
182
+ * @example
183
+ * // Get a specific rotation axis by string ID
184
+ * const rotationAxis = useRotationAxis('categories');
185
+ */
186
+ export function useRotationAxis(axisId) {
103
187
  const store = useStore();
104
188
  const {
105
189
  axis: rotationAxis,
106
190
  axisIds: rotationAxisIds
107
191
  } = useSelector(store, selectorChartRotationAxis);
108
- const id = typeof identifier === 'string' ? identifier : rotationAxisIds[identifier ?? 0];
192
+ const id = axisId ?? rotationAxisIds[0];
109
193
  return rotationAxis[id];
110
194
  }
111
- export function useRadiusAxis(identifier) {
195
+
196
+ /**
197
+ * Get a specific radius axis or the default radius axis for polar charts.
198
+ *
199
+ * Returns the configuration and scale for a radial axis in polar charts.
200
+ * The radius axis controls the radial distance of data points from the center of the circle.
201
+ *
202
+ * @param {AxisId} [axisId] - The axis identifier. Can be:
203
+ * - A string or number matching the axis ID defined in the chart's radius axis configuration
204
+ * - Undefined to get the default (first) radius axis
205
+ * @returns The radius axis configuration, or undefined if not found
206
+ *
207
+ * @example
208
+ * // Get the default radius axis
209
+ * const radiusAxis = useRadiusAxis();
210
+ *
211
+ * @example
212
+ * // Get a specific radius axis by string ID
213
+ * const radiusAxis = useRadiusAxis('magnitude');
214
+ */
215
+ export function useRadiusAxis(axisId) {
112
216
  const store = useStore();
113
217
  const {
114
218
  axis: radiusAxis,
115
219
  axisIds: radiusAxisIds
116
220
  } = useSelector(store, selectorChartRadiusAxis);
117
- const id = typeof identifier === 'string' ? identifier : radiusAxisIds[identifier ?? 0];
221
+ const id = axisId ?? radiusAxisIds[0];
118
222
  return radiusAxis[id];
119
223
  }
@@ -13,16 +13,35 @@ export declare function getValueToPositionMapper<Domain extends {
13
13
  /**
14
14
  * Get the X scale.
15
15
  *
16
- * @param {AxisId | undefined} axisId - If provided returns the scale for the x axis with axisId, else returns the values for the default x axis.
17
- * @returns {AxisScaleConfig[S]['scale']} The scale for the specified X axis.
16
+ * @param axisId - The axis identifier. Can be:
17
+ * - A string or number matching the axis ID defined in the chart's `xAxis` prop
18
+ * - Undefined to get the default (first) X axis
19
+ * @returns The X axis scale
18
20
  */
19
21
  export declare function useXScale<S extends ScaleName>(axisId?: AxisId): AxisScaleConfig[S]['scale'];
20
22
  /**
21
23
  * Get the Y scale.
22
24
  *
23
- * @param {AxisId | undefined} axisId - If provided returns the scale for the y axis with axisId, else returns the values for the default y axis.
24
- * @returns {AxisScaleConfig[S]['scale']} The scale for the specified Y axis.
25
+ * @param axisId - The axis identifier. Can be:
26
+ * - A string or number matching the axis ID defined in the chart's `yAxis` prop
27
+ * - Undefined to get the default (first) Y axis
28
+ * @returns The Y axis scale
25
29
  */
26
30
  export declare function useYScale<S extends ScaleName>(axisId?: AxisId): AxisScaleConfig[S]['scale'];
27
- export declare function useRotationScale<S extends ScaleName>(identifier?: number | string): AxisScaleConfig[S]['scale'] | undefined;
28
- export declare function useRadiusScale<S extends ScaleName>(identifier?: number | string): AxisScaleConfig[S]['scale'] | undefined;
31
+ /**
32
+ * Get the rotation scale.
33
+ *
34
+ * @param axisId - The axis identifier. Can be:
35
+ * - A string or number matching the axis ID defined in the chart's `rotationAxis` prop
36
+ * - Undefined to get the default rotation axis
37
+ * @returns The rotation axis scale, or undefined if not found
38
+ */
39
+ export declare function useRotationScale<S extends ScaleName>(axisId?: number | string): AxisScaleConfig[S]['scale'] | undefined;
40
+ /**
41
+ * Get the radius scale.
42
+ * @param axisId - The axis identifier. Can be:
43
+ * - A string or number matching the axis ID defined in the chart's `radiusAxis` prop
44
+ * - Undefined to get the default radius axis
45
+ * @returns The radius axis scale, or undefined if not found
46
+ */
47
+ export declare function useRadiusScale<S extends ScaleName>(axisId?: number | string): AxisScaleConfig[S]['scale'] | undefined;
@@ -25,8 +25,10 @@ export function getValueToPositionMapper(scale) {
25
25
  /**
26
26
  * Get the X scale.
27
27
  *
28
- * @param {AxisId | undefined} axisId - If provided returns the scale for the x axis with axisId, else returns the values for the default x axis.
29
- * @returns {AxisScaleConfig[S]['scale']} The scale for the specified X axis.
28
+ * @param axisId - The axis identifier. Can be:
29
+ * - A string or number matching the axis ID defined in the chart's `xAxis` prop
30
+ * - Undefined to get the default (first) X axis
31
+ * @returns The X axis scale
30
32
  */
31
33
  export function useXScale(axisId) {
32
34
  const axis = useXAxis(axisId);
@@ -36,18 +38,37 @@ export function useXScale(axisId) {
36
38
  /**
37
39
  * Get the Y scale.
38
40
  *
39
- * @param {AxisId | undefined} axisId - If provided returns the scale for the y axis with axisId, else returns the values for the default y axis.
40
- * @returns {AxisScaleConfig[S]['scale']} The scale for the specified Y axis.
41
+ * @param axisId - The axis identifier. Can be:
42
+ * - A string or number matching the axis ID defined in the chart's `yAxis` prop
43
+ * - Undefined to get the default (first) Y axis
44
+ * @returns The Y axis scale
41
45
  */
42
46
  export function useYScale(axisId) {
43
47
  const axis = useYAxis(axisId);
44
48
  return axis.scale;
45
49
  }
46
- export function useRotationScale(identifier) {
47
- const axis = useRotationAxis(identifier);
50
+
51
+ /**
52
+ * Get the rotation scale.
53
+ *
54
+ * @param axisId - The axis identifier. Can be:
55
+ * - A string or number matching the axis ID defined in the chart's `rotationAxis` prop
56
+ * - Undefined to get the default rotation axis
57
+ * @returns The rotation axis scale, or undefined if not found
58
+ */
59
+ export function useRotationScale(axisId) {
60
+ const axis = useRotationAxis(axisId);
48
61
  return axis?.scale;
49
62
  }
50
- export function useRadiusScale(identifier) {
51
- const axis = useRadiusAxis(identifier);
63
+
64
+ /**
65
+ * Get the radius scale.
66
+ * @param axisId - The axis identifier. Can be:
67
+ * - A string or number matching the axis ID defined in the chart's `radiusAxis` prop
68
+ * - Undefined to get the default radius axis
69
+ * @returns The radius axis scale, or undefined if not found
70
+ */
71
+ export function useRadiusScale(axisId) {
72
+ const axis = useRadiusAxis(axisId);
52
73
  return axis?.scale;
53
74
  }
package/esm/index.js CHANGED
@@ -1,5 +1,5 @@
1
1
  /**
2
- * @mui/x-charts v8.17.0
2
+ * @mui/x-charts v8.18.0
3
3
  *
4
4
  * @license MIT
5
5
  * This source code is licensed under the MIT license found in the
@@ -44,6 +44,13 @@ export interface BarSeriesType extends CommonSeriesType<number | null>, Cartesia
44
44
  * @returns {string} The formatted label.
45
45
  */
46
46
  barLabel?: 'value' | ((item: BarItem, context: BarLabelContext) => string | null | undefined);
47
+ /**
48
+ * The placement of the bar label. It accepts the following values:
49
+ * - 'center': the label is centered on the bar
50
+ * - 'outside': the label is placed after the end of the bar, from the point of the view of the origin. For a vertical positive bar, the label is above its top edge; for a horizontal negative bar, the label is placed to the left of its leftmost limit.
51
+ * @default 'center'
52
+ */
53
+ barLabelPlacement?: 'center' | 'outside';
47
54
  }
48
55
  /**
49
56
  * An object that allows to identify a single bar.
@@ -1,6 +1,6 @@
1
1
  import * as React from 'react';
2
2
  import type { BarLabelProps } from "../../BarChart/index.js";
3
- type UseAnimateBarLabelParams = Pick<BarLabelProps, 'xOrigin' | 'yOrigin' | 'x' | 'y' | 'width' | 'height' | 'layout' | 'skipAnimation'> & {
3
+ type UseAnimateBarLabelParams = Pick<BarLabelProps, 'xOrigin' | 'yOrigin' | 'x' | 'y' | 'width' | 'height' | 'layout' | 'skipAnimation' | 'placement'> & {
4
4
  ref?: React.Ref<SVGTextElement>;
5
5
  };
6
6
  type UseAnimateBarLabelReturn = {
@@ -11,14 +11,12 @@ function barLabelPropsInterpolator(from, to) {
11
11
  const interpolateY = (0, _d3Interpolate.interpolateNumber)(from.y, to.y);
12
12
  const interpolateWidth = (0, _d3Interpolate.interpolateNumber)(from.width, to.width);
13
13
  const interpolateHeight = (0, _d3Interpolate.interpolateNumber)(from.height, to.height);
14
- return t => {
15
- return {
16
- x: interpolateX(t),
17
- y: interpolateY(t),
18
- width: interpolateWidth(t),
19
- height: interpolateHeight(t)
20
- };
21
- };
14
+ return t => ({
15
+ x: interpolateX(t),
16
+ y: interpolateY(t),
17
+ width: interpolateWidth(t),
18
+ height: interpolateHeight(t)
19
+ });
22
20
  }
23
21
 
24
22
  /**
@@ -31,15 +29,21 @@ function barLabelPropsInterpolator(from, to) {
31
29
  * pass the ref returned by this hook to the `path` element and the `ref` provided as argument will also be called.
32
30
  */
33
31
  function useAnimateBarLabel(props) {
32
+ const {
33
+ initialX,
34
+ currentX,
35
+ initialY,
36
+ currentY
37
+ } = props.placement === 'outside' ? getOutsidePlacement(props) : getCenterPlacement(props);
34
38
  const initialProps = {
35
- x: props.layout === 'vertical' ? props.x + props.width / 2 : props.xOrigin,
36
- y: props.layout === 'vertical' ? props.yOrigin : props.y + props.height / 2,
39
+ x: initialX,
40
+ y: initialY,
37
41
  width: props.width,
38
42
  height: props.height
39
43
  };
40
44
  const currentProps = {
41
- x: props.x + props.width / 2,
42
- y: props.y + props.height / 2,
45
+ x: currentX,
46
+ y: currentY,
43
47
  width: props.width,
44
48
  height: props.height
45
49
  };
@@ -56,4 +60,49 @@ function useAnimateBarLabel(props) {
56
60
  skip: props.skipAnimation,
57
61
  ref: props.ref
58
62
  });
63
+ }
64
+ const LABEL_OFFSET = 4;
65
+ function getCenterPlacement(props) {
66
+ return {
67
+ initialX: props.layout === 'vertical' ? props.x + props.width / 2 : props.xOrigin,
68
+ initialY: props.layout === 'vertical' ? props.yOrigin : props.y + props.height / 2,
69
+ currentX: props.x + props.width / 2,
70
+ currentY: props.y + props.height / 2
71
+ };
72
+ }
73
+ function getOutsidePlacement(props) {
74
+ let initialY = 0;
75
+ let currentY = 0;
76
+ let initialX = 0;
77
+ let currentX = 0;
78
+ if (props.layout === 'vertical') {
79
+ const shouldPlaceAbove = props.y < props.yOrigin;
80
+ if (shouldPlaceAbove) {
81
+ initialY = props.yOrigin - LABEL_OFFSET;
82
+ currentY = props.y - LABEL_OFFSET;
83
+ } else {
84
+ initialY = props.yOrigin + LABEL_OFFSET;
85
+ currentY = props.y + props.height + LABEL_OFFSET;
86
+ }
87
+ return {
88
+ initialX: props.x + props.width / 2,
89
+ currentX: props.x + props.width / 2,
90
+ initialY,
91
+ currentY
92
+ };
93
+ }
94
+ const shouldPlaceToTheLeft = props.x < props.xOrigin;
95
+ if (shouldPlaceToTheLeft) {
96
+ initialX = props.xOrigin;
97
+ currentX = props.x - LABEL_OFFSET;
98
+ } else {
99
+ initialX = props.xOrigin;
100
+ currentX = props.x + props.width + LABEL_OFFSET;
101
+ }
102
+ return {
103
+ initialX,
104
+ currentX,
105
+ initialY: props.y + props.height / 2,
106
+ currentY: props.y + props.height / 2
107
+ };
59
108
  }