问题
I'm looking for a way to drill down from different groups into multiple series using Highcharter for R. Essentially, I'd like to reproduce this example some in High Charts (from jsfiddle) - http://jsfiddle.net/wchmiel/v1o8dga9/3/
Here's some code that gets me close to this -
library(highcharter)
library(dplyr)
library(purrr)
df <- tibble(x = c(1, 2, 3, 2, 3, 4, 3, 5, 4), y = c(1, 2, 1, 3, 3, 2, 4, 6, 5), key = c(rep("A", 3), rep("B", 3), rep("C", 3)),
drilldown = c(rep("a", 3), rep("b", 3), rep("c", 3)))
drill1 <- data.frame(
x = c(1,2,3),
y = c(3, 3, 1)
)
drill1 <- list_parse2(drill1)
drill2 <- data.frame(
x = c(1,2,4),
y = c(1, 5, 1)
)
drill2 <- list_parse2(drill2)
drill3 <- data.frame(
x = c(1,2,4),
y = c(4, 3, 1)
)
drill3 <- list_parse2(drill3)
hchart(df, "line",
hcaes(x = x, y = y, group = key),
color = c("#A63A50", "#37123C", "#DDA77B"),
drilldown = TRUE) %>%
hc_plotOptions(line = list(marker = list(enabled = FALSE), legendIndex = 1)) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(
id = "a",
data = drill1
),
list(
id = "b",
data = drill2
),
list(
id = "c",
data = drill3
)
)
)
This script lets me drill down to three different charts, one for each group. However, each drill down chart has only one series. I cannot figure out how to have multiple series in the drill down charts.
It's important that the drill down charts include buttons to return to the main chart (as in my example and the jsfiddle example). Also, I need the ability to drill into different charts from different groups, not just one group.
So basically I need the jsfiddle example reproduced in highcharter. I suspect the solution involves some javascript.
回答1:
You can rewrite all from provided jsFiddle into R using JS("...") function.
Here is an example of it:
library(highcharter)
library(dplyr)
library(purrr)
df <- tibble(x = c(1, 2, 3, 2, 3, 4, 3, 5, 4), y = c(1, 2, 1, 3, 3, 2, 4, 6, 5), key = c(rep("A", 3), rep("B", 3), rep("C", 3)),
drilldown = c(rep("a", 3), rep("b", 3), rep("c", 3)))
drill1 <- data.frame(
x = c(1,2,3),
y = c(3, 3, 1)
)
drill1 <- list_parse2(drill1)
drill2 <- data.frame(
x = c(1,2,4),
y = c(1, 5, 1)
)
drill2 <- list_parse2(drill2)
drill3 <- data.frame(
x = c(1,2,4),
y = c(4, 3, 1)
)
drill3 <- list_parse2(drill3)
hchart(df, "line",
hcaes(x = x, y = y, group = key),
color = c("#A63A50", "#37123C", "#DDA77B"),
drilldown = TRUE) %>%
hc_chart(events = list(drilldown = JS("function(e) {
e.preventDefault()
var chart = this,
series = [{
data: [5, 5, 5, 3, 2]
}, {
data: [3, 3, 7, 3, 3]
}];
chart.addSingleSeriesAsDrilldown(e.point, series[0]);
chart.addSingleSeriesAsDrilldown(e.point, series[1]);
chart.applyDrilldown();
}"))) %>%
hc_plotOptions(line = list(marker = list(enabled = FALSE), legendIndex = 1)) %>%
hc_drilldown(
allowPointDrilldown = TRUE,
series = list(
list(
id = "a",
data = drill1
),
list(
id = "b",
data = drill2
),
list(
id = "c",
data = drill3
)
)
)
And simplified jsFiddle example: https://jsfiddle.net/BlackLabel/nx6czgwf
Now you just need to prepare your data and use code from your jsFiddle.
来源:https://stackoverflow.com/questions/57845118/drilldown-to-multiple-series-from-different-groups-highcharter-in-r