【问题标题】:How to hide the series when clicking a legend in googlevis line chart that is embedded in shiny单击嵌入闪亮的googlevis折线图中的图例时如何隐藏系列
【发布时间】:2014-01-10 22:08:53
【问题描述】:

我有一个闪亮的应用程序,它使用 googlevis 包嵌入了谷歌折线图。单击它的图例键时,我需要能够隐藏一行。我在谷歌图表中找到了如何做到这一点的代码:

$http://jsfiddle.net/xDUPF/4/light/$

如何将此行为引入使用闪亮创建的图形?我可以使用“jscode”参数吗?

【问题讨论】:

    标签: javascript r google-visualization shiny googlevis


    【解决方案1】:

    您可以通过插入一些额外的 javascript 代码来实现这一点。该技术显示为here。当您调用 gvisLineChart 并将其分配给 x 时,它会返回一个列表。您可以检查以下内容

    x$html$chart[['jsDrawChart']]
    

    它会返回类似

    的东西
    // jsDrawChart
    function drawChartyourid() {
    var data = gvisDatayourid();
    var options = {};
    options["allowHtml"] = true;
    options["series"] = [{targetAxisIndex: 0},
                                                  {targetAxisIndex:1}];
    options["vAxes"] = [{title:'val1'}, {title:'val2'}];
    
    
        var chart = new google.visualization.LineChart(
        document.getElementById('yourid')
        );
        chart.draw(data,options);    
    }
    

    您可以调整这段 javascript 代码来实现您的目标。例如这里是 ui.Rserver.R。结果可以查看http://spark.rstudio.com/johnharrison/gvisTest

    # ui.R
    
    library(shiny)
    
    shinyUI(pageWithSidebar(
      headerPanel("Hello Shiny!"),
    
      sidebarPanel("Sidebar"),
      mainPanel("Main",
                htmlOutput('gtest'))
    )
    
    )
    
    
    # server.R
    library(shiny)
    library(googleVis)
    
    shinyServer(function(input, output) {
    
      output$gtest <- renderGvis({
        df <- data.frame(country=c("US", "GB", "BR"), val1=c(1,3,4), val2=c(23,12,32))
        gt <- gvisLineChart(df, xvar="country", yvar=c("val1", "val2"),
                            options=list(title="Hello World",
                                         titleTextStyle="{color:'red',fontName:'Courier',fontSize:16}",
                                         curveType='function'),chartid = "yourid"
        )
        jsInsert <- "var columns = [];
        // display these data series by default
        var defaultSeries = [1,2,3];
        var series = {};
        for (var i = 0; i < data.getNumberOfColumns(); i++) {
            if (i == 0 || defaultSeries.indexOf(i) > -1) {
                // if the column is the domain column or in the default list, display the series
        columns.push(i);
      } else {
        // otherwise, hide it
        columns[i] = {
        label: data.getColumnLabel(i),
        type: data.getColumnType(i),
        calc: function () {
        return null;
        }
        };
      }
        if (i > 0) {
        // set the default series option
        series[i - 1] = {};
        if (defaultSeries.indexOf(i) == -1) {
        // backup the default color (if set)
        if (typeof (series[i - 1].color) !== 'undefined') {
        series[i - 1].backupColor = series[i - 1].color;
        }
        series[i - 1].color = '#CCCCCC';
        }
        }
    }
    options['series'] = series;
    
    function showHideSeries () {
      var sel = chart.getSelection();
      // if selection length is 0, we deselected an element
        if (sel.length > 0) {
        // if row is undefined, we clicked on the legend
        if (sel[0].row == null) {
        var col = sel[0].column;
        if (columns[col] == col) {
        // hide the data series
        columns[col] = {
        label: data.getColumnLabel(col),
        type: data.getColumnType(col),
        calc: function () {
        return null;
        }
        };
    
        // grey out the legend entry
        series[col - 1].color = '#CCCCCC';
        }
        else {
        // show the data series
        columns[col] = col;
        series[col - 1].color = null;
        }
        var view = new google.visualization.DataView(data);
        view.setColumns(columns);
        chart.draw(view, options);
        }
        }
      }
    
        google.visualization.events.addListener(chart, 'select', showHideSeries);
        chart.draw(data,options);
        "
        gt$html$chart[['jsDrawChart']] <- gsub("chart.draw\\(data,options\\);", jsInsert, gt$html$chart[['jsDrawChart']])
        gt
    
      })
    
    })
    

    【讨论】:

    • 这很好用!谢谢,只有最后你需要做而不是“chart.draw(数据,选项);” ,我们使用:\n var view = new google.visualization.DataView(data); view.setColumns(列); chart.draw(view, options);
    • @jdharrison 当没有为该 googleVis 图表设置自定义工具提示时,此解决方案非常有效。当我添加自定义工具提示并单击图例时,出现以下错误:给定轴上的所有系列必须具有相同的数据类型。修改 javascript 代码以允许自定义工具提示是否可行?
    • @jdharrison 此外,此解决方案将您可能分配给系列的颜色修改为默认的 googleVis 颜色。是否可以修改代码以允许自定义颜色?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-31
    • 1970-01-01
    • 2023-03-24
    相关资源
    最近更新 更多