Skip to content

Commit

Permalink
tootlip callback barchart
Browse files Browse the repository at this point in the history
  • Loading branch information
severinlandolt committed Jun 19, 2024
1 parent c8a81f0 commit d1c4401
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/components/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -460,15 +460,29 @@ interface ChartTooltipProps {
label: string
categoryColors: Map<string, string>
valueFormatter: (value: number) => string
tooltipCallback?: TooltipCallback
}

type TooltipCallbackProps = Pick<
ChartTooltipProps,
"active" | "payload" | "label"
>

type TooltipCallback = (props: TooltipCallbackProps) => void

const ChartTooltip = ({
active,
payload,
label,
categoryColors,
valueFormatter,
tooltipCallback,
}: ChartTooltipProps) => {
React.useEffect(() => {
if (tooltipCallback) {
tooltipCallback({ active, payload, label })
}
}, [tooltipCallback, active, payload, label])
if (active && payload) {
const filteredPayload = payload.filter((item: any) => item.type !== "none")

Expand Down Expand Up @@ -562,6 +576,7 @@ interface BarChartProps extends React.HTMLAttributes<HTMLDivElement> {
layout?: "vertical" | "horizontal"
type?: "default" | "stacked" | "percent"
legendPosition?: "left" | "center" | "right"
tooltipCallback?: TooltipCallback
}

const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>(
Expand Down Expand Up @@ -594,6 +609,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>(
layout = "horizontal",
type = "default",
legendPosition = "right",
tooltipCallback,
...other
} = props
const paddingValue = !showXAxis && !showYAxis ? 0 : 20
Expand Down Expand Up @@ -796,6 +812,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>(
label={label}
valueFormatter={valueFormatter}
categoryColors={categoryColors}
tooltipCallback={tooltipCallback}
/>
)
) : (
Expand Down Expand Up @@ -855,4 +872,4 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>(

BarChart.displayName = "BarChart"

export { BarChart, type BarChartEventProps }
export { BarChart, type BarChartEventProps, type TooltipCallbackProps }
6 changes: 6 additions & 0 deletions src/components/BarChart/barchart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ export const WithOnValueChange: Story = {
},
}

export const WithTooltipCallback: Story = {
args: {
tooltipCallback: (props) => console.log(props),
},
}

export const WithLargeTickGap: Story = {
args: {
tickGap: 300,
Expand Down

0 comments on commit d1c4401

Please sign in to comment.