我已阅读解决方案this https://stackoverflow.com/questions/11376665/object-has-no-method-getcolumntype帖子但似乎不适合我的问题。我正在使用谷歌图表来构建折线图。 x 轴是日期,y 轴是整数。
当我运行代码时,我收到一条大错误消息
对象{“然后列出我所有的 JSON 格式数据”} 没有方法“getColumnType”。
我通过 AJAX 调用从 Web 服务获取 JSON 图表数据。到目前为止我的代码
<script type="text/javascript">
google.load("visualization", "1", { packages: ["corechart"] });
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "WebService.asmx/HelloWorld",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
async: false
}).responseText;
var options = {
title: 'Company Performance'
};
var chart = new google.visualization.LineChart(document.getElementById('chart_div1'));
chart.draw(jsonData, options);
}
</script>
解决方案很简单...您无法直接从 JSON 数据填充 google 图表。首先需要将 JSON 格式化为数据表。解决方案如下所示:
// Load the Visualization API and the piechart package.
google.load('visualization', '1', { 'packages': ['annotatedtimeline'] });
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
function drawChart() {
var jsonData = $.ajax({
url: "JSON.txt",
dataType: "json",
async: false
}).responseText;
// HERE IS THE FIX!!! Create our data table out of JSON data loaded from server.
var data = new google.visualization.DataTable(jsonData);
// Instantiate and draw our chart, passing in some options.
var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div1'));
chart.draw(data, { width: 400, height: 240 });
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)