DanG
DanG

Reputation: 741

Using Highcharts stock type feature for non-stock data (R)

I am working with a time-series which is not stock market data. I would like to use highcharter in R to make interactive visualization. I made a chart like this for the time being:.

dummy data

library(tidyverse)
library(highcharter)   


data(economics_long, package = "ggplot2")

economics_long2 <- filter(economics_long,
                          variable %in% c("pop", "uempmed", "unemploy"))

hchart(economics_long2, "line", hcaes(x = "date", y = "value01", group = "variable"))

I am wondering, is there any way to add a date filter at the top of this chart like the date filter in type = 'stock' chart in highcharter. Something similar to this picture: enter image description here

Upvotes: 1

Views: 875

Answers (1)

Darek Bienkowski
Darek Bienkowski

Reputation: 339

I think in basic solution you can create own widget / gadget. That is kind of start for it - fully functional - you can style it better for your purpose.

library(shiny)
library(miniUI)
library(highcharter)
library(tidyverse)


hightchart_filter <- function(data) {

  ui <- miniPage(
    miniContentPanel(

        # Dates ####
        dateInput("date_start", "start_date", value = "1900-01-01",   width = "25%"),
        dateInput("date_end", "end_date",  value = "2100-01-01", width = "25%"),

        # Highchart ####
        highchartOutput("high_plot", height = "500px")
      )
    )


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

    # update for data boxes
    updateDateInput(session, "date_start", value = data$date %>% min())
    updateDateInput(session, "date_end", value = data$date %>% max())

    # filter data
    data_filtered <- reactive({
      data %>% filter(between(date, input$date_start, input$date_end))
    })

    # Highchart ####
    output$high_plot <- renderHighchart({
      hchart(data_filtered(), "line", hcaes(x = "date", y = "value01", group = "variable"))
    })

  }

  runGadget(ui, server)
}

and run it :

hightchart_filter(economics_long)

Upvotes: 2

Related Questions