简体   繁体   English

Google Charts API集选择选择系列以突出显示

[英]Google charts API set selection choose series to highlight

Using the Google Charts API [https://developers.google.com/chart/interactive/docs/events] I have a properly formatted ComboChart and a properly formatted google rendered data table. 使用Google Charts API [https://developers.google.com/chart/interactive/docs/events],我具有格式正确的ComboChart和格式正确的Google呈现的数据表。

I am able to use the setSelection() function - However, the selection is highlighting my average line which runs through the middle of the bar chart. 我可以使用setSelection()函数-但是,选择突出显示了穿过条形图中间的平均线。

I am unable to work out how to make the highlighted 'dot' on the chart/graph area appear on the other series/data set (eg highlight the bars instead of the average line - which as per any average, is a straight line through the middle which means nothing to my end user). 我无法弄清楚如何使图表/图形区域上突出显示的“点”出现在其他系列/数据集上(例如,突出显示条而不是平均线-根据任何平均值,这是一条直线中间对我的最终用户而言毫无意义)。

I can add some code to a JS fiddle if you wish but it's really just a basic google combo chart displaying several different bars as my main data set and an average line as my series '1' (with base 0). 如果您愿意,我可以向JS小提琴中添加一些代码,但这实际上只是一个基本的Google组合图,显示几个不同的条形作为我的主要数据集,一条平均线显示为我的系列“ 1”(基数为0)。

Edit: add js fiddle: http://jsfiddle.net/GSryX/ 编辑:添加js小提琴: http : //jsfiddle.net/GSryX/

[code]
some code
[/code]

Any ideas? 有任何想法吗?

When setting the selection, make sure the "column" parameter of the selected object refers to the correct column in your DataTable. 设置选择时,请确保所选对象的“ column”参数引用了DataTable中的正确列。

Edit: 编辑:

If the bars are too small to show the selection effect, you can instead use a hack like this http://jsfiddle.net/asgallant/5SX8w/ to change the bar color on selection. 如果条形太小而无法显示选择效果,则可以改用http://jsfiddle.net/asgallant/5SX8w/这样的hack来更改选择条形。 This works best when you have only 1 series of data; 当您只有1系列数据时,这种方法效果最好。 if you have more than 1 series, it requires modification, and may not display properly unless you are using stacked bars. 如果您有1个以上的序列,则需要对其进行修改,除非您使用堆叠的条形,否则可能无法正确显示。

function drawChart() {
    var data = new google.visualization.DataTable();
    data.addColumn('string', 'Name');
    data.addColumn('number', 'Value');
    data.addRows([
        ['Foo', 94],
        ['Bar', 23],
        ['Baz', 80],
        ['Bat', 47],
        ['Cad', 32],
        ['Qud', 54]
    ]);

    var chart = new google.visualization.ChartWrapper({
        chartType: 'ColumnChart',
        containerId: 'chart_div',
        dataTable: data,
        options: {
            // setting the "isStacked" option to true fixes the spacing problem
            isStacked: true,
            height: 300,
            width: 600,
            series: {
                1: {
                    // set the color to change to
                    color: '00A0D0',
                    // don't show this in the legend
                    visibleInLegend: false
                }
            }
        }
    });

    google.visualization.events.addListener(chart, 'select', function () {
        var selection = chart.getChart().getSelection();
        if (selection.length > 0) {
            var newSelection = [];
            // if row is undefined, we selected the entire series
            // otherwise, just a single element
            if (typeof(selection[0].row) == 'undefined') {
                newSelection.push({
                    column: 2
                });
                chart.setView({
                    columns: [0, {
                        type: 'number',
                        label: data.getColumnLabel(1),
                        calc: function () {
                            // this series is just a placeholder
                            return 0;
                        }
                    }, 1]
                });
            }
            else {
                var rows = [];
                for (var i = 0; i < selection.length; i++) {
                    rows.push(selection[i].row);
                    // move the selected elements to column 2
                    newSelection.push({
                        row: selection[i].row,
                        column: 2
                    });
                }

                // set the view to remove the selected elements from the first series and add them to the second series
                chart.setView({
                    columns: [0, {
                        type: 'number',
                        label: data.getColumnLabel(1),
                        calc: function (dt, row) {
                            return (rows.indexOf(row) >= 0) ? null : {v: dt.getValue(row, 1), f: dt.getFormattedValue(row, 1)};
                        }
                    }, {
                        type: 'number',
                        label: data.getColumnLabel(1),
                        calc: function (dt, row) {
                            return (rows.indexOf(row) >= 0) ? {v: dt.getValue(row, 1), f: dt.getFormattedValue(row, 1)} : null;
                        }
                    }]
                });
            }
            // re-set the selection when the chart is done drawing
            var runOnce = google.visualization.events.addListener(chart, 'ready', function () {
                google.visualization.events.removeListener(runOnce);
                chart.getChart().setSelection(newSelection);
            });
        }
        else {
            // if nothing is selected, clear the view to draw the base chart
            chart.setView();
        }
        chart.draw();
    });

    chart.draw();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM