user3812060
user3812060

Reputation: 118

How to hide/show lines in google chart?

I am new to google charts. I'm using Google line charts.

My Google line chart has 2 lines in it. I should be able to show/hide by using check boxes..

anyone got any idea show to make this??

Upvotes: 2

Views: 5401

Answers (2)

user6743866
user6743866

Reputation:

JAVA SCRIPT code to detect the checkbox change (I've stored the variable chart, data and option as a global variables, not in a function in order to use them in every functions):

$(document).ready(function() { 
        $('#id_of_the_checkbox').change(function () {
            if ($('#id_of_the_checkbox').is(':checked')) {
                var view = new google.visualization.DataView(data);
                view.setColumns([0,1,2]); //the number of the columns that you want to show
                chart.draw(view, options);
            }   
            else {
                var view = new google.visualization.DataView(data);
                view.hideColumns([2]); //the number of the columns that you want to hide
                chart.draw(view, options);
            }
        });
    });

HTML input checkbox code:

<input id="average" type="checkbox" name="average" value="Average" checked="checked"> Show my average consumption<br>

Google Chart DOC (setColumns): https://developers.google.com/chart/interactive/docs/reference#DataView_setColumns

Upvotes: 0

Abinaya Selvaraju
Abinaya Selvaraju

Reputation: 1292

Instead of having check box use the legend to hide/show the lines.

Refer to this answer

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

I hope this will help you!!

Upvotes: 2

Related Questions