Skip to content

Commit 2568874

Browse files
feat - allow passing label as prop
1 parent 0a6c9a0 commit 2568874

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
7777
onValueChange,
7878
enableLegendSlider = false,
7979
customTooltip,
80+
renderLabel,
8081
rotateLabelX,
8182
tickGap = 5,
8283
xAxisLabel,
@@ -428,6 +429,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
428429
animationDuration={animationDuration}
429430
stackId={stack ? "a" : undefined}
430431
connectNulls={connectNulls}
432+
label={renderLabel}
431433
/>
432434
))}
433435
{onValueChange

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
9292
onValueChange,
9393
enableLegendSlider = false,
9494
customTooltip,
95+
renderLabel,
9596
rotateLabelX,
9697
barCategoryGap,
9798
tickGap = 5,
@@ -395,6 +396,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
395396
animationDuration={animationDuration}
396397
shape={(props: any) => renderShape(props, activeBar, activeLegend, layout)}
397398
onClick={onBarClick}
399+
label={renderLabel}
398400
/>
399401
))}
400402
</ReChartsBarChart>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
7272
onValueChange,
7373
enableLegendSlider = false,
7474
customTooltip,
75+
renderLabel,
7576
rotateLabelX,
7677
tickGap = 5,
7778
xAxisLabel,
@@ -372,6 +373,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
372373
isAnimationActive={showAnimation}
373374
animationDuration={animationDuration}
374375
connectNulls={connectNulls}
376+
label={renderLabel}
375377
/>
376378
))}
377379
{onValueChange
@@ -394,6 +396,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
394396
const { name } = props;
395397
onCategoryClick(name);
396398
}}
399+
label={renderLabel}
397400
/>
398401
))
399402
: 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";
@@ -43,6 +44,7 @@ interface BaseChartProps extends BaseAnimationTimingProps, React.HTMLAttributes<
4344
tickGap?: number;
4445
xAxisLabel?: string;
4546
yAxisLabel?: string;
47+
renderLabel?: ImplicitLabelType;
4648
}
4749

4850
export default BaseChartProps;

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,3 +364,29 @@ export const AxisLabels: Story = {
364364
yAxisLabel: "Amount (USD)",
365365
},
366366
};
367+
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+
};

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -387,3 +387,29 @@ export const AxisLabels: Story = {
387387
yAxisLabel: "Amount (USD)",
388388
},
389389
};
390+
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+
};

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,3 +324,29 @@ export const AxisLabels: Story = {
324324
yAxisLabel: "Amount (USD)",
325325
},
326326
};
327+
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+
};

0 commit comments

Comments
 (0)