pyecharts treemap 点击事件
时间: 2023-12-14 22:34:32 浏览: 184
根据提供的引用[1],pyecharts treemap 点击事件可以通过添加 on-click 事件来实现。具体实现步骤如下:
1.导入所需模块
```python
from pyecharts.charts import TreeMap
from pyecharts import options as opts
```
2.创建数据
```python
data = [
{"value": 40, "name": "我是A"},
{"value": 20, "name": "我是B"},
{"value": 10, "name": "我是C"},
{"value": 5, "name": "我是D"},
{"value": 3, "name": "我是E"},
{"value": 2, "name": "我是F"},
{"value": 1, "name": "我是G"},
{"value": 1, "name": "我是H"},
{"value": 1, "name": "我是I"},
{"value": 1, "name": "我是J"},
{"value": 1, "name": "我是K"},
{"value": 1, "name": "我是L"},
{"value": 1, "name": "我是M"},
{"value": 1, "name": "我是N"},
{"value": 1, "name": "我是O"},
{"value": 1, "name": "我是P"},
{"value": 1, "name": "我是Q"},
{"value": 1, "name": "我是R"},
{"value": 1, "name": "我是S"},
{"value": 1, "name": "我是T"},
{"value": 1, "name": "我是U"},
{"value": 1, "name": "我是V"},
{"value": 1, "name": "我是W"},
{"value": 1, "name": "我是X"},
{"value": 1, "name": "我是Y"},
{"value": 1, "name": "我是Z"},
]
```
3.创建 treemap 并添加 on-click 事件
```python
treemap = (
TreeMap()
.add("", data)
.set_global_opts(title_opts=opts.TitleOpts(title="Treemap-基本示例", subtitle="我是副标题"))
.set_series_opts(
label_opts=opts.LabelOpts(
position="inside",
font_size=12,
formatter="{b}",
font_style="italic",
font_weight="bold",
color="white",
),
# 添加 on-click 事件
emphasis_label_opts=opts.LabelOpts(
position="inside",
font_size=12,
formatter="{b}",
font_style="italic",
font_weight="bold",
color="white",
).set_display_mode("single"),
)
.set_global_opts(
tooltip_opts=opts.TooltipOpts(trigger="item", formatter="{b}: {c}")
)
)
# 添加 on-click 事件
treemap.on("click", print)
# 渲染图表
treemap.render("treemap.html")
```
在上述代码中,我们通过 `treemap.on("click", print)` 添加了一个 on-click 事件,当用户点击 treemap 中的某个元素时,会触发该事件并打印出被点击元素的信息。
相关推荐
![html](https://img-home.csdnimg.cn/images/20210720083451.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)