ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

javascript – 如何在点击嵌入闪亮的googlevis折线图中的图例时隐藏系列

2019-06-29 06:33:12  阅读:234  来源: 互联网

标签:javascript r google-visualization shiny googlevis


我有一个使用googlevis包的嵌入式谷歌折线图的闪亮应用程序.我需要能够在点击它的图例键时隐藏一条线.我发现这个代码是关于如何在谷歌图表中做到这一点:

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

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

解决方法:

您可以通过插入一些额外的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.R和server.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

  })

})

标签:javascript,r,google-visualization,shiny,googlevis
来源: https://codeday.me/bug/20190629/1323886.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有