javascript - How concatenate string with value in recharts - ReactJS
问题描述
I want to concatenate "%" with the value on barchart
when values show on top of the bars.
How to do that ?
<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>
I return data like this:
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
}));
I want to concatenate "%" to hum property and display the values on the chart.
解决方案
尝试在 YAxis 中赋予“unit”属性,例如 <YAxis tick={{ fontSize: 12, fill: 'black' }} unit="%"/>
编辑:尝试像这样更改 LabelList 标签。
<LabelList dataKey="hum" position="top" content={renderLabel} />
并在您的代码中添加以下功能性反应组件。
const renderLabel= ({value}) => {
return (
<span>{ value + '%'}</span>
);
};
推荐阅读
- java - 过滤器重定向和登录后加载下一页
- php - 完整性约束违规:1452 无法添加或更新子行:外键约束
- c# - 如何在 iOS 上的 Xamarin.Forms CollectionView 中关闭弹跳效果
- python - 根据来自小 DataFrame 的信息过滤大 DataFrame
- javascript - 反应本机滑块不更新状态更新进度
- android - 播放动画期间运动布局冻结
- php - 无法连接到远程 MySQL 服务器
- javascript - AdonisJs - 如何将我的日期字段与数据库中的日期相等
- typescript - 从对象中获取项目没有错误
- javascript - 如何在 axios nuxt 模块中使用基本身份验证