Search code examples
rhighchartsr-highcharter

How to add the "isSum" bar in Highchart Waterfall chart in R?


I followed this website to make a waterfall chart in R using Highcharts. But I am unable to add the "isSum" bar in the end. On how to do in .js is easily available but not for R.

I also tried to add a column in the df in the end by the name "isSum" and inserted true/false (after adding the cumulative sum row in the end) but it didn't work. Tried to do "isSum=T" under hc_series option also but didn't work.

An example is below:

df=data.frame(y=c(232,345,544,100),name=c("one","two","three","four"))    

highchart()%>%
  hc_chart(type="waterfall")%>%
  hc_add_series(df)

Output from above: enter image description here


Solution

  • My answer is not perfect because I am not an R programmer - maybe it can be done better. I don't know how to define data to include isSum property in R, but I know how to do it in JavaScript.

    I used JS() function in R that allows us to inject JavaScript code and I updated the last point this way:

    library(highcharter)
    
    df=data.frame(y=c(232,345,544,100),name=c("one","two","three","four"))    
    
    highchart()%>%
      hc_chart(type="waterfall", events = list(load = JS("function () {
              this.series[0].points[3].update({
                isSum: true
              });
      }")))%>%
      hc_add_series(df)
    

    edit I got it. You can define it in your data like this:

    df=data.frame(y=c(232,345,544,100),name=c("one","two","three","four"),isSum=c(FALSE, FALSE, FALSE, TRUE))