【问题标题】:How concatenate string with value in recharts - ReactJS如何在 recharts 中将字符串与值连接 - ReactJS
【发布时间】:2023-03-15 09:55:01
【问题描述】:

当值显示在条形顶部时,我想将“%”与 barchart 上的值连接起来。

怎么做?

 <ResponsiveContainer width="99%" aspect={4}>
      <BarChart data={chartData} margin={{ top: 0, left: -30, right: 0, bottom: 0 }}>
        <Bar dataKey='hum' name="Humidity" barSize={15} fill='rgba(0, 60, 255, 0.6)' unit=" %" >
        <LabelList dataKey="hum" position="top" />
        </Bar>
        <CartesianGrid stroke="#ccc" strokeDasharray="5 5" />
        <XAxis dataKey="date" tick={{ fontSize: 12, fill: 'black' }} tickFormatter={(unixTime) => moment(unixTime).format('DD.MM - HHч.')} interval={1} />
        <YAxis tick={{ fontSize: 12, fill: 'black' }} />
        <Tooltip />
      </BarChart>
 </ResponsiveContainer>

我这样返回数据:

formatData = (data) =>
data.map(({ dt_txt, main, pop }) => ({
  // date -> Can be used as dataKey for XAxis
  //Further you can format the date as per your need
  date: dt_txt,
  // temp -> Can be used as dataKey for Line
  temp: main.temp,
  hum: main.humidity,
  rain:  pop
}));

我想将 "%" 连接到 hum 属性并在图表上显示值。

【问题讨论】:

    标签: javascript reactjs recharts


    【解决方案1】:

    尝试在 YAxis 中赋予“unit”属性,像这样

    编辑: 尝试像这样更改 LabelList 标记。

    <LabelList dataKey="hum" position="top" content={renderLabel} />
    

    并在您的代码中添加以下功能性反应组件。

    const renderLabel= ({value}) => {
    
      return (
        <span>{ value + '%'}</span>
      );
    };
    

    【讨论】:

    • 这对 YAxis 来说是个好主意,但我想在顶部显示每个值的“%”..
    • 根据文档,您可以在 LabelList 标记中添加 content={renderLabel},其中 renderLabel 是您的反应组件,它应该返回与 % 连接的值。
    • 我更新了我的 qeustion,你能检查一下吗.. 我是 react 新手,真的很想知道如何做到这一点.. 如果可能的话
    猜你喜欢
    • 2010-11-03
    • 1970-01-01
    • 2020-07-31
    • 1970-01-01
    • 2017-01-29
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多