Search code examples
rr-highcharter

Highcharter dataLabels from different series


I have got the following code: It gives me a linechart with datalabels which correspond to the the values of the wt column. However I would like to display the values of the cyl series. Something like this: return(this.series.cyl.y)

What I actually want to archieve with my "real" dataset is to calculate the difference of the previous value of the same series, which seems to be not possible because this.point.y does not return an array, but returns the actual values. So the better solution would be to calculate a new series or column in R and use that column as dataLabel. Can someone help me?

library(highchart)

highchart() %>%
    hc_add_series(type = "line", data = mtcars$wt, name = "cyl", dataLabels = list(
        enabled = TRUE,
        formatter = JS(
            'function(){
            return this.point.y
            //return(this.series.cyl.y)
        }'
        )
    ))

Solution

  • Try using format within hc_add_series:

    highchart() %>%
      hc_add_series(type = "line", data = mtcars, name = "cyl", hcaes(y = wt), 
                    dataLabels = list(
                      enabled = TRUE,
                      format = "{point.cyl}"))