简体   繁体   English

Google Charts HTML工具提示中带有javascript onclick事件吗?

[英]Google Charts HTML tooltip with javascript onclick event inside of it?

I have been trying to have a click event in the Google charts tooltip with chart option isHtml: true . 我一直在尝试通过图表选项isHtml: true在Google图表工具提示中发生click事件。 So far, I have tried two ways to get this done but without success. 到目前为止,我已经尝试了两种方法来完成此任务,但没有成功。

1) Writing an onclick function by adding a button in the tooltip. 1)通过在工具提示中添加按钮来编写onclick函数。 But, whenever I click on the button, I get the following error "Uncaught Reference - function not defined". 但是,每当我单击按钮时,都会出现以下错误“未捕获的引用-未定义函数”。 I tried placing the function almost everywhere in the directive but the code doesn't seem to pick that up. 我尝试将函数放置在指令中的几乎所有位置,但是代码似乎没有意识到这一点。

HTML code in the tooltip: 工具提示中的HTML代码:

'<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>

exportCSV function: exportCSV函数:

var exportCSV = function(){
    console.log("Function Triggered");
}

2) Adding chart.setAction() in the google charts. 2)在Google图表中添加chart.setAction()。 But the problem here is, I have isHtml: True in chart options. 但是这里的问题是,我有isHtml: True在图表选项中为isHtml: True When I try using the following code it doesn't seem to do anything. 当我尝试使用以下代码时,它似乎没有任何作用。

chart.setAction({ id: 'export', text: 'Export CSV', action: function() { selection = chart.getSelection(); console.log(selection); } });

But, when I tried replacing function action with enabled in chart.setAction, the code returns the object when I click on the column or the Bar chart but not on the export data button in the tooltip. 但是,当我尝试在chart.setAction中enabled功能替换功能action时,当我单击列或条形图而不是工具提示中的导出数据按钮时,代码返回对象。

All I need is to capture the click event in the tooltip. 我所需要的只是捕获工具提示中的click事件。 It would be great if someone could help me out on this issue. 如果有人可以帮助我解决这个问题,那就太好了。

Thanks! 谢谢!

I think you just need to define exportCSV in global scope, 认为您只需要在全局范围内定义exportCSV
see following example... 请参阅以下示例...

Also, without tooltip {trigger: 'selection'} in the chart options , 另外,在图表options没有tooltip {trigger: 'selection'}
I can't seem to click the tooltip before it disappears. 我似乎无法在工具提示消失之前单击它。
must click pie slice to see tooltip... 必须点击饼图以查看工具提示...

 google.charts.load('current', { callback: function () { var data = google.visualization.arrayToDataTable([ ['Genre', 'Percentage of my books', {role: 'tooltip', type: 'string', p: {html:true}}], ['Science Fiction', 217, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['General Science', 203, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['Computer Science', 175, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['History', 155, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['Economics/Political Science', 98, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['General Fiction', 72, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['Fantasy', 51, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'], ['Law', 29, '<h5 style="padding-left:.66em;" id="export" href="#" onclick="exportCSV()">Export CSV</h5>'] ]); var chart = new google.visualization.PieChart(document.getElementById('chart_div')); var options = { height: 400, tooltip: { isHtml: true, trigger: 'selection' }, width: 600 }; chart.draw(data, options); }, packages: ['corechart'] }); var exportCSV = function(){ alert("Function Triggered"); } 
 <script src="https://www.gstatic.com/charts/loader.js"></script> <div id="chart_div"></div> 

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

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