如何隐藏Google折线图?

时间:2015-07-03 04:24:18

标签: javascript codeigniter google-visualization

我在我的项目中使用Google折线图,根据数据显示不同的行。我想在点击它们的图例时显示/隐藏线条。

ListItem selectedListItem = ddCurrencyField.Items.FindByValue("YourDefaultValue");

    if (selectedListItem != null)
    {
        selectedListItem.Selected = true;
    };

3 个答案:

答案 0 :(得分:1)

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

使用' 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);
    }
});

答案 1 :(得分:-1)

使用此

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

Read this Answer as welljsfiddle preview

答案 2 :(得分:-1)

这就是我在单击其各自的图例标题时解决隐藏/显示行的问题。

                    /*****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();
                        }
                    }