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: Legend position #28

Merged
merged 9 commits into from
May 31, 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
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