jQuery 在 Google 图表中显示/隐藏线条/数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17444586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:04:53  来源:igfitidea点击:

Show/hide lines/data in Google Chart

javascriptjqueryhtmlcharts

提问by Bidstrup

I'm trying to make a google line chart with 2 lines in it.

我正在尝试制作一个包含 2 行的谷歌折线图。

You should be able to turn them on and off(show/hide) by two checkboxes..

您应该能够通过两个复选框打开和关闭它们(显示/隐藏)。

Anyone got any idea show to make this, og just give some pointers?

任何人都有任何想法来制作这个,OG只是提供一些指示?

My guess would be some onClick jQuery stuff?

我的猜测是一些 onClick jQuery 的东西?

<html>
<head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      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]
        ]);
        var options = {
          title: 'Company Performance'
        };
        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

回答by Shinov T

try this

尝试这个

Mark up:

标记:

 <body>
   <div id="chart_div" style="width: 900px; height: 500px;"></div>

   <button type="button" id="hideSales"  >Hide Sales</button>
   <button type="button" id="hideExpenses"  >Hide Expence</button>

 </body>

Script:

脚本:

<script type="text/javascript">
  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]
    ]);
    var options = {
      title: 'Company Performance'
    };
    var chart = new google.visualization.LineChart(document.getElementById('chart_div'));

    chart.draw(data, options);


   var hideSal = document.getElementById("hideSales");
   hideSal.onclick = function()
   {
      view = new google.visualization.DataView(data);
      view.hideColumns([1]); 
      chart.draw(view, options);
   }
   var hideExp = document.getElementById("hideExpenses");
   hideExp.onclick = function()
   {
      view = new google.visualization.DataView(data);
      view.hideColumns([2]); 
      chart.draw(view, options);
   }


  }


</script>

回答by Abinaya Selvaraju

To get your required output check this code.

要获得所需的输出,请检查此代码。

        google.visualization.events.addListener(chart, 'select', function () {
            var sel = chart.getSelection();
            // if selection length is 0, we deselected an element
            if (sel.length > 0) {
                // if row is null, 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);
                }
            }
        });

Instead of having check box use the legend to hide/show the lines.

而不是让复选框使用图例来隐藏/显示线条。

Check this for the working sample: jqfaq.com

检查工作示例:jqfaq.com

回答by Pythonator

Recently the behavior of the selectevent changed so Abinaya Selvaraju's answer needs a slight fix

最近select事件的行为发生了变化,因此 Abinaya Selvaraju 的回答需要稍微修正

if (typeof sel[0].row === 'undefined') {
    ...
}

becomes

变成

if (sel[0].row == null) {
    ...
}

回答by Daniel

I updated the solution provided by Shinov T to allow real toggling (show/hide) of columns. You can see the result in this fiddle.

我更新了 Shinov T 提供的解决方案,以允许真正切换(显示/隐藏)列。你可以在这个 fiddle 中看到结果。

I added this code to save the current state of each column to allow toggleing:

我添加了此代码以保存每列的当前状态以允许切换:

var toggleSales = document.getElementById("toggleSales");
var salesHidden = false;
toggleSales.onclick = function() {
  salesHidden = !salesHidden;
  view = new google.visualization.DataView(data);
  if (salesHidden) {
    view.hideColumns([1]);
  }
  chart.draw(view, options);
}

var toggleExp = document.getElementById("toggleExpenses");
var expHidden = false;
toggleExp.onclick = function() {
  expHidden = !expHidden;
  view = new google.visualization.DataView(data);
  if (expHidden) {
    view.hideColumns([2]);
  }
  chart.draw(view, options);
}