Customizing sparkline tooltips for bar type

41 Views Asked by At

I would like the tooltips to read "one, two, three", instead of "0, 1, 2" in this sparkline plot created with the R sparkline package.

Although I don't see this anywhere in the sparkline documentation, I see from some examples online that the offset variable for a bar chart refers to the x-axis values.

I am trying to map the offset values using the tooltipValueLookups argument. What am I doing wrong?

library(sparkline)
sparkline(
  values = c(3, 5, 2), type = "bar", barWidth = 20, 
  tooltipFormat = "{{offset:names}}",
  tooltipValueLookups = "{names: {0:'one', 1:'two', 2:'three'}}"
)

tooltip

1

There are 1 best solutions below

0
Arthur On

I figured it out!

The tooltipValueLookups argument can accept a list and the tooltipFormat argument can specify the relationship between the offset (x-axis value) and the named element of tooltipValueLookups.

sparklines are really cool but unfortunately as a user, one has to navigate both the R and JS documentation and sometimes guess how they relate.

library(sparkline)
sparkline(
  values = c(3, 5, 2), type = "bar", barWidth = 20, 
  tooltipFormat = "{{offset:names}}",
  tooltipValueLookups = list(names = c("one", "two", "three"))
)