前段时间写了篇博客,解决的是pyecharts 实现显示数据为百分比的柱状图,以为折线图也是一样的方法。只要调整 formatter 即可。
原始代码如下 :
from pyecharts.charts import Line
from pyecharts import options as opts
data_x = ['20200306', '20200307', '20200308', '20200309', '20200310', '20200311', '20200312']
data_y = ['18.51%', '17.11%', '18.67%', '16.61%', '16.74%', '17.55%', '17.18%']
data_y_0 = [float(x.strip('%')) for x in data_y]line = (Line().add_xaxis(data_x).add_yaxis("带宽利用率", data_y_0, label_opts=opts.LabelOpts(formatter="{c}%")).set_global_opts(yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} %")))
)
line.render()
一切看起来都很正常。显示却是这样。
那一大串是啥, 看起来应该是 formatter="{b},{c}%" 的效果。 反复折腾,无法得到和 柱状图一样的效果。
帖子讨论过程:https://bbs.csdn.net/topics/396397133
官方答复是用另一种方法:
from pyecharts.charts import Line
from pyecharts import options as opts
from pyecharts.commons.utils import JsCode
data_x = ['20200306', '20200307', '20200308', '20200309', '20200310', '20200311', '20200312']
data_y = ['18.51%', '17.11%', '18.67%', '16.61%', '16.74%', '17.55%', '17.18%']
data_y_0 = [float(x.strip('%')) for x in data_y]line = (Line().add_xaxis(data_x).add_yaxis("带宽利用率", data_y_0, label_opts=opts.LabelOpts(formatter=JsCode("function (params) {return params.value[1] + '%'}"))).set_global_opts(yaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(formatter="{value} %")))
)
line.render()
OK , 正常了。