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

feat: add donut chart legend #982

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
35 changes: 35 additions & 0 deletions src/components/chart-elements/DonutChart/DonutChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import type { EventProps } from "components/chart-elements/common";
import { CustomTooltipProps } from "components/chart-elements/common/CustomTooltipProps";
import type BaseAnimationTimingProps from "../common/BaseAnimationTimingProps";
import { Legend } from "components/text-elements";

type DonutChartVariant = "donut" | "pie";

Expand All @@ -36,6 +37,7 @@
className?: string;
onValueChange?: (value: EventProps) => void;
customTooltip?: React.ComponentType<CustomTooltipProps>;
activeSector?: string | null | undefined;
}

const renderInactiveShape = (props: any) => {
Expand Down Expand Up @@ -90,6 +92,7 @@
onValueChange,
customTooltip,
className,
activeSector = undefined,
...other
} = props;
const CustomTooltip = customTooltip;
Expand All @@ -98,6 +101,7 @@

const [activeIndex, setActiveIndex] = React.useState<number | undefined>(undefined);
const hasOnValueChange = !!onValueChange;
const legendLabels = data.map((v) => v[index]);

function onShapeClick(data: any, index: number, event: React.MouseEvent) {
event.stopPropagation();
Expand All @@ -115,6 +119,15 @@
}
}

useEffect(() => {
if (!activeSector) {
return setActiveIndex(undefined);
}

const idx = data.findIndex((v) => v[index] === activeSector) ?? undefined;
setActiveIndex(idx);
}, [activeSector, data]);

Check warning on line 129 in src/components/chart-elements/DonutChart/DonutChart.tsx

View workflow job for this annotation

GitHub Actions / Build dist

React Hook useEffect has a missing dependency: 'index'. Either include it or remove the dependency array

Check warning on line 129 in src/components/chart-elements/DonutChart/DonutChart.tsx

View workflow job for this annotation

GitHub Actions / Build dist

React Hook useEffect has a missing dependency: 'index'. Either include it or remove the dependency array

Check warning on line 129 in src/components/chart-elements/DonutChart/DonutChart.tsx

View workflow job for this annotation

GitHub Actions / Build dist

React Hook useEffect has a missing dependency: 'index'. Either include it or remove the dependency array

useEffect(() => {
const pieSectors = document.querySelectorAll(".recharts-pie-sector");
if (pieSectors) {
Expand All @@ -126,6 +139,28 @@

return (
<div ref={ref} className={tremorTwMerge("w-full h-40", className)} {...other}>
{activeSector !== undefined ? (
<>
<div className="flex justify-center">
<Legend
className="my-3"
categories={legendLabels}
colors={colors}
onClickLegendItem={(category) => {
const idx = data.findIndex((v) => v[index] === category) ?? undefined;

if (idx === activeIndex) {
setActiveIndex(undefined);
return;
}

setActiveIndex(idx);
}}
/>
</div>
<div className="my-4" />
</>
) : null}
<ResponsiveContainer className="h-full w-full">
{data?.length ? (
<ReChartsDonutChart
Expand Down
9 changes: 9 additions & 0 deletions src/stories/chart-elements/DonutChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -171,3 +171,12 @@ export const CustomTooltipSimple: Story = {
},
},
};

export const ActiveValue: Story = {
args: { activeSector: "New York" },
};

export const ActiveValueNoDefaultValue: Story = {
// Setting activeValue to null shows legend without preselcted value
args: { activeSector: null },
};
Loading