繁体   English   中英

在 plotly R hoverlabel 中更改组标签的背景

[英]Change background of group label in plotly R hoverlabel

有没有办法改变 plotly R 中组标签的背景颜色? 我可以看到主标签可以设置样式但找不到组标签的任何解决方案,如果可能的话最好是背景颜色和字体颜色。 在此处输入图像描述

我不知道本地 Plotly 方法,但我可以为您提供解决方法。

现在,默认设置为白色背景,不透明度为 80%。

您没有使您的问题可重现(这通常会更快地为您提供很好的答案),所以我创建了一些。

library(plotly)
library(tidyverse)

df <- data.frame(
  x = LETTERS[1:3],
  y = c(1:3)
)

我创建了这个小块来用您选择的颜色替换背景颜色......我目前将colr设置为#444

# current background of the trace label is white at .8 opacity
colr = "#444"  # this is the current text color in the tooltip data

plot_ly(df, x = ~x, y = ~y, name = "trace", type = 'bar',
        showlegend = T, color = ~x) %>% 
  htmlwidgets::onRender(
    paste0(
      "function(el, x) {
      colr = '", colr, "';
      el.on('plotly_hover', function(d){
      hd = document.querySelector('g.hoverlayer');
      hd.innerHTML = hd.innerHTML.replace('rgb(255, 255, 255)', colr);
    });
    }"
  ))

在此处输入图像描述

如果您将其更改为: colr = 'black'

在此处输入图像描述

虽然,以这种方式更改颜色可能更合适(这完全相同)。 不过,如果您有大量数据,两者之一可能会更快。

plot_ly(df, x = ~x, y = ~y, name = "trace",
        showlegend = T, color = ~x, type = "bar") %>% 
  htmlwidgets::onRender(
    paste0(
      "function(el, x) {
      el.on('plotly_hover', function(d){
        hd = document.querySelector('g.hoverlayer');
        if(hd.innerHTML !== '') { /* if there's a tooltip then... */
          rct = document.querySelector('g.hoverlayer rect');
          rct.style.fill = '", colr, "';
        }
      });
      }"
    ))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM