Skip to contents

Allows you to update the series array overriding the existing one.

Usage

ax_proxy_series(proxy, newSeries, animate = TRUE)

Arguments

proxy

A apexchartProxy htmlwidget object.

newSeries

New series to use, can be:

  • a list of series, each one being a list with at least a data element (and optionally name, type, ...): all series are replaced;

  • a single serie: a named list with a data element;

  • data for a single serie (e.g. the result of parse_df() or a numeric vector): all series are replaced by one serie (without name).

animate

Should the chart animate on re-rendering.

Examples


if (interactive()) {
  library(shiny)

  ui <- fluidPage(
    fluidRow(
      column(
        width = 8, offset = 2,
        tags$h2("Real time chart"),
        apexchartOutput(outputId = "chart")
      )
    )
  )

  server <- function(input, output, session) {

    rv <- reactiveValues()
    rv$df <- data.frame(
      date = Sys.Date() + 1:20,
      values = sample(10:90, 20, TRUE)
    )

    observe({
      invalidateLater(1000, session)
      df <- isolate(rv$df)
      # Append new line of data
      df <- rbind(
        df, data.frame(
          date = df$date[length(df$date)] + 1,
          values = sample(10:90, 1, TRUE)
        )
      )
      rv$df <- df
    })

    output$chart <- renderApexchart({
      # Generate chart once
      apex(isolate(rv$df), aes(date, values), "spline") %>%
        ax_xaxis(
          range = 10 * 24 * 60 * 60 * 1000
          # Fixed range for x-axis : 10 days
          # days*hours*minutes*seconds*milliseconds
        )
    })

    observe({
      # Update chart to add new data
      apexchartProxy("chart") %>%
        ax_proxy_series(
          parse_df(rv$df),
          T
        )
    })

    # To update several series, use a list of series, e.g.:
    # ax_proxy_series(proxy, list(
    #   list(name = "serie 1", data = parse_df(df1)),
    #   list(name = "serie 2", data = parse_df(df2))
    # ))

  }

  shinyApp(ui, server)
}