vue echarts柱状图上的小数转成百分数
可以通过 echarts 的 formatter 属性来实现将小数转为百分数。具体的实现方式如下:
-
在 echarts 的 option 中找到柱状图的 series 部分,设置 formatter 属性为一个函数。
-
在函数中将数据项的值乘以 100 并添加百分号,然后返回处理后的字符串即可。
示例代码如下:
option = {
// 其他配置项省略
series: [
{
name: '数据',
type: 'bar',
data: [0.15, 0.23, 0.37, 0.41, 0.58],
label: {
show: true,
formatter: function (params) {
// 将小数转为百分数并添加百分号
return (params.value * 100).toFixed(2) + '%';
}
}
}
]
};
在上述示例中,label.show 表示是否显示标签,formatter 函数中的 params.value 表示当前数据项的值,将其乘以 100 并调用 toFixed 方法保留两位小数,最后添加百分号即可。
原文地址: https://www.cveoy.top/t/topic/wie 著作权归作者所有。请勿转载和采集!