如何隐藏谷歌折线图的线条

How to hide lines of Google line chart?

本文关键字:折线图 谷歌 何隐藏 隐藏      更新时间:2023-09-26

我在项目中使用谷歌折线图,它根据数据显示不同的线条。我希望在单击图例时显示/隐藏线条。

    function drawSalesGraph()
{
    if (sales_data_graph.length > 1)
    {
        graph_height = 500;
        var options_graph = {
            width: '1200',
            height:graph_height,
            colors: ['#ea6f09','#fb250d', '#0ac9c6', '#2680be', '#575bee','#6bd962','#ff0000','#000000'],
            fontSize : 10,
            pointSize : 10,
            legend: {'position': 'right'}
        };
        var data = new google.visualization.arrayToDataTable(sales_data_graph);
        $('#graph_sales_data').show();
    }
    else
    {
        var data = new google.visualization.DataTable();
        $('#graph_sales_data').hide();
    }
    // Create and draw the visualization.
    chart = new google.visualization.AreaChart(document.getElementById('graph_sales_data'));
    chart.draw(data, options_graph);
}

我找到了一些简单的解决方案,所以我在这里分享代码

使用系列选项的"lineDashStyle"属性是一个技巧:(

将lineDashStyle的第一个值设置为0,将第二个值设置成大于0的值(谷歌图表版本为45(

... prepare the data and option for chart
// draw chart
chart.draw(data, option);
// add event handler for legend click
google.visualization.events.addListener(chart, 'click', function (e) {
    var legendPrefix = 'legendentry#';
    // Check if clicked legend entry
    if (e.targetID.indexOf(legendPrefix) == 0) {
        // index of clicked legend entry
        var idx = e.targetID.substring(legendPrefix.length);
        // Show line
        if (option.series[idx].lineDashStyle && option.series[idx].lineDashStyle[0] == 0) {
            option.series[idx].lineDashStyle = option.series[idx].originalLineDashStyle;
        }
        // Hide line
        // ( Set the first value of lineDashStyle as 0,
        //   and second as something that greater than 0 )
        else {  
            option.series[idx].originalLineDashStyle = option.series[idx].lineDashStyle;
            option.series[idx].lineDashStyle = [0, 1];
        }
       chart.draw(data, option);
    }
});

使用此

vAxis: {
    ridlines: {
        color: 'transparent'
    },
    baselineColor: 'transparent'
},

阅读此答案或jsfiddle预览

这就是我如何解决单击相应图例标题时隐藏/显示行的问题。

                    /*****drawChart is used to Draw Graph.******/
                    function drawChart() {
                        if (sales_data_graph.length > 1)
                        {
                            $('#graph_sales_data').show();
                            var data = new google.visualization.arrayToDataTable(sales_data_graph);
                            // Instantiate and draw our chart, passing in some options.
                            var chart = new google.visualization.ChartWrapper({
                                chartType: 'LineChart',
                                containerId: 'graph_sales_data',
                                dataTable: data,
                                colors: ['#ea6f09', '#fb250d', '#0ac9c6', '#2680be', '#575bee', '#6bd962', '#ff0000', '#000000'],
                                options: {
                                    width: 1200,
                                    height: 500,
                                    fontSize: 10,
                                    pointSize: 10
                                }
                            });
                            // create columns array
                            var columns = [0];
                            /* the series map is an array of data series
                             * "column" is the index of the data column to use for the series
                             * "roleColumns" is an array of column indices corresponding to columns with roles that are associated with this data series
                             * "display" is a boolean, set to true to make the series visible on the initial draw
                             */
                            var seriesMap = [{
                                    column: 1,
                                    roleColumns: [1],
                                    display: true
                                }, {
                                    column: 2,
                                    roleColumns: [2],
                                    display: true
                                }, {
                                    column: 3,
                                    roleColumns: [3],
                                    display: true
                                }, {
                                    column: 4,
                                    roleColumns: [4],
                                    display: true
                                }, {
                                    column: 5,
                                    roleColumns: [5],
                                    display: true
                                }, {
                                    column: 6,
                                    roleColumns: [6],
                                    display: true
                                }, {
                                    column: 7,
                                    roleColumns: [7],
                                    display: true
                                }, {
                                    column: 8,
                                    roleColumns: [8],
                                    display: true
                                }];
                            var columnsMap = {};
                            var series = [];
                            for (var i = 0; i < seriesMap.length; i++) {
                                var col = seriesMap[i].column;
                                columnsMap[col] = i;
                                // set the default series option
                                series[i] = {};
                                if (seriesMap[i].display) {
                                    // if the column is the domain column or in the default list, display the series
                                    columns.push(col);
                                }
                                else {
                                    // otherwise, hide it
                                    columns.push({
                                        label: data.getColumnLabel(col),
                                        type: data.getColumnType(col),
                                        sourceColumn: col,
                                        calc: function() {
                                            return null;
                                        }
                                    });
                                    // backup the default color (if set)
                                    if (typeof(series[i].color) !== 'undefined') {
                                        series[i].backupColor = series[i].color;
                                    }
                                    series[i].color = '#CCCCCC';
                                }
                                for (var j = 0; j < seriesMap[i].roleColumns.length; j++) {
                                    //columns.push(seriesMap[i].roleColumns[j]);
                                }
                            }
                            chart.setOption('series', series);
                            function showHideSeries() {
                                var sel = chart.getChart().getSelection();
                                // if selection length is 0, we deselected an element
                                if (sel.length > 0) {
                                    // if row is undefined, we clicked on the legend
                                    if (sel[0].row == null) {
                                        var col = sel[0].column;
                                        if (typeof(columns[col]) == 'number') {
                                            var src = columns[col];
                                            // hide the data series
                                            columns[col] = {
                                                label: data.getColumnLabel(src),
                                                type: data.getColumnType(src),
                                                sourceColumn: src,
                                                calc: function() {
                                                    return null;
                                                }
                                            };
                                            // grey out the legend entry
                                            series[columnsMap[src]].color = '#CCCCCC';
                                        }
                                        else {
                                            var src = columns[col].sourceColumn;
                                            // show the data series
                                            columns[col] = src;
                                            series[columnsMap[src]].color = null;
                                        }
                                        var view = chart.getView() || {};
                                        view.columns = columns;
                                        chart.setView(view);
                                        chart.draw();
                                    }
                                }
                            }
                            google.visualization.events.addListener(chart, 'select', showHideSeries);
                            // create a view with the default columns
                            var view = {
                                columns: columns
                            };
                            chart.draw();
                        }
                        else
                        {
                            $('#graph_sales_data').hide();
                        }
                    }