Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

e_tooltip and label #639

Open
antoine4ucsd opened this issue Jul 23, 2024 · 5 comments
Open

e_tooltip and label #639

antoine4ucsd opened this issue Jul 23, 2024 · 5 comments

Comments

@antoine4ucsd
Copy link

Hello
I have this df

df <- data.frame(
  redcap_data_access_group = c(80001, 80001, 80001),
  N = c(744, 744, 744),
  name_new = c("IMV", "SARI", "SARI on IMV"),
  n = c(248, 248, 248),
  pct = c(0.333, 0.333, 0.333),
  lab_pct = c("33%", "33%", "33%"),
  lab_n = c("248 / 744", "248 / 744", "248 / 744"),
  stringsAsFactors = FALSE
)

if I run this

db  |>
        mutate(redcap_data_access_group = as.character(redcap_data_access_group)) |>
        arrange(pct) |>
        group_by(name_new) |>
        e_charts(redcap_data_access_group, height = "300") |>
        e_bar(pct, stack = "redcap_data_access_group",
              bind = lab_n,
              label = list(
                      show = TRUE,
                      formatter = "{b}",
                      position = "inside"
              )
        ) |>
        e_tooltip(trigger = "item",
                  formatter = htmlwidgets::JS("
              function(params){
                return('<strong>' + params.name +
                '</strong><br /> ' + (params.value[1] * 100).toFixed(2) + '%');
              }"),
                  backgroundColor = "rgba(255,255,255,0.7)"
        ) |>
        e_color(c("#be0032", "#0067a5", "#f3c300")) |>
        e_legend(show = FALSE)

the data when hovering is showing lab_n followed by percentage.

image

is there a way to add the name_new before lab_n followed by percentage when hovering?

thank you!

@rdatasculptor
Copy link
Contributor

rdatasculptor commented Aug 9, 2024

Because of lack of time I haven't managed yet to get them stacked, but maybe you can use this script for some inspiration?

df <- data.frame(
  redcap_data_access_group =  c(80001, 80001, 80001),
  N = c(744, 744, 744),
  name_new = c("IMV", "SARI", "SARI on IMV"),
  n = c(248, 248, 248),
  pct = c(0.333, 0.333, 0.333),
  lab_pct = c("33%", "33%", "33%"),
  lab_n = c("248 / 744", "248 / 744", "248 / 744"),
  stringsAsFactors = FALSE
) |>
  mutate(tooltipContent = paste0("<b>", name_new, " ", lab_n, "</b><br />", lab_pct))
df$color <- c("#be0032", "#0067a5", "#f3c300")

df  |>
  mutate(redcap_data_access_group = as.character(redcap_data_access_group)) |>
  arrange(pct) |>
  #group_by(name_new) |>
  e_charts(name_new, height = "300") |>
  e_bar(pct, stack = "redcap_data_access_group",
        bind = lab_n,
        label = list(
          show = TRUE,
          formatter = "{b}",
          position = "inside"
        )
  ) |>
  e_add_nested('extra', tooltipContent) |>
  e_add_nested('itemStyle', color = color) |>
  e_tooltip(trigger = "item",
            formatter = htmlwidgets::JS(
              'function(params){
        return params.data.extra.tooltipContent;
      }'
            ),
            backgroundColor = "rgba(255,255,255,0.7)"
  ) |>
  e_legend(show = FALSE)

@antoine4ucsd
Copy link
Author

Hello
I am still struggling with e_tooltip...
I have this data

data <- data.frame(
  name = c('Fatigue', 'Sore throat', 'Headache', 'Sore throat', 'Headache', 'Fatigue'),
  N = c(165, 158, 161, 134, 132, 133),
  n = c(96, 8, 25, 0, 2, 13),
  pct = 100*c(0.582, 0.0506, 0.155, 0, 0.0152, 0.0977),
  lab_n = c('96 / 165', '8 / 158', '25 / 161', '0 / 134', '2 / 132', '13 / 133'),
  time = c('admission', 'admission', 'admission', 'discharge', 'discharge', 'discharge'),
  stringsAsFactors = FALSE
)

I would like to show a mirror plot with the absolute value of the percentage for

data|>
        dplyr::select(time,name,pct)|>spread(time,pct)|>
        ungroup()|>
        dplyr::mutate(admission=-admission)|>
        e_chart(name)|>
        e_bar(admission,stack = "grp", color = "#1f77b4")|>
        e_bar(discharge,stack = "grp",color = "#e377c2")|>
        e_flip_coords()|>
        e_y_axis(nameLocation = 'middle',position="left",
                 nameTextStyle = list(fontWeight = 'bold',fontSize = 16),nameGap = 65
        ) %>%
        # Tooltip to show absolute percentage values
         e_tooltip(trigger = "axis", 
                      backgroundColor = "rgba(255,255,255,0.7)")|>

        e_x_axis(
                nameLocation = 'middle',
                nameTextStyle = list(fontWeight = 'bold', fontSize = 16),
                nameGap = 25,
                name = "Percentage of cases",
                axisLabel = list(
                        show = TRUE, 
                        formatter = htmlwidgets::JS('function(value) { return Math.abs(value) + "%"; }')
                )
        )

so far this is what I get
image

I would like to see 58.1% and 9.7%
does that make sense?

thank you!

@antoine4ucsd
Copy link
Author

Hello
just follow up . I am still struggling with tooltips... any clues?

thank you!

@rdatasculptor
Copy link
Contributor

It's a little bit hard to understand what you intend, because the chart and data are different from your first attempt. Can you please tell me why my suggestions were not sufficient? Then maybe I can help you with better solution

@antoine4ucsd
Copy link
Author

thank you for following up.
my goal is to show the absolute value in percentage for each group
in the screenshot. the value are -0.58 and 0.097 so absolute value are 0.58 and 0.0.97 and in percentage 58% and 9.7%
in my example above, it shows the value*100 but not absolute and not with %.
I am still struggling with the tooltip syntax. really sorry. it often leads to NA because I cannot figure out to the correct value, etc

not sure what else you'd need to clarify?
thank you for always trying to help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants