You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何不使用Dash为Plotly旭日图添加可点击超链接?

给Plotly旭日图扇区添加可点击URL链接的方案

Plotly Express默认没有直接给旭日图扇区添加点击链接的功能,但可以通过自定义JavaScript事件+自定义数据绑定实现纯Plotly环境下的点击跳转效果,无需额外依赖框架。

实现步骤与代码示例

import pandas as pd
import plotly.express as px

data = {
    'site': {1: 'Site 1', 2: 'Site 2'},
    'link': {1: 'http://www.google.com', 2: 'http://www.facebook.com'},
    'popularity': {1: 50, 2: 50}
}

df = pd.DataFrame(data)

# 创建旭日图,将link字段作为customdata绑定到每个扇区
fig = px.sunburst(
    df,
    path=['site', 'link'],
    values='popularity',
    maxdepth=2,
    custom_data=['link']  # 绑定链接数据
)

# 添加自定义JavaScript点击事件处理逻辑
fig.add_js_event(
    event='plotly_click',
    code="""
        const link = event.points[0].customdata[0];
        if (link) {
            window.open(link, '_blank'); // 在新窗口打开链接
        }
    """
)

fig.show()

代码说明

  • custom_data=['link']:将数据中的URL链接绑定到每个扇区的自定义数据字段,点击时可以获取到对应链接
  • fig.add_js_event:监听图表的plotly_click事件,当用户点击扇区时,提取customdata中的链接并通过window.open在新窗口打开

这种方法完全基于Plotly原生API实现,不需要引入Dash等额外框架,满足纯Plotly环境的需求。

内容的提问来源于stack exchange,提问作者Louiz Zanjroz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 12:55:58