Skip to content

Commit a86fce6

Browse files
authored
fix(charts): fix description and behavior of chartConfig.legendPosition (#4911)
1 parent e0c6d64 commit a86fce6

File tree

10 files changed

+24
-13
lines changed

10 files changed

+24
-13
lines changed

packages/charts/src/components/BarChart/BarChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ const BarChart = forwardRef<HTMLDivElement, BarChartProps>((props, ref) => {
214214
const isBigDataSet = dataset?.length > 30;
215215
const primaryDimensionAccessor = primaryDimension?.accessor;
216216

217-
const [width, legendPosition] = useLongestYAxisLabelBar(dataset, dimensions);
217+
const [width, legendPosition] = useLongestYAxisLabelBar(dataset, dimensions, chartConfig.legendPosition);
218218
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
219219
const [xAxisHeight] = useObserveXAxisHeights(chartRef, 1);
220220
const isRTL = useIsRTL(chartRef);

packages/charts/src/components/BulletChart/BulletChart.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,8 @@ const BulletChart = forwardRef<HTMLDivElement, BulletChartProps>((props, ref) =>
242242

243243
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(
244244
dataset,
245-
layout === 'vertical' ? dimensions : sortedMeasures
245+
layout === 'vertical' ? dimensions : sortedMeasures,
246+
chartConfig.legendPosition
246247
);
247248

248249
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);

packages/charts/src/components/ColumnChart/ColumnChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ const ColumnChart = forwardRef<HTMLDivElement, ColumnChartProps>((props, ref) =>
171171

172172
const tooltipValueFormatter = useTooltipFormatter(measures);
173173

174-
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, measures);
174+
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, measures, chartConfig.legendPosition);
175175

176176
const primaryDimension = dimensions[0];
177177
const { primaryMeasure, secondaryMeasure } = resolvePrimaryAndSecondaryMeasures(

packages/charts/src/components/ColumnChartWithTrend/ColumnChartWithTrend.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ const ColumnChartWithTrend = forwardRef<HTMLDivElement, ColumnChartWithTrendProp
144144
);
145145

146146
const { lineMeasures, columnMeasures, columnDataset } = usePrepareTrendMeasures(measures, dataset);
147-
const [yAxisWidth] = useLongestYAxisLabel(columnDataset, columnMeasures);
147+
const [yAxisWidth] = useLongestYAxisLabel(columnDataset, columnMeasures, chartConfig.legendPosition);
148148

149149
const columnTooltipConfig = {
150150
formatter: (value, name, tooltipProps) => {

packages/charts/src/components/ComposedChart/index.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@ const ComposedChart = forwardRef<HTMLDivElement, ComposedChartProps>((props, ref
246246
const isBigDataSet = dataset?.length > 30 ?? false;
247247
const primaryDimensionAccessor = primaryDimension?.accessor;
248248

249-
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, layout === 'vertical' ? dimensions : measures);
249+
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(
250+
dataset,
251+
layout === 'vertical' ? dimensions : measures,
252+
chartConfig.legendPosition
253+
);
250254

251255
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
252256
const xAxisHeights = useObserveXAxisHeights(chartRef, layout === 'vertical' ? 1 : props.dimensions.length);

packages/charts/src/components/LineChart/LineChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ const LineChart = forwardRef<HTMLDivElement, LineChartProps>((props, ref) => {
203203
const isBigDataSet = dataset?.length > 30 ?? false;
204204
const primaryDimensionAccessor = primaryDimension?.accessor;
205205

206-
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, measures);
206+
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset, measures, chartConfig.legendPosition);
207207
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
208208
const xAxisHeights = useObserveXAxisHeights(chartRef, props.dimensions.length);
209209
const { chartConfig: _0, dimensions: _1, measures: _2, ...propsWithoutOmitted } = rest;

packages/charts/src/components/ScatterChart/ScatterChart.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@ const ScatterChart = forwardRef<HTMLDivElement, ScatterChartProps>((props, ref)
206206
const yMeasure = measures.find(({ axis }) => axis === 'y');
207207
const zMeasure = measures.find(({ axis }) => axis === 'z');
208208

209-
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset?.[0]?.data, [yMeasure]);
209+
const [yAxisWidth, legendPosition] = useLongestYAxisLabel(dataset?.[0]?.data, [yMeasure], chartConfig.legendPosition);
210210
const xAxisHeights = useObserveXAxisHeights(chartRef, 1);
211211
const marginChart = useChartMargin(chartConfig.margin, chartConfig.zoomingTool);
212212
const { chartConfig: _0, measures: _1, ...propsWithoutOmitted } = rest;

packages/charts/src/hooks/useLongestYAxisLabel.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getValueByDataKey } from 'recharts/lib/util/ChartUtils.js';
33
import { defaultMaxYAxisWidth } from '../internal/defaults.js';
44
import { getTextWidth } from '../internal/Utils.js';
55

6-
export const useLongestYAxisLabel = (dataset: unknown[], elements): [number, object] =>
6+
export const useLongestYAxisLabel = (dataset: unknown[], elements, legendPosition: string): [number, object] =>
77
useMemo(() => {
88
let labelLength = 0;
99
const primaryElement = elements[0];
@@ -20,5 +20,9 @@ export const useLongestYAxisLabel = (dataset: unknown[], elements): [number, obj
2020

2121
labelLength = Math.min(labelLength, defaultMaxYAxisWidth);
2222

23+
if (legendPosition === 'middle') {
24+
return [labelLength, { width: 'auto' }];
25+
}
26+
2327
return [labelLength, { marginLeft: labelLength, maxWidth: `calc(100% - ${labelLength + 10}px)` }];
2428
}, [dataset, elements]);

packages/charts/src/hooks/useLongestYAxisLabelBar.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getValueByDataKey } from 'recharts/lib/util/ChartUtils.js';
33
import { defaultMaxYAxisWidth } from '../internal/defaults.js';
44
import { getTextWidth } from '../internal/Utils.js';
55

6-
export const useLongestYAxisLabelBar = (dataset: unknown[], elements): [number[], object] =>
6+
export const useLongestYAxisLabelBar = (dataset: unknown[], elements, legendPosition = ''): [number[], object] =>
77
useMemo(() => {
88
let axisWidths = Array(elements.length).fill(0);
99
let marginLeft = 0;
@@ -25,6 +25,8 @@ export const useLongestYAxisLabelBar = (dataset: unknown[], elements): [number[]
2525
axisWidths = axisWidths.map((length) => Math.min(defaultMaxYAxisWidth, length));
2626
marginLeft = axisWidths.reduce((acc, val) => acc + val, 0);
2727
}
28-
28+
if (legendPosition === 'middle') {
29+
return [axisWidths, { width: 'auto' }];
30+
}
2931
return [axisWidths, { marginLeft, maxWidth: `calc(100% - ${marginLeft + 8}px)` }];
3032
}, [dataset, elements]);

packages/charts/src/interfaces/IChartBaseProps.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ export interface IChartBaseProps<T = ICartesianChartConfig> extends Omit<CommonP
5858
* **Properties available on all charts:**
5959
*
6060
* - `margin`: Sets the margin of the chart container. Receives a object with four possible properties (`right`, `left`, `top`, `bottom`) that expect a number as value.
61-
* - `legendPosition`: Position of the legend. Can be one of the following: `"top"`, `"left"`, `"right"`, `"bottom"`
61+
* - `legendPosition`: Vertical position of the legend. Can be one of the following: `"top"`,`"middle"`, `"bottom"` (`"middle"` is not supported for: ColumnChartWithTrend, DonutChart, PieChart)
6262
* - `legendHorizontalAlign`: Alignment of the legend. Can be one of the following: `"left"`, `"center"`, `"right"`
6363
* - `resizeDebounce`: Number that sets the amount of delay time the chart waits when resizing.
6464
*
@@ -75,11 +75,11 @@ export interface IChartBaseProps<T = ICartesianChartConfig> extends Omit<CommonP
7575
bottom: number;
7676
};
7777
/**
78-
* Position of the legend. Can be one of the following: `"top"`, `"left"`, `"right"`, `"bottom"`
78+
* Vertical position of the legend. Can be one of the following: `"top"`,`"middle"`, `"bottom"` (`"middle"` is not supported for: ColumnChartWithTrend, DonutChart, PieChart)
7979
*/
8080
legendPosition?: string;
8181
/**
82-
* Alignment of the legend. Can be one of the following: `"left"`, `"center"`, `"right"`
82+
* Horizontal alignment of the legend. Can be one of the following: `"left"`, `"center"`, `"right"`
8383
*/
8484
legendHorizontalAlign?: string;
8585
/**

0 commit comments

Comments
 (0)