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: Chart padding prop #862

Merged
merged 3 commits into from
Aug 28, 2024
Merged
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
6 changes: 4 additions & 2 deletions src/components/chart-elements/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,13 +78,15 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
enableLegendSlider = false,
customTooltip,
rotateLabelX,
padding = (!showXAxis && !showYAxis) || (startEndOnly && !showYAxis)
? { left: 0, right: 0 }
: { left: 20, right: 20 },
tickGap = 5,
xAxisLabel,
yAxisLabel,
...other
} = props;
const CustomTooltip = customTooltip;
const paddingValue = (!showXAxis && !showYAxis) || (startEndOnly && !showYAxis) ? 0 : 20;
const [legendHeight, setLegendHeight] = useState(60);
const [activeDot, setActiveDot] = useState<ActiveDot | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
Expand Down Expand Up @@ -174,7 +176,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
/>
) : null}
<XAxis
padding={{ left: paddingValue, right: paddingValue }}
padding={padding}
hide={!showXAxis}
dataKey={index}
tick={{ transform: "translate(0, 6)" }}
Expand Down
5 changes: 3 additions & 2 deletions src/components/chart-elements/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,11 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
xAxisLabel,
yAxisLabel,
className,
enableLegendSlider = false,
padding = !showXAxis && !showYAxis ? { left: 0, right: 0 } : { left: 20, right: 20 },
...other
} = props;
const CustomTooltip = customTooltip;
const paddingValue = !showXAxis && !showYAxis ? 0 : 20;
const [legendHeight, setLegendHeight] = useState(60);
const categoryColors = constructCategoryColors(categories, colors);
const [activeBar, setActiveBar] = React.useState<any | undefined>(undefined);
Expand Down Expand Up @@ -187,7 +188,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>

{layout !== "vertical" ? (
<XAxis
padding={{ left: paddingValue, right: paddingValue }}
padding={padding}
hide={!showXAxis}
dataKey={index}
interval={startEndOnly ? "preserveStartEnd" : intervalType}
Expand Down
4 changes: 2 additions & 2 deletions src/components/chart-elements/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,13 +73,13 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
enableLegendSlider = false,
customTooltip,
rotateLabelX,
padding = !showXAxis && !showYAxis ? { left: 0, right: 0 } : { left: 20, right: 20 },
tickGap = 5,
xAxisLabel,
yAxisLabel,
...other
} = props;
const CustomTooltip = customTooltip;
const paddingValue = !showXAxis && !showYAxis ? 0 : 20;
const [legendHeight, setLegendHeight] = useState(60);
const [activeDot, setActiveDot] = useState<ActiveDot | undefined>(undefined);
const [activeLegend, setActiveLegend] = useState<string | undefined>(undefined);
Expand Down Expand Up @@ -170,7 +170,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
/>
) : null}
<XAxis
padding={{ left: paddingValue, right: paddingValue }}
padding={padding}
hide={!showXAxis}
dataKey={index}
interval={startEndOnly ? "preserveStartEnd" : intervalType}
Expand Down
1 change: 1 addition & 0 deletions src/components/chart-elements/common/BaseChartProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ interface BaseChartProps extends BaseAnimationTimingProps, React.HTMLAttributes<
noDataText?: string;
onValueChange?: (value: EventProps) => void;
enableLegendSlider?: boolean;
padding?: { left?: number; right?: number };
customTooltip?: React.ComponentType<CustomTooltipProps>;
rotateLabelX?: {
angle: number;
Expand Down
41 changes: 41 additions & 0 deletions src/stories/chart-elements/AreaChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -351,6 +351,46 @@
},
};

export const Padding: Story = {
args: {
padding: { left: 0, right: 0 },
},
};

// keep because of if statement
// const ResponsiveTemplate: ComponentStory<typeof AreaChart> = (args) => {
// if (args.onValueChange?.length === 0) {
// args.onValueChange = undefined;
// }

// return (
// <>
// <Title>Desktop</Title>
// <Card>
// <AreaChart {...args} yAxisWidth={60} />
// </Card>
// <Title className="mt-5">Mobile</Title>
// <div className="w-64">
// <Card>
// <AreaChart {...args} yAxisWidth={60} />
// </Card>
// </div>
// </>
// );
// };

// const DefaultTemplate: ComponentStory<typeof AreaChart> = ({ ...args }) => {
// if (args.onValueChange?.length === 0) {
// args.onValueChange = undefined;
// }

// return (
// <Card>
// <AreaChart {...args} yAxisWidth={60} />
// </Card>
// );
//

export const tickGap: Story = {
args: {
data: longBaseChartData,
Expand All @@ -364,3 +404,4 @@
yAxisLabel: "Amount (USD)",
},
};

Check failure on line 407 in src/stories/chart-elements/AreaChart.stories.tsx

View workflow job for this annotation

GitHub Actions / Build dist

Delete `⏎`
5 changes: 5 additions & 0 deletions src/stories/chart-elements/BarChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,11 @@ export const CustomTooltipComplex: Story = {
},
};

export const Padding: Story = {
args: {
padding: { left: 0, right: 0 },
},
};
export const tickGap: Story = {
args: {
data: longBaseChartData,
Expand Down
5 changes: 5 additions & 0 deletions src/stories/chart-elements/LineChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,11 @@ export const CustomTooltipComplex: Story = {
},
};

export const Padding: Story = {
args: {
padding: { left: 0, right: 0 },
},
};
export const tickGap: Story = {
args: {
data: longBaseChartData,
Expand Down
Loading