Skip to content

Commit

Permalink
fix: Padding improvement when no axes are shown (#774)
Browse files Browse the repository at this point in the history
* Added no axes logic
  • Loading branch information
severinlandolt authored Oct 24, 2023
1 parent 416c2ab commit 260efdb
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/components/chart-elements/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
...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 @@ -147,7 +148,6 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
}
: undefined
}
margin={{ left: 20, right: 20 }}
>
{showGridLines ? (
<CartesianGrid
Expand All @@ -164,7 +164,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>((props, ref)
/>
) : null}
<XAxis
padding={{ left: 20, right: 20 }}
padding={{ left: paddingValue, right: paddingValue }}
hide={!showXAxis}
dataKey={index}
tick={{ transform: "translate(0, 6)" }}
Expand Down
4 changes: 2 additions & 2 deletions src/components/chart-elements/BarChart/BarChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
...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 @@ -141,7 +142,6 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>
}
: undefined
}
margin={{ left: 20, right: 20 }}
>
{showGridLines ? (
<CartesianGrid
Expand All @@ -160,7 +160,7 @@ const BarChart = React.forwardRef<HTMLDivElement, BarChartProps>((props, ref) =>

{layout !== "vertical" ? (
<XAxis
padding={{ left: 20, right: 20 }}
padding={{ left: paddingValue, right: paddingValue }}
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 @@ -75,6 +75,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
...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 @@ -143,7 +144,6 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
}
: undefined
}
margin={{ left: 20, right: 20 }}
>
{showGridLines ? (
<CartesianGrid
Expand All @@ -160,7 +160,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>((props, ref)
/>
) : null}
<XAxis
padding={{ left: 20, right: 20 }}
padding={{ left: paddingValue, right: paddingValue }}
hide={!showXAxis}
dataKey={index}
interval={startEndOnly ? "preserveStartEnd" : intervalType}
Expand Down
11 changes: 11 additions & 0 deletions src/stories/chart-elements/AreaChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,17 @@ export const WithoutLegendScroll: Story = {
args: { enableLegendSlider: false },
};

export const NoAxes: Story = {
args: {
showXAxis: false,
showYAxis: false,
},
};

export const NoYAxisStartEndOnly: Story = {
args: { showYAxis: false, startEndOnly: true },
};

//Custom tooltips
const customTooltipColors: Color[] = ["cyan"];
const customTooltipIndex = "month";
Expand Down
8 changes: 8 additions & 0 deletions src/stories/chart-elements/BarChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,14 @@ export const RotateXLabelVertical: Story = {
},
};

export const NoAxes: Story = {
args: { showXAxis: false, showYAxis: false },
};

export const NoYAxisStartEndOnly: Story = {
args: { showYAxis: false, startEndOnly: true },
};

//Custom tooltips
const customTooltipColors: Color[] = ["cyan"];
const customTooltipIndex = "month";
Expand Down
8 changes: 8 additions & 0 deletions src/stories/chart-elements/LineChart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,14 @@ export const LongIndexNameAndPreserveStartEnd: Story = {
args: { data: longIndexBaseChartData, intervalType: "preserveStartEnd" },
};

export const NoAxes: Story = {
args: { showXAxis: false, showYAxis: false },
};

export const NoYAxisStartEndOnly: Story = {
args: { showYAxis: false, startEndOnly: true },
};

export const RotateXLabels: Story = {
args: {
data: longBaseChartData,
Expand Down

0 comments on commit 260efdb

Please sign in to comment.