You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

react-chart-js-2环形图如何为数值标签添加美元符号格式化显示

环形图金额追加美元符号需求

我希望该环形图(donut chart)中显示的每笔金额都附带美元符号,该需求并不影响核心功能使用,但优化显示效果会更好。
我已经尝试了所有能想到的语法组合,都没有实现预期效果。
事先说明:我接触相关开发的时间不长,但学习意愿很强,希望能得到大家的帮助。

react-chart-js-2 环形图问题

我的代码如下:

const [open, setOpen] = React.useState(false);
const classes = useStyles();

const holdings = useSelector((store) => store.holdingsReducer); // 从数据库获取用户所有持仓数据

const handleClickOpen = () => {
  setOpen(true);
};

const handleClose = () => {
  setOpen(false);
  return;
};

let namesArray = [];
const loopNames = () => {
  for (let item of holdings) {
    namesArray.push(item.name);
  }
  return namesArray;
};

let donutArray = [];
const loopHoldings = () => {
  let donutArray = [];
  for (let item of holdings) {
    let val = (item.coins_held * item.per_coin_val).toFixed(2);
    donutArray.push(val);
  }
  return donutArray;
};

返回块代码如下:

<div>
  <Button
    style={{
      textAlign: "center",
      fontSize: "24px",
      paddingBottom: "0px",
      backgroundColor: "#9333F0",
      color: "white",
    }}
    variant="contained"
    size="medium"
    onClick={handleClickOpen}
    className={classes.pieButton}
  >
    <b>
      <DonutLargeIcon
        style={{
          transform: "scale(1.5)",
          paddingTop: "8px",
          paddingBottom: "2px",
        }}
      />
    </b>
  </Button>

  <Dialog
    open={open}
    TransitionComponent={Transition}
    keepMounted
    onClose={handleClose}
    aria-describedby="alert-dialog-slide-description"
  >
    <Doughnut
      style={{ padding: "5px" }}
      data={{
        labels: loopNames(),
        datasets: [
          {
            data: loopHoldings(),
            backgroundColor: [
              "#613BED",
              "#44E059",
              "#D747F5",
              "#3ED2F7",
              "#F7EE3E",
              "#CDD6E3",
              "#F2DFC2",
              "#FA6534",
            ],
          },
        ],
      }}
      height={400}
      width={400}
      options={{
        plugins: {
          legend: true,
        },
        animations: true,
        maintainAspectRatio: true,
      }}
    />

    <DialogActions>
      <Container style={{ textAlign: "right", marginBottom: "5px" }}>
        <Button
          style={{
            textAlign: "center",
            fontSize: "14px",
            fontWeight: "bold",
            backgroundColor: "#9333F0",
            color: "white",
          }}
          className={classes.dialogueButtons}
          onClick={handleClose}
        >
          Close
        </Button>
      </Container>
    </DialogActions>
  </Dialog>
</div>
);
}

感谢大家的帮助!


内容的提问来源于stack exchange,提问作者Chris Mo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 07:45:04