21

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

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

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

我的猜测是一些 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>
4

4 回答 4

35

尝试这个

标记:

 <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 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>
于 2013-07-03T11:09:21.297 回答
23

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

        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);
                }
            }
        });

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

检查此工作示例:jqfaq.com

于 2013-07-03T10:58:18.170 回答
9

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

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

变成

if (sel[0].row == null) {
    ...
}
于 2013-11-27T00:38:39.233 回答
2

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

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

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);
}
于 2016-03-09T16:31:18.547 回答