Skip to contents

[toegankelijkheid]

Usage

ro_ply_keyboard_nav_linechart(fig)

Arguments

fig

A plotly object (from plotly::plot_ly() or plotly::ggplotly()).

Value

A plotly object with improved keyboard accessibility for line charts.

Details

Enhances the accessibility of interactive line charts created with plotly or ggplotly. This function injects custom JavaScript to make bar elements focusable and navigable via keyboard, displays accessible tooltips, and ensures screen readers can interpret the chart. The function is intended for use on plotly line charts (including those created with ggplotly).

Keyboard navigation: users can use Tab to focus the first bar, and arrow keys (Left/Right/Up/Down) to move between points on the line Pressing Esc removes focus from the chart and returns focus to the next logical element.

Examples

if (FALSE) { # \dontrun{
data <- tidyr::tibble(expand.grid(Jaar = 2001:2020, Geslacht = c("Man", "Vrouw")),
               value = sample(1:50, 40, TRUE)) |>
  dplyr::bind_rows(., dplyr::summarize(., value = sum(value), .by = "Jaar") |>
                     dplyr::mutate(Geslacht = "Totaal")) |>
  dplyr::mutate(Geslacht = factor(Geslacht, levels = c("Man", "Vrouw", "Totaal")))
library(ggplot2)
library(plotly)
p <- ggplot(data, aes(x = Jaar, y = value, color = Geslacht, group = Geslacht,
                      text = paste0(
                          "Geslacht: <b>", Geslacht,
                          "</b><br>Jaar: <b>", Jaar,
                          "</b><br>Aantal cases: <b>", value
                      ))) +
    geom_line()
fig <- ggplotly(p, tooltip = "text")
fig <- ro_ply_keyboard_nav_linechart(fig)
fig
} # }