Skip to content

Commit 21445ec

Browse files
Feat/allow passing label as prop (#1)
* feat - allow passing label as prop * add donut
1 parent a641abb commit 21445ec

File tree

9 files changed

+144
-0
lines changed

9 files changed

+144
-0
lines changed

src/components/chart-elements/AreaChart/AreaChart.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
7878
onValueChange,
7979
enableLegendSlider = false,
8080
customTooltip,
81+
renderLabel,
8182
rotateLabelX,
8283
tickGap = 5,
8384
xAxisLabel,
@@ -441,6 +442,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
441442
animationDuration={animationDuration}
442443
stackId={stack ? "a" : undefined}
443444
connectNulls={connectNulls}
445+
label={renderLabel}
444446
/>
445447
))}
446448
{onValueChange

src/components/chart-elements/BarChart/BarChart.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
9393
onValueChange,
9494
enableLegendSlider = false,
9595
customTooltip,
96+
renderLabel,
9697
rotateLabelX,
9798
barCategoryGap,
9899
tickGap = 5,
@@ -420,6 +421,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
420421
animationDuration={animationDuration}
421422
shape={(props: any) => renderShape(props, activeBar, activeLegend, layout)}
422423
onClick={onBarClick}
424+
label={renderLabel}
423425
/>
424426
))}
425427
</ReChartsBarChart>

src/components/chart-elements/DonutChart/DonutChart.tsx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import { parseData, parseLabelInput } from "./inputParser";
1818
import type { EventProps } from "components/chart-elements/common";
1919
import { CustomTooltipProps } from "components/chart-elements/common/CustomTooltipProps";
2020
import type BaseAnimationTimingProps from "../common/BaseAnimationTimingProps";
21+
import { PieLabel } from "recharts/types/polar/Pie";
2122

2223
type DonutChartVariant = "donut" | "pie";
2324

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

4143
const renderInactiveShape = (props: any) => {
@@ -89,6 +91,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
8991
noDataText,
9092
onValueChange,
9193
customTooltip,
94+
renderLabel,
9295
className,
9396
...other
9497
} = props;
@@ -177,6 +180,7 @@ const DonutChart = React.forwardRef<HTMLDivElement, DonutChartProps>((props, ref
177180
activeIndex={activeIndex}
178181
inactiveShape={renderInactiveShape}
179182
style={{ outline: "none" }}
183+
label={renderLabel}
180184
/>
181185
{/* {showTooltip ? (
182186
<Tooltip

src/components/chart-elements/LineChart/LineChart.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
7373
onValueChange,
7474
enableLegendSlider = false,
7575
customTooltip,
76+
renderLabel,
7677
rotateLabelX,
7778
tickGap = 5,
7879
xAxisLabel,
@@ -385,6 +386,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
385386
isAnimationActive={showAnimation}
386387
animationDuration={animationDuration}
387388
connectNulls={connectNulls}
389+
label={renderLabel}
388390
/>
389391
))}
390392
{onValueChange
@@ -407,6 +409,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
407409
const { name } = props;
408410
onCategoryClick(name);
409411
}}
412+
label={renderLabel}
410413
/>
411414
))
412415
: null}

src/components/chart-elements/common/BaseChartProps.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { ImplicitLabelType } from "recharts/types/component/Label";
12
import { Color, ValueFormatter, IntervalType } from "../../../lib";
23
import type BaseAnimationTimingProps from "./BaseAnimationTimingProps";
34
import { CustomTooltipProps } from "./CustomTooltipProps";
@@ -44,6 +45,7 @@ interface BaseChartProps extends BaseAnimationTimingProps, React.HTMLAttributes<
4445
tickGap?: number;
4546
xAxisLabel?: string;
4647
yAxisLabel?: string;
48+
renderLabel?: ImplicitLabelType;
4749
}
4850

4951
export default BaseChartProps;

src/stories/chart-elements/AreaChart.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,32 @@ export const AxisLabels: Story = {
365365
},
366366
};
367367

368+
export const DataLabelsSimple: Story = {
369+
args: {
370+
renderLabel: true,
371+
},
372+
};
373+
374+
export const DataLabelsObject: Story = {
375+
args: {
376+
renderLabel: { fill: "white", fontSize: 20, position: "top" },
377+
},
378+
};
379+
380+
const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
381+
return (
382+
<text x={x} y={y} fill="white" textAnchor="end" dominantBaseline="central">
383+
{value}
384+
</text>
385+
);
386+
};
387+
388+
export const DataLabelsFunction: Story = {
389+
args: {
390+
renderLabel: renderCustomizedLabel,
391+
},
392+
}
393+
368394
export const ShowAxisLine: Story = {
369395
args: {
370396
showAxisLine: true,

src/stories/chart-elements/BarChart.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,32 @@ export const AxisLabels: Story = {
388388
},
389389
};
390390

391+
export const DataLabelsSimple: Story = {
392+
args: {
393+
renderLabel: true,
394+
},
395+
};
396+
397+
export const DataLabelsObject: Story = {
398+
args: {
399+
renderLabel: { fill: "white", fontSize: 20, position: "insideTop" },
400+
},
401+
};
402+
403+
const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
404+
return (
405+
<text x={x} y={y} fill="black" textAnchor="end" dominantBaseline="central">
406+
{value}
407+
</text>
408+
);
409+
};
410+
411+
export const DataLabelsFunction: Story = {
412+
args: {
413+
renderLabel: renderCustomizedLabel,
414+
},
415+
}
416+
391417
export const ShowAxisLine: Story = {
392418
args: {
393419
showAxisLine: true,

src/stories/chart-elements/DonutChart.stories.tsx

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,3 +171,56 @@ export const CustomTooltipSimple: Story = {
171171
},
172172
},
173173
};
174+
175+
export const DataLabelsSimple: Story = {
176+
args: {
177+
renderLabel: true,
178+
},
179+
};
180+
181+
export const DataLabelsObject: Story = {
182+
args: {
183+
renderLabel: { fill: "red", fontSize: 20 },
184+
},
185+
};
186+
187+
const RADIAN = Math.PI / 180;
188+
const renderCustomizedLabel = ({
189+
cx,
190+
cy,
191+
midAngle,
192+
innerRadius,
193+
outerRadius,
194+
percent,
195+
}: {
196+
cx: number;
197+
cy: number;
198+
midAngle: number;
199+
innerRadius: number;
200+
outerRadius: number;
201+
percent: number;
202+
}) => {
203+
const radius = innerRadius + 10 + (outerRadius - innerRadius) * 0.6;
204+
const x = cx + radius * Math.cos(-midAngle * RADIAN);
205+
const y = cy + radius * Math.sin(-midAngle * RADIAN);
206+
207+
return (
208+
<text
209+
x={x}
210+
y={y}
211+
fill="white"
212+
fontSize="9"
213+
textAnchor={x > cx ? "start" : "end"}
214+
dominantBaseline="auto"
215+
>
216+
{`${(percent * 100).toFixed(0)}%`}
217+
</text>
218+
);
219+
};
220+
221+
export const DataLabelsFunction: Story = {
222+
args: {
223+
renderLabel: renderCustomizedLabel,
224+
variant: "pie",
225+
},
226+
};

src/stories/chart-elements/LineChart.stories.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -325,6 +325,32 @@ export const AxisLabels: Story = {
325325
},
326326
};
327327

328+
export const DataLabelsSimple: Story = {
329+
args: {
330+
renderLabel: true,
331+
},
332+
};
333+
334+
export const DataLabelsObject: Story = {
335+
args: {
336+
renderLabel: { fill: "white", fontSize: 20, position: "top" },
337+
},
338+
};
339+
340+
const renderCustomizedLabel = ({ x, y, value }: { x: number; y: number; value: number }) => {
341+
return (
342+
<text x={x} y={y} fill="white" textAnchor="end" dominantBaseline="central">
343+
{value}
344+
</text>
345+
);
346+
};
347+
348+
export const DataLabelsFunction: Story = {
349+
args: {
350+
renderLabel: renderCustomizedLabel,
351+
},
352+
}
353+
328354
export const ShowAxisLine: Story = {
329355
args: {
330356
showAxisLine: true,

0 commit comments

Comments
 (0)