jbkunst / highcharter

R wrapper for highcharts
http://jkunst.com/highcharter/
Other
720 stars 148 forks source link

helper: Enable automatically dataLabels to last value of each series #777

Open jbkunst opened 1 year ago

jbkunst commented 1 year ago
helper_dataLabel_last_value <- function(hc, series = "all", format = NULL){

  if(length(series) == 1 && series == "all") {
    ss <- map_chr(hc$x$hc_opts$series, pluck, "name")
  } else {
    ss <- series
  }

  reduce(ss, .init = hc, .f = last_value_one_series)

}

last_value_one_series <- function(hc, series = "PD"){

    ss <- map_chr(hc$x$hc_opts$series, pluck, "name")

    id <- which(ss == series)

    data <- hc$x$hc_opts$series[[id]]$data

    data[[length(data)]] <- last(data) |>
      append(
        list(
          dataLabels = list(
            # format = "{point.y:,.2f}",
            formatter = JS("function () { return parseFloat(this.y.toFixed(2)).toLocaleString(); }"),
            enabled = TRUE
          )
        )
      )

    hc$x$hc_opts$series[[id]]$data <- data

    hc

  }
stale[bot] commented 6 months ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions. Feel free to reopen it if you find it necessary.