简体   繁体   English

Google图表从主要数据数组中删除数组

[英]Google charts remove array from inside the main array of data

I have the following script: 我有以下脚本:

  google.load("visualization", "1", {packages:["corechart"]});
  google.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses'],
      ['2004',  1000,      400],
      ['2005',  1170,      460],
      ['2006',  660,       1120],
      ['2007',  1030,      540],
      ['2009',  1030,      540],
      ['2011',  1,      677]
    ]);

    var options = {
      title: 'Company Performance',
      curveType: 'function',
      legend: { position: 'bottom' }
    };

    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
    chart.draw(data, options);

    function swicthGraphType(){
      $("input[type='radio']").click(function(){

        var thisValue = $(this).attr("data-lineType");

        console.log(data.Ad[0]);

        if(thisValue == ""){
          options.curveType = '';
        }
        if(thisValue == "function"){
          options.curveType = 'function';
        }

        chart.draw(data, options);


      });
    }

     swicthGraphType();

  }

I would like to create a click function which removes temporally one of the arrays for example: 我想创建一个单击函数,以暂时删除数组之一,例如:

data.Ad[0]

and put it back in the graph when I want. 并在需要时将其放回图表中。 Basically I would need a function which I can use to show what I need to see in the graph 基本上,我需要一个函数来显示需要在图中看到的内容

Solution A: 解决方案A:
Add a array variable var blackList = [] , 添加一个数组变量var blackList = []
write a filter function, function dataFilter() {} , which loop each element of your data, if it's not in the black list, then add it to a new array, finally, return the new array. 编写一个过滤器函数function dataFilter() {} ,该function dataFilter() {}循环数据的每个元素(如果不在黑名单中),然后将其添加到新数组中,最后返回新数组。
then use the new array to fill your chart instead of you original data. 然后使用新数组填充图表而不是原始数据。
you modify the black list variable, to decide which to filter or not. 您修改黑名单变量,以决定是否进行过滤。

Solution B (base on A): 解决方案B(基于A):
Use object instead of array for black list: 将对象而不是数组用于黑名单:

If you think modify the black list as an array is not easy, then you can create the black list as an object, use properties of object to store your black list, the name of property is year eg 2006, the value of property is your data of that year. 如果您认为将黑名单修改为数组不容易,则可以将黑名单创建为对象,使用对象的属性存储黑名单,属性名称为年份,例如2006,属性值为那年的数据。

Add/override a black list element: blackList[2006] = ['2006', 660, 1120]; 添加/覆盖黑名单元素: blackList[2006] = ['2006', 660, 1120];
Read/detect a black list element: blackList[2006] 读取/检测黑名单元素: blackList[2006]
Delete a black listelement: delete blackList[2006] 删除黑名单元素: delete blackList[2006]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM