在Google图表中显示/隐藏线条/数据 [英] Show/hide lines/data in Google Chart

查看:245
本文介绍了在Google图表中显示/隐藏线条/数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要制作一个包含2行的Google折线图。

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

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

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

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

My guess would be some onClick jQuery stuff?

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


推荐答案

试试

标记:

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

这篇关于在Google图表中显示/隐藏线条/数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆