问题
I have read the solution on this post but cannot seem to fit it to my issue. I am using google charts to build a line chart. x-axis being a date, y-axis being an integer.
When I run my code I get a large error message
Object {"it then lists all my JSON formatted data"} has no method 'getColumnType'.
I am getting my JSON chart data from a web service via an AJAX call. My code so far
<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>
回答1:
the solution is simple....You cannot fill a google chart directly from JSON data. The JSON first needs to be formatted into a datatable. the solution looks like this:
// 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 });
}
回答2:
I think you need to explicitly parse the JSON; $.ajax()
won't do that for you in this case.
Try adding:
jsonData = JSON.parse(jsonData);
before you build the chart.
Now, that said, I don't know what it is that's looking for a getColumnType()
function; that's not going to be there on your JSON response in either string form or parsed form.
来源:https://stackoverflow.com/questions/13411545/object-has-no-method-getcolumntype-error