Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use LabelList instead of BarLabel, and add valueLabelProps option support. #7160

Merged
merged 3 commits into from
Nov 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/mantine.dev/src/pages/charts/bar-chart.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,12 @@ rendered behind the chart.
To display value above each bar, set `withBarValueLabel`:

<Demo data={BarChartDemos.barValueLabel} />

## Bar value label props

You can pass props down to recharts [LabelList](https://recharts.org/en-US/api/LabelList)
component with `valueLabelProps` prop. `valueLabelProps` accepts either an object with props
or a function that receives series data as an argument and returns an object with
props.

<Demo data={BarChartDemos.valueLabelProps} />
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { BarChart } from '@mantine/charts';
import { MantineDemo } from '@mantinex/demo';
import { data, dataCode } from './_data';

const code = `
import { BarChart } from '@mantine/charts';
import { data } from './data';

function Demo() {
return (
<BarChart
h={300}
data={data}
dataKey="month"
valueFormatter={(value) => new Intl.NumberFormat('en-US').format(value)}
withBarValueLabel
valueLabelProps={{ position: 'inside', fill: 'white' }}
series={[
{ name: 'Smartphones', color: 'violet.6' },
{ name: 'Laptops', color: 'blue.6' },
{ name: 'Tablets', color: 'teal.6' },
]}
/>
);
}
`;

function Demo() {
return (
<BarChart
h={300}
data={data}
dataKey="month"
valueFormatter={(value) => new Intl.NumberFormat('en-US').format(value)}
withBarValueLabel
valueLabelProps={{ position: 'inside', fill: 'white' }}
series={[
{ name: 'Smartphones', color: 'violet.6' },
{ name: 'Laptops', color: 'blue.6' },
{ name: 'Tablets', color: 'teal.6' },
]}
/>
);
}

export const valueLabelProps: MantineDemo = {
type: 'code',
component: Demo,
code: [
{ code, language: 'tsx', fileName: 'Demo.tsx' },
{ code: dataCode, language: 'tsx', fileName: 'data.ts' },
],
};
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,11 @@ export const Demo_barValueLabel = {
render: renderDemo(demos.barValueLabel),
};

export const Demo_valueLabelProps = {
name: '⭐ Demo: valueLabelProps',
render: renderDemo(demos.valueLabelProps),
};

export const Demo_minBarSize = {
name: '⭐ Demo: minBarSize',
render: renderDemo(demos.minBarSize),
Expand Down
1 change: 1 addition & 0 deletions packages/@docs/demos/src/demos/charts/BarChart/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ export { barValueLabel } from './BarChart.demo.barValueLabel';
export { minBarSize } from './BarChart.demo.minBarSize';
export { mixedStack } from './BarChart.demo.mixedStack';
export { stripes } from './BarChart.demo.stripes';
export { valueLabelProps } from './BarChart.demo.valueLabelProps';
27 changes: 19 additions & 8 deletions packages/@mantine/charts/src/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
CartesianGrid,
Cell,
Label,
LabelList,
LabelListProps,
Legend,
BarChart as ReChartsBarChart,
ReferenceLine,
Expand All @@ -31,7 +33,6 @@ import {
import { ChartLegend, ChartLegendStylesNames } from '../ChartLegend';
import { ChartTooltip, ChartTooltipStylesNames } from '../ChartTooltip';
import type { BaseChartStylesNames, ChartSeries, GridChartBaseProps } from '../types';
import { BarLabel } from './BarLabel';
import classes from '../grid-chart.module.css';

function valueToPercent(value: number) {
Expand Down Expand Up @@ -92,6 +93,11 @@ export interface BarChartProps
/** Determines whether a label with bar value should be displayed on top of each bar, incompatible with `type="stacked"` and `type="percent"`, `false` by default */
withBarValueLabel?: boolean;

/** Props passed down to recharts `LabelList` component */
valueLabelProps?:
| ((series: BarChartSeries) => Partial<Omit<LabelListProps<Record<string, any>>, 'ref'>>)
| Partial<LabelListProps<Record<string, any>>>;

/** Sets minimum height of the bar in px, `0` by default */
minBarSize?: number;

Expand Down Expand Up @@ -202,6 +208,7 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
xAxisLabel,
yAxisLabel,
withBarValueLabel,
valueLabelProps,
withRightYAxis,
rightYAxisLabel,
rightYAxisProps,
Expand All @@ -217,6 +224,8 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
const [highlightedArea, setHighlightedArea] = useState<string | null>(null);
const shouldHighlight = highlightedArea !== null;
const stacked = type === 'stacked' || type === 'percent';
const tickFormatter = type === 'percent' ? valueToPercent : valueFormatter;

const handleMouseLeave = (event: React.MouseEvent<HTMLDivElement>) => {
setHighlightedArea(null);
onMouseLeave?.(event);
Expand Down Expand Up @@ -258,11 +267,6 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
fillOpacity={dimmed ? 0.1 : fillOpacity}
strokeOpacity={dimmed ? 0.2 : 0}
stackId={stacked ? 'stack' : item.stackId || undefined}
label={
withBarValueLabel ? (
<BarLabel valueFormatter={valueFormatter} orientation={orientation} />
) : undefined
}
yAxisId={item.yAxisId || 'left'}
minPointSize={minBarSize}
{...(typeof barProps === 'function' ? barProps(item) : barProps)}
Expand All @@ -275,6 +279,15 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
}
/>
))}
{withBarValueLabel && (
<LabelList
position={orientation === 'vertical' ? 'right' : 'top'}
fontSize={12}
fill="var(--chart-bar-label-color, var(--mantine-color-dimmed))"
formatter={tickFormatter}
{...(typeof valueLabelProps === 'function' ? valueLabelProps(item) : valueLabelProps)}
/>
)}
</Bar>
);
});
Expand All @@ -299,8 +312,6 @@ export const BarChart = factory<BarChartFactory>((_props, ref) => {
);
});

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

const sharedYAxisProps = {
axisLine: false,
...(orientation === 'vertical'
Expand Down
23 changes: 0 additions & 23 deletions packages/@mantine/charts/src/BarChart/BarLabel.tsx

This file was deleted.

14 changes: 11 additions & 3 deletions packages/@mantine/charts/src/CompositeChart/CompositeChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
CartesianGrid,
DotProps,
Label,
LabelList,
Legend,
Line,
LineProps,
Expand All @@ -31,7 +32,6 @@ import {
useResolvedStylesApi,
useStyles,
} from '@mantine/core';
import { BarLabel } from '../BarChart/BarLabel';
import { ChartLegend, ChartLegendStylesNames } from '../ChartLegend';
import { ChartTooltip, ChartTooltipStylesNames } from '../ChartTooltip';
import { PointLabel } from '../PointLabel/PointLabel';
Expand Down Expand Up @@ -340,11 +340,19 @@ export const CompositeChart = factory<CompositeChartFactory>((_props, ref) => {
isAnimationActive={false}
fillOpacity={dimmed ? 0.1 : 1}
strokeOpacity={dimmed ? 0.2 : 0}
label={withBarValueLabel ? <BarLabel valueFormatter={valueFormatter} /> : undefined}
yAxisId={item.yAxisId || 'left'}
minPointSize={minBarSize}
{...(typeof barProps === 'function' ? barProps(item) : barProps)}
/>
>
{withBarValueLabel && (
<LabelList
position="top"
fontSize={12}
fill="var(--chart-bar-label-color, var(--mantine-color-dimmed))"
formatter={valueFormatter}
/>
)}
</Bar>
);
}

Expand Down
4 changes: 0 additions & 4 deletions packages/@mantine/charts/src/grid-chart.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,6 @@
font-family: var(--mantine-font-family);
color: var(--chart-text-color, var(--mantine-color-dimmed));
}

&:where([data-orientation='vertical']) {
--chart-bar-label-color: var(--mantine-color-white);
}
}

.container {
Expand Down
Loading