recharts- react中条形图中单个条的工具提示

发布于 2025-02-12 03:02:11 字数 3734 浏览 1 评论 0原文

因此,我有一个条形图有三个条,我想为每个条显示工具提示。目前,它将所有条形工具提示显示为一个。她是我的代码。

import React, { memo } from "react";
import {
  Bar,
  BarChart,
  CartesianGrid,
  LabelList,
  Rectangle,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
import { makeStyles } from "@material-ui/core/styles";

import ChartWrapper from "../chartParent/chartWrapper/ChartWrapper";

const Categories = [
  { category: "great", fill: "#00D063" },
  { category: "fair", fill: "#FFC135" },
  { category: "risky", fill: "#F44138" },
];
const useStyles = makeStyles((theme) => ({
  root: {
    margin: -30,
    width: 160,
    backgroundColor: "#fff",
    borderRadius: 5.5,
  },
  title: {
    fontFamily: "Open Sans Condensed",
    fontWeight: 600,
    fontSize: 18,
  },
  tooltipData: {
    width: "100%",
    height: 31,
    borderRadius: "5.5px 5.5px 0px 0px",
  },
  label: {
    color: "#fff",
    fontFamily: "Lato",
    fontWeight: 400,
    fontSize: 14,
    marginLeft: 18,
    paddingTop: "7px",
  },
  toolValue: {
    marginTop: 11,
    marginLeft: 18,
    fontFamily: "Roboto",
    fontWeight: 600,
    fontSize: 19,
  },
  signature: {
    fontWeight: 500,
    fontSize: 14,
    marginTop: -10,
    marginLeft: 18,
    paddingBottom: 11,
    paddingRight: 18,
    color: "#8D8D8D",
    fontFamily: "Lato",
  },
}));
const data = [
  {
    fair: 1,
    great: 1,
    risky: 1,
    week: "5.06 - 11.06",
  },
  {
    fair: 1,
    great: 0,
    risky: 1,
    week: "12.06 - 18.06",
  },

  {
    fair: 1,
    great: 0,
    risky: 1,
    week: "19.06 - 25.06",
  },
  {
    fair: 1,
    great: 1,
    risky: 0,
    week: "26.06 - 2.07",
  },
];

const Categories = [
  { category: "great", fill: "#00D063" },
  { category: "fair", fill: "#FFC135" },
  { category: "risky", fill: "#F44138" },
];
const PerformanceBreakdownChart = () => {
  const classes = useStyles();
  console.log("categories", Categories);

  return (
    <ChartWrapper {...props}>
      <ResponsiveContainer minHeight={150} width={"100%"} height="100%">
        <BarChart
          data={data}
          margin={{
            top: 30,
            right: 20,
            left: 20,
            bottom: 0,
          }}
          barSize={20}
        >
          <CartesianGrid horizontal={false} vertical={false} />
          <XAxis dataKey="week" axisLine={false} tickLine={false} />
          <YAxis hide={true} dy={4} />

          <Tooltip cursor={{ fill: "transparent" }} />
          {Categories.map(({ category, fill }) => (
            <Bar
              key={category}
              dataKey={category}
              fill={fill}
              isAnimationActive={false}
              shape={<Rectangle radius={[10, 10, 10, 10]} />}
              minPointSize={16}
            >
              <LabelList
                dataKey={category}
                position="insideBottomLeft"
                dy={11}
                offset="15"
                angle="-90"
                fill="#FFF"
              />
            </Bar>
          ))}
        </BarChart>
      </ResponsiveContainer>
    </ChartWrapper>
  );
};

export default memo(PerformanceBreakdownChart);

上面的代码显示所有条形的工具提示,如下所示

“在此处输入图像说明”

但是,我想显示每个单独的bar的工具提示,如下所示

我该怎么做才能实现这一目标?谢谢

So I have a bar chart having three bars and I wanted to show tooltip for each bar. Currently it shows all bars tooltip as one. Her is my code.

import React, { memo } from "react";
import {
  Bar,
  BarChart,
  CartesianGrid,
  LabelList,
  Rectangle,
  ResponsiveContainer,
  Tooltip,
  XAxis,
  YAxis,
} from "recharts";
import { makeStyles } from "@material-ui/core/styles";

import ChartWrapper from "../chartParent/chartWrapper/ChartWrapper";

const Categories = [
  { category: "great", fill: "#00D063" },
  { category: "fair", fill: "#FFC135" },
  { category: "risky", fill: "#F44138" },
];
const useStyles = makeStyles((theme) => ({
  root: {
    margin: -30,
    width: 160,
    backgroundColor: "#fff",
    borderRadius: 5.5,
  },
  title: {
    fontFamily: "Open Sans Condensed",
    fontWeight: 600,
    fontSize: 18,
  },
  tooltipData: {
    width: "100%",
    height: 31,
    borderRadius: "5.5px 5.5px 0px 0px",
  },
  label: {
    color: "#fff",
    fontFamily: "Lato",
    fontWeight: 400,
    fontSize: 14,
    marginLeft: 18,
    paddingTop: "7px",
  },
  toolValue: {
    marginTop: 11,
    marginLeft: 18,
    fontFamily: "Roboto",
    fontWeight: 600,
    fontSize: 19,
  },
  signature: {
    fontWeight: 500,
    fontSize: 14,
    marginTop: -10,
    marginLeft: 18,
    paddingBottom: 11,
    paddingRight: 18,
    color: "#8D8D8D",
    fontFamily: "Lato",
  },
}));
const data = [
  {
    fair: 1,
    great: 1,
    risky: 1,
    week: "5.06 - 11.06",
  },
  {
    fair: 1,
    great: 0,
    risky: 1,
    week: "12.06 - 18.06",
  },

  {
    fair: 1,
    great: 0,
    risky: 1,
    week: "19.06 - 25.06",
  },
  {
    fair: 1,
    great: 1,
    risky: 0,
    week: "26.06 - 2.07",
  },
];

const Categories = [
  { category: "great", fill: "#00D063" },
  { category: "fair", fill: "#FFC135" },
  { category: "risky", fill: "#F44138" },
];
const PerformanceBreakdownChart = () => {
  const classes = useStyles();
  console.log("categories", Categories);

  return (
    <ChartWrapper {...props}>
      <ResponsiveContainer minHeight={150} width={"100%"} height="100%">
        <BarChart
          data={data}
          margin={{
            top: 30,
            right: 20,
            left: 20,
            bottom: 0,
          }}
          barSize={20}
        >
          <CartesianGrid horizontal={false} vertical={false} />
          <XAxis dataKey="week" axisLine={false} tickLine={false} />
          <YAxis hide={true} dy={4} />

          <Tooltip cursor={{ fill: "transparent" }} />
          {Categories.map(({ category, fill }) => (
            <Bar
              key={category}
              dataKey={category}
              fill={fill}
              isAnimationActive={false}
              shape={<Rectangle radius={[10, 10, 10, 10]} />}
              minPointSize={16}
            >
              <LabelList
                dataKey={category}
                position="insideBottomLeft"
                dy={11}
                offset="15"
                angle="-90"
                fill="#FFF"
              />
            </Bar>
          ))}
        </BarChart>
      </ResponsiveContainer>
    </ChartWrapper>
  );
};

export default memo(PerformanceBreakdownChart);

The above code shows tooltip for all bars at once as follows

enter image description here

But, I want to show the tooltip for each individual bar as follows

enter image description here

What can I do to achieve this? Thanks

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文