plotly / plotly/plotly.R

hovertemplate not rendering text for single items in the plot

Open
#2,213 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
R
Stars
2.7k
Forks
641
PR merge metrics
No merged PRs in 30d

Description


When using hovertemplate in bar charts to annotate bars,
if the variable is present only once then text annotation doesn't work

For example in below image, Type B is only present once
and when hovered on it shows as "%{text}
hovertemplate

but when using hoverinfo it seems to work fine
hoverinfo

I need to use hovertemplate, as i had to use it in a function, where color and text options are passed as variable

any workaround to fix this?

Reproducible code example

library(data.table)
library(plotly)

data <- data.table(Date = c(rep(as.Date("2022-10-18"), 3),
                                rep(as.Date("2022-10-19"), 3),
                                rep(as.Date("2022-10-20"), 3)),
                       Type = c('A', 'B', 'C', 'A', 'D', 'C', 'C', 'D', 'A'),
                       Frequency = c(6, 3,  6, 10,  9,  9,  9,  3,  2))

# hovertemplate - Type B is only one entry and it shows as %{text}
plot_ly(data, x=~Date, y=~Frequency, color=~Type, type='bar', text=~Type,
        hovertemplate=paste('<b>%{text}</b>',
                            '<br><b>%{y}</b>',
                            '<br><b>%{x}</b>',
                            '<extra></extra>')) %>%
  layout(barmode="stack")

# hoverinfo - works as expected
plot_ly(data, x=~Date, y=~Frequency, color=~Type, type='bar',
        hoverinfo = 'text',
        text = ~paste('<b>', Type, '</b>',
                      '<br><b>', Date, '</b>',
                      '<br><b>', Frequency, '</b>')) %>%
  layout(barmode="stack")

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the reproducible R example from the issue and compare the hovertemplate and hoverinfo outputs, focusing on the single Type B bar. Trace the Plotly rendering path used by the R package; done when hovertemplate displays the Type B text instead of the literal %{text}.

Written by the indexing model from the issue text.

Assessment

Tech stack
plotly, r
Domain
data-visualization
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.