自定义每个栏的Rechart条形图标签和颜色

发布于 2025-01-26 05:15:52 字数 1546 浏览 2 评论 0 原文

我有一个Rechart条形图,我想拥有这样的标签...

我希望颜色像这样...

问题是我似乎不能同时获得它们。第二张图像是我可以获得的最接近的图像,但它带来了值,而不是名称,因此int(85)而不是字符串(85%),也不是角度。

这是我的代码。我评论了允许倾斜标签的部分,因为它在那里时无法正常工作。.TSX

import { BarChart, Bar, LabelList, Cell } from "recharts";
import { colors } from "../../Colors/colors";

const data = [
  {
    name: `${85}%`,
    uv: 85,
  },
  {
    name: `${95}%`,
    uv: 95,
  },
  {
    name: `${80}%`,
    uv: 80,
  },
];
export default function MyBarChart(): JSX.Element {
  return (
    <BarChart width={150} height={400} data={data}>
      <Bar
        dataKey="uv"
        fill={colors.blueTheme[0]}
        radius={8}
        label={{ position: "insideBottom", fill: "black" }}
        name="name"
      >
        {/* <LabelList
          dataKey="name"
          position="insideBottom"
          angle={270}
          offset={25}
        /> */}
        {colors.blueTheme.map((entry, index) => (
          <Cell key={`cell-${index}`} fill={colors.blueTheme[index % 20]} />
        ))}
      </Bar>
    </BarChart>
  );
}

如何

export const colors = {
  blueTheme: ["#85A5FF", "#ADC6FF", "#D6E4FF"],
};

同时获得具有颜色差异的角度标签?

I have a rechart bar chart that I would like to have the Labels like this...
enter image description here

And I want the colors to be like this...
enter image description here

The problem is that I can't seem to get them both at the same time. The second image is the closest I can get but it is bringing in the value instead of the name hence the int(85) instead of string(85%) and it is also not angles.

Here is my code. I commented out the part that allows for angled labels because it wont work when is there..

import { BarChart, Bar, LabelList, Cell } from "recharts";
import { colors } from "../../Colors/colors";

const data = [
  {
    name: `${85}%`,
    uv: 85,
  },
  {
    name: `${95}%`,
    uv: 95,
  },
  {
    name: `${80}%`,
    uv: 80,
  },
];
export default function MyBarChart(): JSX.Element {
  return (
    <BarChart width={150} height={400} data={data}>
      <Bar
        dataKey="uv"
        fill={colors.blueTheme[0]}
        radius={8}
        label={{ position: "insideBottom", fill: "black" }}
        name="name"
      >
        {/* <LabelList
          dataKey="name"
          position="insideBottom"
          angle={270}
          offset={25}
        /> */}
        {colors.blueTheme.map((entry, index) => (
          <Cell key={`cell-${index}`} fill={colors.blueTheme[index % 20]} />
        ))}
      </Bar>
    </BarChart>
  );
}

The colors.tsx

export const colors = {
  blueTheme: ["#85A5FF", "#ADC6FF", "#D6E4FF"],
};

How can I get the angled labels with the color differences simultaneously?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

浅紫色的梦幻 2025-02-02 05:15:52

我发现了...

<BarChart width={150} height={400} data={data}>
  <Bar dataKey="uv" radius={8}>
    <LabelList
      dataKey="name"
      position="insideBottom"
      angle={270}
      offset={25}
      fill="black"
    />
    {colors.blueTheme.map((entry, index) => (
      <Cell key={`cell-${index}`} fill={colors.blueTheme[index % 20]} />
    ))}
  </Bar>
</BarChart>

I figured it out...

<BarChart width={150} height={400} data={data}>
  <Bar dataKey="uv" radius={8}>
    <LabelList
      dataKey="name"
      position="insideBottom"
      angle={270}
      offset={25}
      fill="black"
    />
    {colors.blueTheme.map((entry, index) => (
      <Cell key={`cell-${index}`} fill={colors.blueTheme[index % 20]} />
    ))}
  </Bar>
</BarChart>
太傻旳人生 2025-02-02 05:15:52

如果您更新数据并添加每个条形所需的颜色,则如下:

  const data = [
  {
    name: `${85}%`,
    uv: 85,
    color: "#85A5FF" // you may pass a value directly as string
  },
  {
    name: `${95}%`,
    uv: 95,
    color: colors.blueTheme[1]  // or colors.blueTheme by index
  },
  {
    name: `${80}%`,
    uv: 80,
    color: colors.blueTheme[getIndex()]  // or some custom logic to evaluate index
  },
];

您可以按以下方式更新代码,它将起作用:

export default function MyBarChart(): JSX.Element {
  return (
    <BarChart width={300} height={400} data={data}>
    <Bar
      dataKey="uv"
      fill="#85A5FF"
      radius={8}
      label={{
        position: "insideBottom",
        angle: -60,
        fill: "black",
        offset: 25
      }}
    >
      <LabelList dataKey="name" />
      {data.map((entry, index) => (
        <Cell fill={entry.color} key={`cell-${index}`} />
      ))}
    </Bar>
  </BarChart>
  );
}

工作示例:

If you update the data and add the color each bar needs to have, as follows:

  const data = [
  {
    name: `${85}%`,
    uv: 85,
    color: "#85A5FF" // you may pass a value directly as string
  },
  {
    name: `${95}%`,
    uv: 95,
    color: colors.blueTheme[1]  // or colors.blueTheme by index
  },
  {
    name: `${80}%`,
    uv: 80,
    color: colors.blueTheme[getIndex()]  // or some custom logic to evaluate index
  },
];

You can update the code as follows and it will work:

export default function MyBarChart(): JSX.Element {
  return (
    <BarChart width={300} height={400} data={data}>
    <Bar
      dataKey="uv"
      fill="#85A5FF"
      radius={8}
      label={{
        position: "insideBottom",
        angle: -60,
        fill: "black",
        offset: 25
      }}
    >
      <LabelList dataKey="name" />
      {data.map((entry, index) => (
        <Cell fill={entry.color} key={`cell-${index}`} />
      ))}
    </Bar>
  </BarChart>
  );
}

Working example:
https://codesandbox.io/embed/bar-chart-with-customized-event-forked-4ygf7i?fontsize=14&hidenavigation=1&theme=dark

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文