gpt4 book ai didi

javascript - 谷歌图表 - 绘制后运行功能

转载 作者:行者123 更新时间:2023-11-29 14:39:59 25 4
gpt4 key购买 nike

使用谷歌图表,在绘制完成后尝试从图表中获取值,我知道我需要制作一个回调函数,该函数将在“绘制”完成后运行 - 但我无法让它工作...我在哪里调用函数“afterDraw”以使其在函数“draw”完成后运行?请帮忙..谢谢:)

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addColumn('number', 'Stam');
data.addRows([
['Mushrooms', 3,5],
['Onions', 1,7],
['Olives', 1,3],
['Zucchini', 1,5],
['Pepperoni', 2,8]
]);

var options = {'title':'How Much Pizza I Ate Last Night',
'height':500};

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));
chart.draw(data, options);

}
function afterDraw(){
alert('all done');
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div id="chart_div"></div>

最佳答案

在创建图表之后和绘制之前

'ready' 事件添加监听器

这会在绘制图表时通知您

google.visualization.events.addListener(chart, 'ready', afterDraw);

请参阅以下工作片段...

google.charts.load('current', {'packages':['corechart']});
google.charts.setOnLoadCallback(drawChart);

function drawChart() {
var data = new google.visualization.DataTable();
data.addColumn('string', 'Topping');
data.addColumn('number', 'Slices');
data.addColumn('number', 'Stam');
data.addRows([
['Mushrooms', 3,5],
['Onions', 1,7],
['Olives', 1,3],
['Zucchini', 1,5],
['Pepperoni', 2,8]
]);

var options = {'title':'How Much Pizza I Ate Last Night',
'height':500};

var chart = new google.visualization.ColumnChart(document.getElementById('chart_div'));

google.visualization.events.addListener(chart, 'ready', afterDraw);

chart.draw(data, options);

}
function afterDraw(){
console.log('all done');
}
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<div id="chart_div"></div>

关于javascript - 谷歌图表 - 绘制后运行功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39719846/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com