chart.getSelection()无法与Google条形图一起正常使用 [英] chart.getSelection() does not work properly with google bar charts

查看:87
本文介绍了chart.getSelection()无法与Google条形图一起正常使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

drawBarChart = function (data) { 
//few statements goes here which sets options which are being passed to chartDraw i.e. t.options.chart.options

gChart = new google.visualization.BarChart(chartingPlace);
        //setTimeout(function () {
        // t.options.chart.options.height = ((t.chart.size.height) - 40) + "px";
        ////console.log(JSON.stringify(t.options.chart.options));

        //google.visualization.events.addListener(gChart, 'ready', function () {
            // grab a few details before redirecting
            google.visualization.events.addListener(gChart, 'select', function () {
                var selectedItem = gChart.getSelection()[0];
                console.log(gChart.getSelection());
                if (selectedItem) {
                    var topping = data.getValue(selectedItem.row, 0);
                    alert('The user selected ' + topping);
                }

           // });
        });
gChart.draw(data, t.options.chart.options);
}

我的应用程序具有许多针对不同情况显示的图表.我的要求是,点击Google条形图中的条形,然后打开一个与条形名称相关联的新标签.为此,我尝试在条形图上使用直接的选择"事件,如下所示:

My app having a number of charts displaying for different scenarios. My requirement is, click on a bar from google bar chart and open a new tab associated with the name of the bar. For this purpose, I tried using direct 'select' event on bar chart as follows:

google.visualization.events.addListener(gChart, 'select', function () {
                    var selectedItem = gChart.getSelection()[0];
                    console.log(gChart.getSelection());
                    if (selectedItem) {
                        var topping = data.getValue(selectedItem.row, 0);
                        alert('The user selected ' + topping);
                    }
});

但是我无法获得,它为gChart.getSelection()返回了空数组,所以我尝试了上面第一个代码中提到的带有"ready"事件的代码.它可以工作,但不能输出不一致.有时会给出空数组,有时会给出选定的对象.

But I could not get, this returned empty array for gChart.getSelection(), so I tried the code with 'ready' event mentioned in as first code above. It works, but not output is not consistent. sometimes it gives empty array and sometimes with selected object.

我仍然无法找到为什么它表现出这种行为

I am still not able to find why it is showing this kind of behavior

更多信息:我的应用程序具有不同的选项卡,在其上显示条形图,折线图,面积图,组合图的数量.getSelection()与折线图搭配使用效果很好,但无法获得条形图的一致输出.

More info: my application is having different tabs, showing number of bar,line,area,combo charts on it. getSelection() works well with line chart but not able to get the consistent output with bars.

任何帮助都是有意义的.

Any help is appreciable.

请不要将其标记为重复项,因为我已经经历了其他类似的问题,但是它们没有回答我的问题,我没有特权,因此我无法在回复中发表评论以要求更多的澄清.这里是类似的问题: barchart isStacked上的Google可视化-Click事件:true

Please do not mark it as duplicate, as I have gone through other similar questions but they does not answer my issue, I do not have that privilege so I could not comment in replies asking for more clarification. Similar question here : google visualization-Click event on barchart isStacked: true

请帮助!

提前谢谢!

更新:我无法从这里得到任何答案或任何回应.这真的非常令人失望.

Updates : I could not get any answer or any response from here. This is really very disappointing.

解决方案:这就是我解决此问题的方法:getSelection对我不起作用,所以我尝试了条文本上的click事件.通过单击事件,我可以在栏上看到文本,然后将其链接到新标签页.

Solution: This is how i worked out the solution for this problem : The getSelection could not work for me so i tried click event on bar text. Through the click event i could get the text on bar and i linked it to opn new tab.

如果有人需要代码中的解决方案,请告诉我.

If anyone need solution in code, please let me know.

谢谢.

推荐答案

请仔细阅读下面的代码段,我已更正,或者您可以说以不同的方式组织代码.如果您需要进一步的解释,请告诉我.

Please go through below code snippet, I corrected or you can say organised the code in different way. If you need further explanation please let me know.

google.visualization.events.addListener(googleChartObj, 'select', function () {
                var selectedBar = googleChartObj.getSelection();
                ////console.log(selectedBar);
                data.getRowLabel(selectedBar[0].row);
               /// for Bar charts, normally row-parameters are used
               /// manipulate on 'data' which refers to data-table to get desired results

            });

这篇关于chart.getSelection()无法与Google条形图一起正常使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆