jbkunst / highcharter

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

highchart drilldown option #134

Closed hmaeda closed 8 years ago

hmaeda commented 8 years ago

Hello,

I can't remember where I found this link, but was wondering, does the highchartr framework allow for something like the following link: [http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/drilldown/basic/]

to be included into a shiny app?

Any help on this would be much appreciated.

jbkunst commented 8 years ago

Hi @hmaeda ,

This example can help you http://rpubs.com/jbkunst/highcarter-drilldown. Hope it helps you.

hmaeda commented 8 years ago

Thank you that does help a lot. How would I get a more levels of drilldown as suggested might be possible here...http://jsfiddle.net/6LXVQ/2/

Also any suggestions about changing the chart title upon drilldown...i.e. as suggested in the answer here: http://stackoverflow.com/questions/25316424/change-title-when-drilldown-in-highcharts/25316669

jbkunst commented 8 years ago

The same way as the fiddle, check the example above.

About jsonlite::toJSON() I'm not sure if I understand the point. toJSON(mtcars) return a json object which can be a string instead of a list like list.parse

Example:

library(highcharter)

hc <- highchart() %>% 
  hc_plotOptions(
    series = list(
      boderWidth = 0,
      dataLabels = list(enabled = TRUE)
    )
  ) %>% 
  hc_chart(type = "column") %>% 
  hc_xAxis(type = "category") %>% 
  hc_add_series(
    name = "Things",
    data = list(
      list(name = "Animals", y = 5, drilldown = "animals")
      )
    )

hc 

hc <- hc %>% 
  hc_drilldown(
    series = list(
      list(
        id  = "animals",
        name = "Animals",
        data = list(
          list(name = "Cats", y = 4, drilldown = "cats"),
          list(name = "Dogs", y = 1)
        )
      ),
      list(
        id = "cats",
        data = list(
          list(name = "purr", y = 1),
          list(name = "miau", y = 2)
        )
      )
    )
  )

hc

jsonlite::toJSON(hc$x$hc_opts$series, pretty = TRUE, auto_unbox = TRUE)
jsonlite::toJSON(hc$x$hc_opts$drilldown, pretty = TRUE, auto_unbox = TRUE)
hmaeda commented 8 years ago

Thanks I realised that the previous example dataframes used value instead of y, preventing the multilevel drilldown when adapting your example that you published on rpubs. Did you have thoughts on how implement the changing chart title on the drilldown event?

jbkunst commented 8 years ago

I think this is solve here ;) http://stackoverflow.com/questions/25316424/change-title-when-drilldown-in-highcharts/25316669

The implemention is just copy paste but remember add JS("function() ... " ) in drilldownargument.

Tell me if you can do it.

hmaeda commented 8 years ago

I tried to take your suggested code and suggestions, but the result didn't look like it it worked.

Any suggestions?

Please see below for what I tried...

library(highcharter)

hc <- highchart() %>% 
  hc_plotOptions(
    series = list(
      boderWidth = 0,
      dataLabels = list(enabled = TRUE)
    )
  ) %>% 
  hc_chart(type = "column",
           events=list(drilldown=JS('function() {chart.setTitle({ text: "new_title" })}'))) %>% 
  hc_title(text = "Basic drilldown") %>%
  hc_xAxis(type = "category") %>% 
  hc_add_series(
    name = "Things",
    data = list(
      list(name = "Animals", y = 5, drilldown = "animals")
    )
  ) %>% 

  hc_drilldown(

    series = list(
      list(
        id  = "animals",
        name = "Animals",
        data = list(
          list(name = "Cats", y = 4, drilldown = "cats"),
          list(name = "Dogs", y = 1)
        )
      ),
      list(
        id = "cats",
        data = list(
          list(name = "purr", y = 1),
          list(name = "miau", y = 2)
        )
      )
    )
  )

hc
jwinter6 commented 8 years ago

Hi, I also have problems with changing the title, moreover I also would like to change the xAxis Categories. In case anyone has a working code snippet I would be really happy :) My code is similar to the one from @hmaeda.

jbkunst commented 8 years ago

Hi @hmaeda @jwinter6 ,

Sorry for the late response. I played some time with the example and I have some info.

  1. In the drilldown function the object is this instead of chart, so this.setTitle({text: 'whatever :D' }
  2. To cach the object wich you click you need to set function(e) instaed of function() then you access what you whant as e.seriesOptions.name or e.seriesOptions.id

Tell me please if this help you.

Aps, I dont know how it works if you need change title in a back operation (clicking the button), I guess this is documented somewhere X)

Example:

  hc_chart(
    type = "column",
    events = list(
      drilldown = 
        JS('function(e) {
             console.log(e.seriesOptions);
             this.setTitle({text: e.seriesOptions.name || e.seriesOptions.id })
           } '))) %>%  
jbkunst commented 8 years ago

@jwinter6

To change the axis title http://stackoverflow.com/questions/5880235/change-highcharts-axis-title but use the this instead of chart

Bryant-zhu commented 6 years ago

@jbkunst
thanks for the reply , as I run into the similar problem recently

I need not only change the title but also the label . more over I was wondering is there any way to change back the title as I click back to things after the drill down.

thanks