Show/hide lines/data in Google Chart

41,047

Solution 1

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>

Solution 2

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

Solution 3

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) {
    ...
}

Solution 4

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);
}
Share:
41,047

Related videos on Youtube

Bidstrup
Author by

Bidstrup

Marketing automation tech lead, with lots of experience in html coding email and mail platforms

Updated on April 15, 2020

Comments

  • Bidstrup
    Bidstrup about 4 years

    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>
    
  • Jeroen K
    Jeroen K about 10 years
    Thanks! I edited Abinaya Selvaraju's answer accordingly.
  • Rony SP
    Rony SP about 10 years
    @PhilippTsipman your JSFiddle is not working please check the updated : jsfiddle.net/xDUPF/53
  • dKen
    dKen almost 9 years
    This is the correct answer. OP clearly asked to switch series on and off again using checkboxes and the highest-rated answer does not do this.
  • dKen
    dKen almost 9 years
    -1. OP asked to use checkboxes for this, and this answer does not satisfy that requirement. The implementation of toggling series using checkboxes vs. a legend is largely different, and incompatible. This answer below is the correct approach for what is being asked. @abinaya, this is the second time I've seen you copy/paste this answer what was a question specifically about checkboxes; I don't think either actually answer the questions correctly.
  • Bogatyr
    Bogatyr over 8 years
    Not to mention, this solution is much easier to understand.
  • Shinov T
    Shinov T over 7 years
    @Arfan try chart.draw(data, options); again to redraw.