工具提示仅显示Recharts中的第一个栏反应?

发布于 2025-02-11 21:19:59 字数 5176 浏览 2 评论 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";
import CustomTooltip from "../timeSeriesChart/CustomTooltip";
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 PerformanceBreakdownChart = ({ data, ...props }) => {
  const classes = useStyles();
  console.log("categories", data);

  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" }}
            content={<CustomTooltip />}
          />
          {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);

customTooltip.js

import React, { memo } from "react";
import { makeStyles } from "@material-ui/core/styles";
import { getCategory, getCategoryColor } from "../../../data/helpers/helpers";
import { useTranslation } from "react-i18next";
import { GREY } from "../../../pages/overviewPage/containers/DistanceTraveledParentContainer";

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 CustomTooltip = ({ active, payload, label, signature }) => {
  const { t } = useTranslation();
  const classes = useStyles();

  if (active && payload) {
    const color = signature ? GREY : getCategoryColor(payload[0].value);
    const customLabel = t(
      signature ? signature : getCategory(payload[0].value)
    );

    return (
      <div className={classes.root}>
        <div
          className={classes.tooltipData}
          style={{ backgroundColor: `${color}` }}
        >
          <p className={classes.label}>{`${label}`}</p>
        </div>
        <p className={classes.toolValue}>{payload[0].value}</p>
        <p className={classes.signature}>{customLabel}</p>
      </div>
    );
  }

  return null;
};

export default memo(CustomTooltip);

上述代码输出看起来像这样。 输出

我如何将单个条的工具显示为以下片段? 实际所需工具提示

I am new to react and recharts and wanted to show tooltip for individual bars but Unfortunately It shows tooltip for the first bar not for the others.

Here 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";
import CustomTooltip from "../timeSeriesChart/CustomTooltip";
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 PerformanceBreakdownChart = ({ data, ...props }) => {
  const classes = useStyles();
  console.log("categories", data);

  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" }}
            content={<CustomTooltip />}
          />
          {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);

CustomTooltip.js

import React, { memo } from "react";
import { makeStyles } from "@material-ui/core/styles";
import { getCategory, getCategoryColor } from "../../../data/helpers/helpers";
import { useTranslation } from "react-i18next";
import { GREY } from "../../../pages/overviewPage/containers/DistanceTraveledParentContainer";

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 CustomTooltip = ({ active, payload, label, signature }) => {
  const { t } = useTranslation();
  const classes = useStyles();

  if (active && payload) {
    const color = signature ? GREY : getCategoryColor(payload[0].value);
    const customLabel = t(
      signature ? signature : getCategory(payload[0].value)
    );

    return (
      <div className={classes.root}>
        <div
          className={classes.tooltipData}
          style={{ backgroundColor: `${color}` }}
        >
          <p className={classes.label}>{`${label}`}</p>
        </div>
        <p className={classes.toolValue}>{payload[0].value}</p>
        <p className={classes.signature}>{customLabel}</p>
      </div>
    );
  }

  return null;
};

export default memo(CustomTooltip);

The above code output looks like this.
output

How can I show tooltip for individual bars as the following snippet?
actual needed tooltip

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

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

发布评论

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