[英]Change background of group label in plotly R hoverlabel
我不知道本地 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.