隐藏启动Highcharts R上的特定行

问题描述 投票:0回答:1

我有一个数据框,其中有4个在启动Shiny应用程序时绘制的变量。但是,我想在启动时隐藏其中的一些,以便“价格”是绘制的唯一变量。我正在使用highchart程序包。

我相信我找到了所需的Java脚本代码,但不确定如何将其插入highchart函数。

下面是我的代码,当前输出与所需输出。数据在底部

data %>% hchart("line",  hcaes(x = date, y = Value, group = Variable))%>% hc_add_theme(hc_theme_ffx())

enter image description here

我想要的输出在启动时看起来像这样:enter image description here

这是我相信的JS代码适合我在某个地方的电话:

"series: [{
           name: 'Value',
           data: '[iv30, iv60, Hvol20]',
           visible: false
        }]"

这里是我的数据样本:

 data = structure(list(date = structure(c(18180, 18179, 18178, 18177, 
    18176, 18173, 18172, 18171, 18170, 18169, 18180, 18179, 18178, 
    18177, 18176, 18173, 18172, 18171, 18170, 18169, 18180, 18179, 
    18178, 18177, 18176, 18173, 18172, 18171, 18170, 18169, 18180, 
    18179, 18178, 18177, 18176, 18173, 18172, 18171, 18170, 18169
    ), class = "Date"), Variable = c("Hvol20", "Hvol20", "Hvol20", 
    "Hvol20", "Hvol20", "Hvol20", "Hvol20", "Hvol20", "Hvol20", "Hvol20", 
    "iv30", "iv30", "iv30", "iv30", "iv30", "iv30", "iv30", "iv30", 
    "iv30", "iv30", "iv60", "iv60", "iv60", "iv60", "iv60", "iv60", 
    "iv60", "iv60", "iv60", "iv60", "Price", "Price", "Price", "Price", 
    "Price", "Price", "Price", "Price", "Price", "Price"), Value = c(13.95, 
    13.63, 13.46, 13.26, 12.72, 12.65, 12.03, 11.99, 11.13, 10.31, 
    13.83, 16.31, 16.71, 17.91, 15.19, 15.08, 17.87, 18.48, 15.9, 
    13.75, 14.69, 16.52, 17.09, 18.16, 16.23, 15.75, 18, 18.9, 16.76, 
    15.63, 296.7, 293.24, 291.32, 288.67, 293.08, 294.4, 290.42, 
    288.06, 293.24, 296.77)), row.names = c(NA, -40L), class = c("grouped_df", 
    "tbl_df", "tbl", "data.frame"), groups = structure(list(Variable = c("Hvol20", 
    "iv30", "iv60", "Price"), .rows = list(1:10, 11:20, 21:30, 31:40)), row.names = c(NA, 
    -4L), class = c("tbl_df", "tbl", "data.frame"), .drop = TRUE))
r highcharts
1个回答
0
投票

我的做法不是完美的解决方案。实际上,这种解决方法有点有趣,因为您的要求在R中似乎很简单,但我不是R开发人员,我知道如何仅在R中使用JavaScript代码。

我刚刚编写了一个JavaScript函数,该函数会在首次加载图表时隐藏您的前3个系列。我使用JS(“”)函数将我的JavaScript代码注入R。

data %>% hchart("line",  hcaes(x = date, y = Value, group = Variable))%>% hc_add_theme(hc_theme_ffx()) %>%
hc_chart(
  events = list(
    load = JS("function() {
        this.series.forEach(function(series, i) {
        if (i < 3) {
            series.hide();
        }
      });
    }")
  )
)

它...有效:D,但是如果您需要更好的解决方案,则需要一个知道如何在R中解析数据的人。

© www.soinside.com 2019 - 2024. All rights reserved.