我正在尝试制作一个图表,其中的数据可以通过各种下拉菜单和日期选择器进行选择。我似乎找不到在单击事件时在图表中传递新数据的方法。到目前为止,我让它工作到了onClick,它绘制了一个全新的图表。但在我看来这不是解决问题的办法。
那么有没有其他方法可以做到这一点呢?HTML:
<div id="piechart" style="width: 450px; height: 500px;"></div>
<div class="date-selector-container">
<div class="btn-group">
<button type="button" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-expanded="false">
Jaar <span class="caret"></span>
</button>
<ul class="dropdown-menu" role="menu">
<li><a class="2015-btn" href="#">2015</a></li>
<li><a href="#">2014</a></li>
<li><a href="#">2013</a></li>
</ul>
</div>JS:
google.load("visualization", "1", {packages:["corechart"], "callback": drawChart});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 1],
['Eat', 22],
['Commute', 32],
['Watch TV', 42],
['Sleep', 75]
]);
var options = {
chartArea: {width:'100%',height:'100%'},
forceIFrame: 'false',
is3D: 'true',
pieSliceText: 'value',
sliceVisibilityThreshold: 1/20, // Only > 5% will be shown.
titlePosition: 'none'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
});
//On button click, load new data
$(".2015-btn").click(function(){
google.load("visualization", "1", {packages:["corechart"], "callback": drawChart});
google.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
chartArea: {width:'100%',height:'100%'},
forceIFrame: 'false',
is3D: 'true',
pieSliceText: 'value',
sliceVisibilityThreshold: 1/20, // Only > 5% will be shown.
titlePosition: 'none'
};
var chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
});发布于 2015-06-04 18:43:29
将js更改为如下所示。
在drawChart函数之外创建图表变量,使用已有的图表,而不是创建新的图表。
此处的工作示例jsfiddle
google.load("visualization", "1", {packages:["corechart"], "callback": drawChart});
google.setOnLoadCallback(drawChart);
var chart;
function drawChart() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 1],
['Eat', 22],
['Commute', 32],
['Watch TV', 42],
['Sleep', 75]
]);
var options = {
chartArea: {width:'100%',height:'100%'},
forceIFrame: 'false',
is3D: 'true',
pieSliceText: 'value',
sliceVisibilityThreshold: 1/20, // Only > 5% will be shown.
titlePosition: 'none'
};
chart = new google.visualization.PieChart(document.getElementById('piechart'));
chart.draw(data, options);
}
$(document).ready(function(){
//On button click, load new data
$(".2015-btn").click(function() {
var data = google.visualization.arrayToDataTable([
['Task', 'Hours per Day'],
['Work', 11],
['Eat', 2],
['Commute', 2],
['Watch TV', 2],
['Sleep', 7]
]);
var options = {
chartArea: { width: '100%', height: '100%' },
forceIFrame: 'false',
is3D: 'true',
pieSliceText: 'value',
sliceVisibilityThreshold: 1 / 20, // Only > 5% will be shown.
titlePosition: 'none'
};
chart.draw(data, options);
});
});https://stackoverflow.com/questions/30640728
复制相似问题