简体   繁体   English

Plotly R:根据不同的栏更改hoverinfo字体颜色 colors

[英]Plotly R: change hoverinfo font color according to different bar colors

I have this dataframe:我有这个 dataframe:

df2 = data.frame(value = c(9, 2, 7, 3, 6),
                 key = c('ar', 'or', 'br', 'gt', 'ko'))

And this is the code I have to generate this plot这是我必须生成的代码plot

df2 %>% 
  plot_ly(x = ~key,
          y = ~value,
          type = 'bar',
          color = ~value,
          colors = c(rgb(0, 0, 0, 1), rgb(1, 0.85, 0.85, 1)),
          stroke = I('black'),
          span = I(1),
          hoverinfo = 'text',
          hovertext = ~paste0('value: ', value,
                              '\nkey: ', key)
          ) %>% 
  layout(hoverlabel = list(bordercolor = 'transparent', 
                           font = list(family = 'DM Sans', 
                                       size = 15, 
                                       color = 'white')))

But, as shown above, the font color of the hoverinfo is white when hovering over the lighter color.但是,如上所示,当悬停在较浅的颜色上时, hoverinfofont color白色

Basically, I would like to set the font color to black when the color is lighter , and to white when the color is dark , as these images show: black, when lighter , and white, when darker基本上,我想black when the color is lighterwhite when the color is dark ,如这些图像所示:黑色,较浅白色,较深

Any tips here?这里有什么提示吗?

There might be an easier method than this, but this works.可能有比这更简单的方法,但这很有效。 I've used the package htmlwidgets , in addition to plotly .除了plotly htmlwidgets

I've written in the change in text color as an event because the alternative is five separate traces like you eluded to in your question.我已经将文本颜色的变化写为一个事件,因为替代方案是五个单独的痕迹,就像您在问题中所避开的那样。

In the JS, I have an if–else statement where it queries the pointerNumber .在 JS 中,我有一个 if–else 语句,它查询pointerNumber The pointer number is the number of markers, bars, lines, whatever it is you have on your plot. In this case, it's bars.指针编号是标记、条、线的数量,无论您在 plot 上有什么。在这种情况下,它是条。

The pointer numbers always start at zero and are consecutive numbers—don't assume the order matches the visualization.指针数字总是从零开始并且是连续的数字——不要假设顺序与可视化相匹配。 (I think it follows the order the data is provided, but I've never felt the need to investigate it, so I'm not 100% on that.) (我认为它遵循提供数据的顺序,但我从来没有觉得有必要调查它,所以我不是 100% 的。)

In this code I changed pointer number 0 to have black text (that's the light pink one).在此代码中,我将指针编号 0 更改为黑色文本(即浅粉色文本)。 If you felt that the br bar should also have black text, you could modify if(pn === 0){ to if(pn === 0 || pn === 2) { and now the bar next to the pink one would also have black text.如果你觉得br栏也应该有黑色文本,你可以修改if(pn === 0){if(pn === 0 || pn === 2) {现在粉红色旁边的栏一个也会有黑色文本。 (Pointer number 1 happens to be the column on the far right.) (指针编号 1 恰好是最右侧的列。)

The event code:事件代码:

hoverer = "function(el, x) {
            el.on('plotly_hover', function(d){
              var pn = d.points[0].pointNumber;
              var fon = [];
              var hov = [];
              if(pn === 0){ 
                col = 'black';
              } else {
                col = 'white'};
              fon = {
                'family': 'DM Sans',
                'size': 15,
                'color': col};
              hov = {
                'bordercolor': 'transparent',
                'font': fon};
              h = {hoverlabel: hov};
              Plotly.restyle(el.id, h);
            });}"

This is how you use it with your graph:这就是你如何将它与你的图表一起使用:

df2 %>% 
  plot_ly(x = ~key,
          y = ~value,
          type = 'bar',
          color = ~value,
          colors = c(rgb(0, 0, 0, 1), rgb(1, 0.85, 0.85, 1)),
          stroke = I('black'),
          span = I(1),
          hoverinfo = 'text',
          hovertext = ~paste0('value: ', value,
                              '\nkey: ', key)) %>% 
  layout(hoverlabel = list(bordercolor = 'transparent', 
                           font = list(family = 'DM Sans', 
                                       size = 15, 
                                       color = 'white'))) %>% 
  htmlwidgets::onRender(hoverer) # this will trigger the event driven code

在此处输入图像描述 在此处输入图像描述

在此处输入图像描述

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

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