python基于echart使用JavaScript实现数据的图标显示代码
时间: 2023-12-10 19:42:03 浏览: 84
首先,需要安装 echarts 和 pyecharts 库。
然后,可以按照以下步骤实现:
1. 导入所需库:
```python
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
```
2. 准备数据:
```python
data = {"name": ["A", "B", "C", "D", "E", "F"],
"value": [10, 20, 30, 40, 50, 60]}
```
3. 创建 Bar 图:
```python
bar = (Bar()
.add_xaxis(data["name"])
.add_yaxis("数值", data["value"],
itemstyle_opts=opts.ItemStyleOpts(color="#5793f3"),
label_opts=opts.LabelOpts(formatter=JsCode("""
function(params) {
return params.value + ' %';
}
"""))
))
```
4. 设置图表样式:
```python
bar.set_global_opts(
title_opts=opts.TitleOpts(title="数据图表", subtitle="数据来源:某某公司"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
yaxis_opts=opts.AxisOpts(name="数量", axislabel_opts=opts.LabelOpts(formatter="{value}")),
)
```
5. 渲染图表:
```python
bar.render_notebook()
```
完整代码如下:
```python
from pyecharts import options as opts
from pyecharts.charts import Bar
from pyecharts.commons.utils import JsCode
data = {"name": ["A", "B", "C", "D", "E", "F"],
"value": [10, 20, 30, 40, 50, 60]}
bar = (Bar()
.add_xaxis(data["name"])
.add_yaxis("数值", data["value"],
itemstyle_opts=opts.ItemStyleOpts(color="#5793f3"),
label_opts=opts.LabelOpts(formatter=JsCode("""
function(params) {
return params.value + ' %';
}
"""))
))
bar.set_global_opts(
title_opts=opts.TitleOpts(title="数据图表", subtitle="数据来源:某某公司"),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(rotate=-15)),
yaxis_opts=opts.AxisOpts(name="数量", axislabel_opts=opts.LabelOpts(formatter="{value}")),
)
bar.render_notebook()
```
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![7z](https://img-home.csdnimg.cn/images/20241231044736.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![py](https://img-home.csdnimg.cn/images/20250102104920.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)