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

[Feature]: BarChart - Allow stackId to be specified for "mixed" bar charts #997

Open
BenJenkinson opened this issue Mar 24, 2024 · 1 comment

Comments

@BenJenkinson
Copy link

BenJenkinson commented Mar 24, 2024

What problem does this feature solve?

I want to display a "mixed" bar chart; where some bars within a category are stacked and others are either not, or are stacked separately.

image

This is possible in recharts, the library that tremor is using to display charts, using the stackId property:

In tremor, this line currently sets all bars to use the same stack:

stackId={stack || relative ? "a" : undefined}

What does the proposed API look like?

Suggestion 1

With this addition, there are really several different props taken by tremor related to categories, that are passed to the recharts <Bar> component.

You could consider changing the categories prop from string[] to some kind of Category[] object:

const data = [
  {
    name: "House #1",
    "2022-water": 50,
    "2022-electricity": 100,
    "2023-water": 65,
    "2023-electricity": 140,
  },
  {
    name: "House #2",
    "2022-water": 30,
    "2022-electricity": 100,
    "2023-water": 15,
    "2023-electricity": 85,
  },
];

export default function Example() {
  return (
    <BarChart
      data={data}
      categories={[
        { dataKey: "2022-water", color: "blue", stackId: "2022" },
        { dataKey: "2022-electricity", color: "yellow", stackId: "2023" },
        { dataKey: "2023-water", color: "blue", stackId: "2022" },
        { dataKey: "2023-electricity", color: "yellow", stackId: "2023" },
      ]}
    />
  );
}

Suggestion 2

To avoid changing the type of the categories prop to be some kind of Category[] rather than string[], you could copy the pattern followed by the categories and colors props:

const data = [
  {
    name: "House #1",
    "2022-water": 50,
    "2022-electricity": 100,
    "2023-water": 65,
    "2023-electricity": 140,
  },
  {
    name: "House #2",
    "2022-water": 30,
    "2022-electricity": 100,
    "2023-water": 15,
    "2023-electricity": 85,
  },
];

export default function Example() {
  return (
    <BarChart
      data={data}
      categories={[
        "2022-water",
        "2022-electricity",
        "2023-water",
        "2023-electricity",
      ]}
      colors={["blue", "yellow", "blue", "yellow"]}
      stacks={["2022", "2022", "2023", "2023"]}
    />
  );
}
@ojuan19
Copy link

ojuan19 commented May 24, 2024

This will be a great improvement to the BarChart component, any news here?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants