I have a google chart code like below
<script type="text/javascript">
// Load the Visualization API and the piechart package.
google.load('visualization', '1.0', {'packages':['corechart']});
// Set a callback to run when the Google Visualization API is loaded.
google.setOnLoadCallback(drawChart);
// Callback that creates and populates a data table,
// instantiates the pie chart, passes in the data and
// draws it.
function drawChart() {
// Create the data table.
var data = new google.visualization.DataTable();
data.addColumn('string', 'Type');
data.addColumn('number', 'Total');
data.addRows([
['Damage', 3],
['Lost', 1]
]);
// Instantiate and draw our chart, passing in some options.
var chart = new
google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(data, options);
}
</script>
I want to insert with json api array like below
"status": 200,
"message": "OK",
"data": [
{
"_id": {
"report_type": "robbery"
},
"report_type": "robbery",
"Counts": 11
},
{
"_id": {
"report_type": "property_damage"
},
"report_type": "property_damage",
"Counts": 39
},
{
"_id": {
"report_type": null
},
"report_type": null,
"Counts": 2
}
]
I want to change type
and total
in the google chart above with report_type
and Counts
value in api.
I have tried write code below, but the results is not coming
function drawChart() {
$.ajax({
url: "api-url",
type: "GET",
success: function (data) {
var arrReport = [['Type', 'Total']];
$.each(data, function (index, value) {
arrReport.push([value.data.report_type, value.data.Counts]);
});
var options = {
'width':400,
'height':300
};
var figures = google.visualization.arrayToDataTable(arrReport)
var chart = new
google.visualization.PieChart(document.getElementById('chart_div'));
chart.draw(figures, google.visualization.PieChart(options));
}
});
}
Do you know where's the error from my code ?
Thank you