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: allow passing label as prop #1061

Open
wants to merge 2 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
2 changes: 2 additions & 0 deletions src/components/chart-elements/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
onValueChange,
enableLegendSlider = false,
customTooltip,
renderLabel,
rotateLabelX,
tickGap = 5,
xAxisLabel,
Expand Down Expand Up @@ -428,6 +429,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
animationDuration={animationDuration}
stackId={stack ? "a" : undefined}
connectNulls={connectNulls}
label={renderLabel}
/>
))}
{onValueChange
Expand Down
2 changes: 2 additions & 0 deletions src/components/chart-elements/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
onValueChange,
enableLegendSlider = false,
customTooltip,
renderLabel,
rotateLabelX,
barCategoryGap,
tickGap = 5,
Expand Down Expand Up @@ -395,6 +396,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
animationDuration={animationDuration}
shape={(props: any) => renderShape(props, activeBar, activeLegend, layout)}
onClick={onBarClick}
label={renderLabel}
/>
))}
</ReChartsBarChart>
Expand Down
4 changes: 4 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 { parseData, parseLabelInput } from "./inputParser";
import type { EventProps } from "components/chart-elements/common";
import { CustomTooltipProps } from "components/chart-elements/common/CustomTooltipProps";
import type BaseAnimationTimingProps from "../common/BaseAnimationTimingProps";
import { PieLabel } from "recharts/types/polar/Pie";

type DonutChartVariant = "donut" | "pie";

Expand All @@ -36,6 +37,7 @@ export interface DonutChartProps extends BaseAnimationTimingProps {
className?: string;
onValueChange?: (value: EventProps) => void;
customTooltip?: React.ComponentType<CustomTooltipProps>;
renderLabel?: PieLabel;
}

const renderInactiveShape = (props: any) => {
Expand Down Expand Up @@ -89,6 +91,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
noDataText,
onValueChange,
customTooltip,
renderLabel,
className,
...other
} = props;
Expand Down Expand Up @@ -177,6 +180,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
activeIndex={activeIndex}
inactiveShape={renderInactiveShape}
style={{ outline: "none" }}
label={renderLabel}
/>
{/* {showTooltip ? (
<Tooltip
Expand Down
3 changes: 3 additions & 0 deletions src/components/chart-elements/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
onValueChange,
enableLegendSlider = false,
customTooltip,
renderLabel,
rotateLabelX,
tickGap = 5,
xAxisLabel,
Expand Down Expand Up @@ -372,6 +373,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
isAnimationActive={showAnimation}
animationDuration={animationDuration}
connectNulls={connectNulls}
label={renderLabel}
/>
))}
{onValueChange
Expand All @@ -394,6 +396,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
const { name } = props;
onCategoryClick(name);
}}
label={renderLabel}
/>
))
: null}
Expand Down
2 changes: 2 additions & 0 deletions src/components/chart-elements/common/BaseChartProps.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ImplicitLabelType } from "recharts/types/component/Label";
import { Color, ValueFormatter, IntervalType } from "../../../lib";
import type BaseAnimationTimingProps from "./BaseAnimationTimingProps";
import { CustomTooltipProps } from "./CustomTooltipProps";
Expand Down Expand Up @@ -43,6 +44,7 @@ interface BaseChartProps extends BaseAnimationTimingProps, React.HTMLAttributes<
tickGap?: number;
xAxisLabel?: string;
yAxisLabel?: string;
renderLabel?: ImplicitLabelType;
}

export default BaseChartProps;
26 changes: 26 additions & 0 deletions src/stories/chart-elements/AreaChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -364,3 +364,29 @@ export const AxisLabels: Story = {
yAxisLabel: "Amount (USD)",
},
};

export const DataLabelsSimple: Story = {
args: {
renderLabel: true,
},
};

export const DataLabelsObject: Story = {
args: {
renderLabel: { fill: "white", fontSize: 20, position: "top" },
},
};

const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
return (
<text x={x} y={y} fill="white" textAnchor="end" dominantBaseline="central">
{value}
</text>
);
};

export const DataLabelsFunction: Story = {
args: {
renderLabel: renderCustomizedLabel,
},
};
26 changes: 26 additions & 0 deletions src/stories/chart-elements/BarChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,29 @@ export const AxisLabels: Story = {
yAxisLabel: "Amount (USD)",
},
};

export const DataLabelsSimple: Story = {
args: {
renderLabel: true,
},
};

export const DataLabelsObject: Story = {
args: {
renderLabel: { fill: "white", fontSize: 20, position: "insideTop" },
},
};

const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
return (
<text x={x} y={y} fill="black" textAnchor="end" dominantBaseline="central">
{value}
</text>
);
};

export const DataLabelsFunction: Story = {
args: {
renderLabel: renderCustomizedLabel,
},
};
53 changes: 53 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,56 @@ export const CustomTooltipSimple: Story = {
},
},
};

export const DataLabelsSimple: Story = {
args: {
renderLabel: true,
},
};

export const DataLabelsObject: Story = {
args: {
renderLabel: { fill: "red", fontSize: 20 },
},
};

const RADIAN = Math.PI / 180;
const renderCustomizedLabel = ({
cx,
cy,
midAngle,
innerRadius,
outerRadius,
percent,
}: {
cx: number;
cy: number;
midAngle: number;
innerRadius: number;
outerRadius: number;
percent: number;
}) => {
const radius = innerRadius + 10 + (outerRadius - innerRadius) * 0.6;
const x = cx + radius * Math.cos(-midAngle * RADIAN);
const y = cy + radius * Math.sin(-midAngle * RADIAN);

return (
<text
x={x}
y={y}
fill="white"
fontSize="9"
textAnchor={x > cx ? "start" : "end"}
dominantBaseline="auto"
>
{`${(percent * 100).toFixed(0)}%`}
</text>
);
};

export const DataLabelsFunction: Story = {
args: {
renderLabel: renderCustomizedLabel,
variant: "pie",
},
};
26 changes: 26 additions & 0 deletions src/stories/chart-elements/LineChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -324,3 +324,29 @@ export const AxisLabels: Story = {
yAxisLabel: "Amount (USD)",
},
};

export const DataLabelsSimple: Story = {
args: {
renderLabel: true,
},
};

export const DataLabelsObject: Story = {
args: {
renderLabel: { fill: "white", fontSize: 20, position: "top" },
},
};

const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
return (
<text x={x} y={y} fill="white" textAnchor="end" dominantBaseline="central">
{value}
</text>
);
};

export const DataLabelsFunction: Story = {
args: {
renderLabel: renderCustomizedLabel,
},
};
Loading