Skip to content

Commit

Permalink
feat: Legend position (#28)
Browse files Browse the repository at this point in the history
* add legend position to areachart

* Add legend left padding
  • Loading branch information
severinlandolt committed May 31, 2024
1 parent 9c2977e commit dc37ad8
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 3 deletions.
20 changes: 19 additions & 1 deletion src/components/AreaChart/AreaChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,8 @@ const ChartLegend = (
activeLegend: string | undefined,
onClick?: (category: string, color: string) => void,
enableLegendSlider?: boolean,
legendPosition?: "left" | "center" | "right",
yAxisWidth?: number,
) => {
const legendRef = React.useRef<HTMLDivElement>(null)

Expand All @@ -329,8 +331,20 @@ const ChartLegend = (

const filteredPayload = payload.filter((item: any) => item.type !== "none")

const paddingLeft =
legendPosition === "left" && yAxisWidth ? yAxisWidth - 8 : 0

return (
<div ref={legendRef} className="flex items-center justify-end">
<div
ref={legendRef}
style={{ paddingLeft: paddingLeft }}
className={cx(
"flex items-center",
{ "justify-center": legendPosition === "center" },
{ "justify-start": legendPosition === "left" },
{ "justify-end": legendPosition === "right" },
)}
>
<Legend
categories={filteredPayload.map((entry: any) => entry.value)}
colors={filteredPayload.map((entry: any) =>
Expand Down Expand Up @@ -494,6 +508,7 @@ interface AreaChartProps extends React.HTMLAttributes<HTMLDivElement> {
xAxisLabel?: string
yAxisLabel?: string
type?: "default" | "stacked" | "percent"
legendPosition?: "left" | "center" | "right"
}

const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>(
Expand Down Expand Up @@ -524,6 +539,7 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>(
xAxisLabel,
yAxisLabel,
type = "default",
legendPosition = "right",
...other
} = props
const paddingValue = !showXAxis && !showYAxis ? 0 : 20
Expand Down Expand Up @@ -724,6 +740,8 @@ const AreaChart = React.forwardRef<HTMLDivElement, AreaChartProps>(
onCategoryClick(clickedLegendItem)
: undefined,
enableLegendSlider,
legendPosition,
yAxisWidth,
)
}
/>
Expand Down
12 changes: 12 additions & 0 deletions src/components/AreaChart/areachart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,18 @@ export const AllColors: Story = {
},
}

export const WithLegendLeft: Story = {
args: {
legendPosition: "left",
},
}

export const WithLegendCenter: Story = {
args: {
legendPosition: "center",
},
}

export const WithLegendSlider: Story = {
args: {
className: "max-w-md",
Expand Down
20 changes: 19 additions & 1 deletion src/components/LineChart/LineChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,8 @@ const ChartLegend = (
activeLegend: string | undefined,
onClick?: (category: string, color: string) => void,
enableLegendSlider?: boolean,
legendPosition?: "left" | "center" | "right",
yAxisWidth?: number,
) => {
const legendRef = React.useRef<HTMLDivElement>(null)

Expand All @@ -328,8 +330,20 @@ const ChartLegend = (

const filteredPayload = payload.filter((item: any) => item.type !== "none")

const paddingLeft =
legendPosition === "left" && yAxisWidth ? yAxisWidth - 8 : 0

return (
<div ref={legendRef} className="flex items-center justify-end">
<div
ref={legendRef}
style={{ paddingLeft: paddingLeft }}
className={cx(
"flex items-center",
{ "justify-center": legendPosition === "center" },
{ "justify-start": legendPosition === "left" },
{ "justify-end": legendPosition === "right" },
)}
>
<Legend
categories={filteredPayload.map((entry: any) => entry.value)}
colors={filteredPayload.map((entry: any) =>
Expand Down Expand Up @@ -492,6 +506,7 @@ interface LineChartProps extends React.HTMLAttributes<HTMLDivElement> {
connectNulls?: boolean
xAxisLabel?: string
yAxisLabel?: string
legendPosition?: "left" | "center" | "right"
}

const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>(
Expand Down Expand Up @@ -521,6 +536,7 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>(
tickGap = 5,
xAxisLabel,
yAxisLabel,
legendPosition = "right",
...other
} = props
const paddingValue = !showXAxis && !showYAxis ? 0 : 20
Expand Down Expand Up @@ -714,6 +730,8 @@ const LineChart = React.forwardRef<HTMLDivElement, LineChartProps>(
onCategoryClick(clickedLegendItem)
: undefined,
enableLegendSlider,
legendPosition,
yAxisWidth,
)
}
/>
Expand Down
18 changes: 17 additions & 1 deletion src/components/LineChart/linechart.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,11 @@ const chartdata = [
const meta: Meta<typeof LineChart> = {
title: "visualization/LineChart",
component: LineChart,
args: { data: chartdata, index: "date", categories: ["SolarCells", "Glass"] },
args: {
data: chartdata,
index: "date",
categories: ["SolarCells", "Glass"],
},
}

export default meta
Expand Down Expand Up @@ -193,6 +197,18 @@ export const AllColors: Story = {
},
}

export const WithLegendLeft: Story = {
args: {
legendPosition: "left",
},
}

export const WithLegendCenter: Story = {
args: {
legendPosition: "center",
},
}

export const WithLegendSlider: Story = {
args: {
className: "max-w-md",
Expand Down

0 comments on commit dc37ad8

Please sign in to comment.