Show/hide lines/data in Google Chart

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

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

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>

Answers:

Answer

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

To get your required output check this code.

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

Answer

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

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

becomes

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

I updated the solution provided by Shinov T to allow real toggling (show/hide) of columns. You can see the result in this 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);
}

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.