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

[TM-1466] change format in tootltip for doughnut chart #751

Merged
merged 1 commit into from
Dec 16, 2024
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,21 @@ const CustomLegend = ({ payload }: CustomLegendProps) => {

const CustomTooltip = ({ active, payload }: any) => {
if (active && payload && payload.length) {
const value = payload[0].value;
const total = payload[0].payload.total;
const percentage = (value / total) * 100;

const formattedValue = value.toLocaleString("en-US", {
minimumFractionDigits: 1,
maximumFractionDigits: 1
});

const formattedPercentage = percentage.toFixed(0);

return (
<div className="shadow-lg rounded border bg-white p-2">
<p className="font-medium">{payload[0].name}</p>
<p className="text-gray-600">{`Value: ${payload[0].value}`}</p>
<p className="text-gray-600">{`${formattedValue}ha (${formattedPercentage}%)`}</p>
</div>
);
}
Expand Down Expand Up @@ -75,6 +86,13 @@ const EcoRegionDoughnutChart: React.FC<EcoRegionDoughnutChartProps> = ({ data })
const { chartData } = data;
const [activeIndex, setActiveIndex] = useState<number | undefined>(undefined);

const total = chartData.reduce((sum, item) => sum + item.value, 0);

const enhancedChartData = chartData.map(item => ({
...item,
total
}));

const onPieEnter = (_: any, index: number) => {
setActiveIndex(index);
};
Expand All @@ -89,7 +107,7 @@ const EcoRegionDoughnutChart: React.FC<EcoRegionDoughnutChartProps> = ({ data })
<PieChart>
<Tooltip content={<CustomTooltip />} />
<Pie
data={chartData}
data={enhancedChartData}
cx="50%"
cy="50%"
innerRadius={100}
Expand Down
Loading