防止某些项目在Google组合图视图中被过滤

时间:2018-08-14 00:27:44

标签: javascript charts google-visualization

我有一个类别过滤combochart,可以与以下代码完美配合:

var columnsTable = new google.visualization.DataTable();
        columnsTable.addColumn('number', 'colIndex');
        columnsTable.addColumn('string', 'colLabel');


        var initState= {selectedValues: []};
        // put the columns into this data table (skip column 0)
        for (var i = 1; i < data.getNumberOfColumns(); i++) {
            columnsTable.addRow([i, data.getColumnLabel(i)]);

            // you can comment out this next line if you want to have a default selection other than the whole list
            initState.selectedValues.push(data.getColumnLabel(i));
        }

          var chart = new google.visualization.ChartWrapper({
            chartType: 'ColumnChart',
            containerId: 'myPieChart2',
            dataTable: data,
            options: {

                height: 300,
                hAxis: {
                  title: 'Time'
                },
                vAxis: {
                  title: 'Flight Hours (min)'
                },
                title: 'Annual Flight Times by Pilot',
                bar: {groupWidth: '90%'},
                seriesType: 'bars',
                series: {
                    0: {type: 'line',
                        lineDashStyle: [4, 4]},
                    1: {type: 'line',
                        lineDashStyle: [14, 2, 2, 7]},
                }

                //theme: 'material'

            }
        });

      var columnFilter = new google.visualization.ControlWrapper({
            controlType: 'CategoryFilter',
            containerId: 'colFilter_div2',
            dataTable: columnsTable,
            options: {
                filterColumnLabel: 'colLabel',
                ui: {
                    label: 'Pilots',
                    allowTyping: false,
                    allowMultiple: true,
                    allowNone: false,
                    //selectedValuesLayout: 'belowStacked'
                }
            },
            state: initState
        });

              function setChartView () {
            var state = columnFilter.getState();
            var row;
            var view = {
                columns: [0]
            };
            for (var i = 0; i < state.selectedValues.length; i++) {
                row = columnsTable.getFilteredRows([{column: 1, value: state.selectedValues[i]}])[0];
                view.columns.push(columnsTable.getValue(row, 0));
            }
            // sort the indices into their original order
            view.columns.sort(function (a, b) {
                return (a - b);
            });
            chart.setView(view);
            chart.draw();
        }
        google.visualization.events.addListener(columnFilter, 'statechange', setChartView);

        setChartView();
        columnFilter.draw();

    });

在我的数据中,前两列(系列0和系列1)属于相关月份的总计和平均值。因此,我想使它们恒定,这意味着它们无法关闭(参见图片)。

或者,如果有一种方法可以将它们从下拉列表和过滤选项中排除,但仍显示在图表上,则可以接受。最后,我想阻止用户关闭这些项目。

我该怎么做?我花了两个小时,但还是被卡住了。

谢谢您的帮助。

Total and Average columns can be closed by clicking the X buttons

0 个答案:

没有答案