Plotly R:根据不同的条颜色更改Hoverinfo字体颜色

发布于 2025-01-23 10:14:14 字数 1265 浏览 4 评论 0原文

我有此数据框:

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

这是我必须生成的代码此图

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')))

但是,如图所示,上面,Hoveinfo字体颜色 white 在悬停在较浅的颜色上时。

基本上,我想将字体颜色设置为黑色时颜色较轻,而白色当颜色变暗时为白色,如这些图像所示: black,当更轻白色,当时

这里有任何技巧吗?

I have this 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

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.

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

Any tips here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

九厘米的零° 2025-01-30 10:14:14

可能有比这更容易的方法,但这有效。除了plotly,我已经使用了软件包htmlwidgets

我以文本颜色的变化为单写,因为替代方案是五个单独的痕迹,就像您在问题中所避免的那样。

在JS中,我有一个IF -else语句,它查询pointernumber。指针编号是标记,条,线条的数量,无论您的情节上有什么。在这种情况下,是酒吧。

指针数总是从零开始,并且是连续的数字 - 不要假设该顺序与可视化匹配。 (我认为它遵循提供数据的顺序,但我从来没有觉得需要调查它,所以我不是100%。)

在此代码中,我更改了指针数0,以具有黑色文本(这就是浅粉红色)。如果您觉得br bar也应该有黑色文本,则可以修改如果(pn === 0){ to if(pn === 0) ||。 (指针编号1恰好是最右边的列。)

事件代码:

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);
            });}"

这是您与图形一起使用的方式:

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

“在此处输入映像”

There might be an easier method than this, but this works. I've used the package htmlwidgets, in addition to plotly.

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. The pointer number is the number of markers, bars, lines, whatever it is you have on your plot. In this case, it's bars.

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.)

In this code I changed pointer number 0 to have black text (that's the light pink one). 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. (Pointer number 1 happens to be the column on the far right.)

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

enter image description hereenter image description here

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文