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

irlmq6kh  于 2022-12-26  发布在  jQuery
关注(0)|答案(4)|浏览(115)

我正试图使谷歌折线图中有2行。
您应该能够打开和关闭(显示/隐藏)通过两个复选框。
任何人有任何想法显示,使这一点,或只是给予一些指示?
我猜会是一些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>
kmynzznz

kmynzznz1#

试试这个
标记:

<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>
bfnvny8b

bfnvny8b2#

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

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

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

o4hqfura

o4hqfura3#

最近,select事件的行为发生了变化,因此AbinayaSelvaraju的答案需要稍微修改一下

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

变成

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

fae0ux8s4#

我更新了Shinov T提供的解决方案,允许列的真实的切换(显示/隐藏),您可以在this 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);
}

相关问题