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

fix(Charts): tooltip respects dimension formatter #614

Merged
merged 2 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
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: 8 additions & 1 deletion packages/charts/src/components/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from 'recharts';
import { getValueByDataKey } from 'recharts/lib/util/ChartUtils';
import { useChartMargin } from '../../hooks/useChartMargin';
import { useLabelFormatter } from '../../hooks/useLabelFormatter';
import { useLongestYAxisLabelBar } from '../../hooks/useLongestYAxisLabelBar';
import { useObserveXAxisHeights } from '../../hooks/useObserveXAxisHeights';
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures';
Expand Down Expand Up @@ -143,6 +144,7 @@ const BarChart: FC<BarChartProps> = forwardRef((props: BarChartProps, ref: Ref<H
const chartRef = useConsolidatedRef<any>(ref);

const onItemLegendClick = useLegendItemClick(onLegendClick);
const labelFormatter = useLabelFormatter(primaryDimension);

const onDataPointClickInternal = useCallback(
(payload, i, event) => {
Expand Down Expand Up @@ -261,7 +263,12 @@ const BarChart: FC<BarChartProps> = forwardRef((props: BarChartProps, ref: Ref<H
label={chartConfig.referenceLine.label}
/>
)}
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
<Tooltip
cursor={tooltipFillOpacity}
formatter={tooltipValueFormatter}
contentStyle={tooltipContentStyle}
labelFormatter={labelFormatter}
/>
{chartConfig.zoomingTool && (
<Brush
y={10}
Expand Down
9 changes: 8 additions & 1 deletion packages/charts/src/components/ColumnChart/ColumnChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
} from 'recharts';
import { getValueByDataKey } from 'recharts/lib/util/ChartUtils';
import { useChartMargin } from '../../hooks/useChartMargin';
import { useLabelFormatter } from '../../hooks/useLabelFormatter';
import { useLongestYAxisLabel } from '../../hooks/useLongestYAxisLabel';
import { useObserveXAxisHeights } from '../../hooks/useObserveXAxisHeights';
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures';
Expand Down Expand Up @@ -140,6 +141,7 @@ const ColumnChart: FC<ColumnChartProps> = forwardRef((props: ColumnChartProps, r
const primaryDimension = dimensions[0];
const primaryMeasure = measures[0];

const labelFormatter = useLabelFormatter(primaryDimension);
const chartRef = useConsolidatedRef<any>(ref);

const dataKeys = measures.map(({ accessor }) => accessor);
Expand Down Expand Up @@ -272,7 +274,12 @@ const ColumnChart: FC<ColumnChartProps> = forwardRef((props: ColumnChartProps, r
yAxisId={'left'}
/>
)}
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
<Tooltip
cursor={tooltipFillOpacity}
formatter={tooltipValueFormatter}
labelFormatter={labelFormatter}
contentStyle={tooltipContentStyle}
/>
{chartConfig.zoomingTool && (
<Brush
y={10}
Expand Down
10 changes: 9 additions & 1 deletion packages/charts/src/components/ComposedChart/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
YAxis
} from 'recharts';
import { useChartMargin } from '../../hooks/useChartMargin';
import { useLabelFormatter } from '../../hooks/useLabelFormatter';
import { useLongestYAxisLabel } from '../../hooks/useLongestYAxisLabel';
import { useObserveXAxisHeights } from '../../hooks/useObserveXAxisHeights';
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures';
Expand Down Expand Up @@ -151,6 +152,8 @@ const ComposedChart: FC<ComposedChartProps> = forwardRef((props: ComposedChartPr
const primaryDimension = dimensions[0];
const primaryMeasure = measures[0];

const labelFormatter = useLabelFormatter(primaryDimension);

const dataKeys = measures.map(({ accessor }) => accessor);
const colorSecondY = chartConfig.secondYAxis
? dataKeys.findIndex((key) => key === chartConfig.secondYAxis?.dataKey)
Expand Down Expand Up @@ -307,7 +310,12 @@ const ComposedChart: FC<ComposedChartProps> = forwardRef((props: ComposedChartPr
xAxisId={layout === 'vertical' ? 'primary' : undefined}
/>
)}
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
<Tooltip
cursor={tooltipFillOpacity}
formatter={tooltipValueFormatter}
labelFormatter={labelFormatter}
contentStyle={tooltipContentStyle}
/>
{!noLegend && (
<Legend
verticalAlign={chartConfig.legendPosition}
Expand Down
10 changes: 9 additions & 1 deletion packages/charts/src/components/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
YAxis
} from 'recharts';
import { useChartMargin } from '../../hooks/useChartMargin';
import { useLabelFormatter } from '../../hooks/useLabelFormatter';
import { useLongestYAxisLabel } from '../../hooks/useLongestYAxisLabel';
import { useObserveXAxisHeights } from '../../hooks/useObserveXAxisHeights';
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures';
Expand Down Expand Up @@ -124,6 +125,8 @@ const LineChart: FC<LineChartProps> = forwardRef((props: LineChartProps, ref: Re
const primaryDimension = dimensions[0];
const primaryMeasure = measures[0];

const labelFormatter = useLabelFormatter(primaryDimension);

const chartRef = useConsolidatedRef<any>(ref);

const dataKeys = measures.map(({ accessor }) => accessor);
Expand Down Expand Up @@ -252,7 +255,12 @@ const LineChart: FC<LineChartProps> = forwardRef((props: LineChartProps, ref: Re
yAxisId={'left'}
/>
)}
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
<Tooltip
cursor={tooltipFillOpacity}
formatter={tooltipValueFormatter}
contentStyle={tooltipContentStyle}
labelFormatter={labelFormatter}
/>
{chartConfig.zoomingTool && (
<Brush
y={10}
Expand Down
5 changes: 4 additions & 1 deletion packages/charts/src/components/PieChart/PieChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,10 @@ const PieChart: FC<PieChartProps> = forwardRef((props: PieChartProps, ref: Ref<H
[measure]
);

const tooltipValueFormatter = useCallback((value) => measure.formatter(value), [measure.formatter]);
const tooltipValueFormatter = useCallback((value, name) => [measure.formatter(value), dimension.formatter(name)], [
measure.formatter,
dimension.formatter
]);
const chartRef = useConsolidatedRef<any>(ref);

const onItemLegendClick = useLegendItemClick(onLegendClick, () => measure.accessor);
Expand Down
10 changes: 9 additions & 1 deletion packages/charts/src/components/RadarChart/RadarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
RadarChart as RadarChartLib,
Tooltip
} from 'recharts';
import { useLabelFormatter } from '../../hooks/useLabelFormatter';
import { usePrepareDimensionsAndMeasures } from '../../hooks/usePrepareDimensionsAndMeasures';
import { useTooltipFormatter } from '../../hooks/useTooltipFormatter';
import { IChartBaseProps } from '../../interfaces/IChartBaseProps';
Expand Down Expand Up @@ -106,6 +107,8 @@ const RadarChart: FC<RadarChartProps> = forwardRef((props: RadarChartProps, ref:

const primaryDimension = dimensions[0];

const labelFormatter = useLabelFormatter(primaryDimension);

const primaryDimensionAccessor = primaryDimension?.accessor;

const onItemLegendClick = useLegendItemClick(onLegendClick);
Expand Down Expand Up @@ -171,7 +174,12 @@ const RadarChart: FC<RadarChartProps> = forwardRef((props: RadarChartProps, ref:
/>
);
})}
<Tooltip cursor={tooltipFillOpacity} formatter={tooltipValueFormatter} contentStyle={tooltipContentStyle} />
<Tooltip
cursor={tooltipFillOpacity}
formatter={tooltipValueFormatter}
contentStyle={tooltipContentStyle}
labelFormatter={labelFormatter}
/>
{!noLegend && (
<Legend
verticalAlign={chartConfig.legendPosition}
Expand Down
12 changes: 12 additions & 0 deletions packages/charts/src/hooks/useLabelFormatter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { useCallback } from 'react';

export const useLabelFormatter = (primaryDimension) => {
return useCallback(
(name) => {
if (primaryDimension && typeof primaryDimension?.formatter === 'function') {
return primaryDimension.formatter(name);
}
},
[primaryDimension]
);
};