本文介绍了深入研究来自不同组的多个系列-Highcharter(在R中)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种使用Highcharter for R从不同组向下钻取到多个系列的方法.本质上,我想在High Charts中重现此示例(来自jsfiddle)- http://jsfiddle.net/wchmiel/v1o8dga9/3/

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.

重要的是,向下钻取图表包括返回主图表的按钮(在我的示例和jsfiddle示例中).另外,我需要能够深入研究来自不同组而不只是一组的不同图表.

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.

所以基本上我需要用highcharter复制的jsfiddle示例.我怀疑该解决方案涉及一些javascript.

So basically I need the jsfiddle example reproduced in highcharter. I suspect the solution involves some javascript.

推荐答案

您可以使用 JS("...")函数将提供的jsFiddle全部重写为R.

You can rewrite all from provided jsFiddle into R using JS("...") function.

这里是一个示例:

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
      )
    )
  )

简化的jsFiddle示例: https://jsfiddle.net/BlackLabel/nx6czgwf

And simplified jsFiddle example: https://jsfiddle.net/BlackLabel/nx6czgwf

现在,您只需要准备数据并使用jsFiddle中的代码即可.

Now you just need to prepare your data and use code from your jsFiddle.

这篇关于深入研究来自不同组的多个系列-Highcharter(在R中)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 12:45