【发布时间】:2017-02-15 04:40:15
【问题描述】:
我正在尝试使用 AJAX 数据库中的数据更新 Google 图表。
当我选择一个选项时应该会发生更新。
但是,它并没有更新任何东西......
这是图表:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">google.charts.load('current', {'packages':['corechart']}); google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
<?php include("chartdata.php");?>
]);
var options = {
animation: {duration: 1000, easing: 'out', "startup": true},
vAxis: {minValue: 0},
pointSize: 4,
pointShape: 'circle'
};
var chart = new google.visualization.AreaChart(document.getElementById('chart_div'));
chart.draw(data, options);
// FILTER CHART
$("#chart-selector option").click(function() {
var selectname = $(this).val();
$.ajax({
url:"include/chartdata-filter.php",
method:"POST",
data:{select_name:selectname},
dataType:"text",
success:function(chart)
{
alert(chart); // WORKS
chart.draw(google.visualization.arrayToDataTable([chart]), options); // DOESN'T WORK
}
});
});}
这是选择字段。所以当我选择一个它应该更新图表:
<select id="chart-selector" name="select_name" onchange="chartSelect()">
<option>List Item 1</option>
<option>List Item 2</option>
</select>
当我选择其中一个选项时,我会在警报中正确获得输出。警报中的输出如下所示(来自 chart.php):
['Date', 'Column 1', 'Column 2'],
['2016-10-01', 0, 0],
['2016-10-02', 1, 2],
['2016-10-03', 3, 4],
['2016-10-04', 5, 6],
['2016-10-05', 7, 8],
['2016-10-06', 9, 10]
此外,当我手动将此数据粘贴到 AJAX 成功函数中时,它也可以正常工作:
chart.draw(google.visualization.arrayToDataTable([
['Date', 'Column 1', 'Column 2'],
['2016-10-01', 0, 0],
['2016-10-02', 1, 2],
['2016-10-03', 3, 4],
['2016-10-04', 5, 6],
['2016-10-05', 7, 8],
['2016-10-06', 9, 10]]), options); // THIS WORKS
但显然手动粘贴它没有任何意义,因为我想更新数据 onchange。
回顾一下:
我有一些选择。
当我选择其中一个选项时,应从数据库 (WORKS) 中提取数据。
-
数据在警报中正确提取,但未在图表中更新。当我手动粘贴代码时,它可以在更改中运行。所以问题可能出在此处:
chart.draw(google.visualization.arrayToDataTable([chart]), options);
有什么建议吗?
提前感谢任何有想法的人!
【问题讨论】:
标签: javascript php jquery ajax charts