如何调整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
相关产品推荐
相关产品推荐

