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

如何调整Plotly饼图悬停标签内长文本并移除Trace 0标识

Plotly饼图悬停标签问题修复方案

针对你提出的两个需求,调整代码参数即可解决:

  • 移除默认Trace 0标识:在hovertemplate的拼接内容末尾添加<extra></extra>标记,该标记会隐藏Plotly默认渲染的轨迹名称栏
  • 解决长文本截断问题:在hoverlabel配置中新增namelength = -1(关闭默认的文本长度截断规则,完整展示所有内容)、align = "left"(优化长文本排版)

修改后的完整代码如下:

paired1 <- brewer.pal(n = 10, name = "Paired")

output$plot4 <- renderPlotly(
  plot4 <- plot_ly( df.prescripciones.3.direccionadas.grafica ,
                    labels = ~Profesional, 
                    values = ~Total,
                    type = 'pie',
                    marker = list(colors = paired1),
                    texttemplate = ~ paste ("<br>",
                                            "%{percent} <br>"),
                    hovertemplate = ~ paste (  Profesional,
                                               " <br>",IPS," <br>",
                                               "%{percent} <br>",
                                               "$","%{value} <br>",
                                               "<extra></extra>")  ) %>%
    layout( title = list  ( text = "Top 10 Profesionales"),
            font = list (size = 11) ,
            hoverlabel = list(font=list(size=11), 
                              namelength = -1,
                              align = "left") ) %>% 
    layout(showlegend = F) %>%
    layout(legend = list(x = -0.5, y = 0.5)) %>% 
    layout(autosize = T,  margin = list(l = 25,
                                        r = 65,
                                        b = 40,
                                        t = 60))  %>%
    layout(plot_bgcolor  = "rgba(0, 0, 0, 0)",
           paper_bgcolor = "rgba(0, 0, 0, 0)"
    ) 
)

如果依然存在文本超出的情况,可以在长文本字段(如IPS)的拼接逻辑中加入手动换行标记<br>,或者适当调小hoverlabel的字体大小适配。

内容的提问来源于stack exchange,提问作者Andres Mora

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.14 22:45:04