Skip to content

Commit

Permalink
[@mantine/charts] Fix valueFormatter prop not working correctly wit…
Browse files Browse the repository at this point in the history
…h `orientation="vertical"` in BarChart, AreaChart and LineChart components (#6979)
  • Loading branch information
rtivital committed Oct 16, 2024
1 parent bb19ad7 commit 7567fec
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 3 deletions.
22 changes: 22 additions & 0 deletions packages/@mantine/charts/src/AreaChart/AreaChart.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,28 @@ export function Vertical() {
);
}

export function VerticalValueFormatter() {
return (
<div style={{ padding: 40 }}>
<AreaChart
h={400}
data={data}
dataKey="date"
curveType="linear"
withGradient
withLegend
orientation="vertical"
valueFormatter={(value) => `$ ${value * 2}`}
series={[
{ name: 'Apples', color: 'indigo.6' },
{ name: 'Oranges', color: 'blue.6' },
{ name: 'Tomatoes', color: 'teal.6' },
]}
/>
</div>
);
}

export function WithLegend() {
return (
<div style={{ padding: 40 }}>
Expand Down
5 changes: 4 additions & 1 deletion packages/@mantine/charts/src/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,8 @@ export const AreaChart = factory<AreaChartFactory>((_props, ref) => {
);
});

const tickFormatter = type === 'percent' ? valueToPercent : valueFormatter;

const sharedYAxisProps = {
axisLine: false,
...(orientation === 'vertical'
Expand All @@ -334,7 +336,7 @@ export const AreaChart = factory<AreaChartFactory>((_props, ref) => {
tickLine: withYTickLine ? { stroke: 'currentColor' } : false,
allowDecimals: true,
unit,
tickFormatter: type === 'percent' ? valueToPercent : valueFormatter,
tickFormatter: orientation === 'vertical' ? undefined : tickFormatter,
...getStyles('axis'),
};

Expand Down Expand Up @@ -392,6 +394,7 @@ export const AreaChart = factory<AreaChartFactory>((_props, ref) => {
interval="preserveStartEnd"
tickLine={withXTickLine ? { stroke: 'currentColor' } : false}
minTickGap={5}
tickFormatter={orientation === 'vertical' ? tickFormatter : undefined}
{...getStyles('axis')}
{...xAxisProps}
>
Expand Down
20 changes: 20 additions & 0 deletions packages/@mantine/charts/src/BarChart/BarChart.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,23 @@ export function ReferenceLines() {
</div>
);
}

export function VerticalOrientationValueFormatter() {
return (
<BarChart
h={300}
p="xl"
data={data}
dataKey="month"
type="stacked"
orientation="vertical"
valueFormatter={(value) => `${value * 2}%`}
yAxisProps={{ width: 80 }}
series={[
{ name: 'Smartphones', color: 'violet.6' },
{ name: 'Laptops', color: 'blue.6' },
{ name: 'Tablets', color: 'teal.6' },
]}
/>
);
}
5 changes: 4 additions & 1 deletion packages/@mantine/charts/src/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,8 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
);
});

const tickFormatter = type === 'percent' ? valueToPercent : valueFormatter;

const sharedYAxisProps = {
axisLine: false,
...(orientation === 'vertical'
Expand All @@ -303,7 +305,7 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
tickLine: withYTickLine ? { stroke: 'currentColor' } : false,
allowDecimals: true,
unit,
tickFormatter: type === 'percent' ? valueToPercent : valueFormatter,
tickFormatter: orientation === 'vertical' ? undefined : tickFormatter,
...getStyles('axis'),
};

Expand Down Expand Up @@ -354,6 +356,7 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
interval="preserveStartEnd"
tickLine={withXTickLine ? { stroke: 'currentColor' } : false}
minTickGap={5}
tickFormatter={orientation === 'vertical' ? tickFormatter : undefined}
{...getStyles('axis')}
{...xAxisProps}
>
Expand Down
20 changes: 20 additions & 0 deletions packages/@mantine/charts/src/LineChart/LineChart.story.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,26 @@ export function Vertical() {
);
}

export function VerticalValueFormatter() {
return (
<div style={{ padding: 40 }}>
<LineChart
h={300}
data={data}
dataKey="date"
orientation="vertical"
valueFormatter={(value) => `$ ${value * 2}`}
series={[
{ name: 'Apples', color: 'indigo.6' },
{ name: 'Oranges', color: 'blue.6' },
{ name: 'Tomatoes', color: 'teal.6' },
]}
withLegend
/>
</div>
);
}

export function ReferenceLines() {
return (
<div style={{ padding: 40 }}>
Expand Down
3 changes: 2 additions & 1 deletion packages/@mantine/charts/src/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ export const LineChart = factory<LineChartFactory>((_props, ref) => {
tickLine: withYTickLine ? { stroke: 'currentColor' } : false,
allowDecimals: true,
unit,
tickFormatter: valueFormatter,
tickFormatter: orientation === 'vertical' ? undefined : valueFormatter,
...getStyles('axis'),
};

Expand Down Expand Up @@ -373,6 +373,7 @@ export const LineChart = factory<LineChartFactory>((_props, ref) => {
interval="preserveStartEnd"
tickLine={withXTickLine ? { stroke: 'currentColor' } : false}
minTickGap={5}
tickFormatter={orientation === 'vertical' ? valueFormatter : undefined}
{...getStyles('axis')}
{...xAxisProps}
>
Expand Down

0 comments on commit 7567fec

Please sign in to comment.