I am trying to trying to draw Pie chart using nvd3 JavaScript library. It worked fine when tested with hard coded data. But what I want is to be able to fetch dynamic data from the server via php and Ajax. This has not been possible. Can someone kindly point me in the right direction? Thanks
jquery:
function chartdata() {
$.post('chartData.php', {
id : $("#id").val(),
theDate : $(".selectedDate").text()
}, function(data) {
var testdata = JSON.stringify(data);
alert(testdata);
console.log(testdata)
}, 'json');
};
nvd3:
nv.addGraph(function() {
var width = 250, height = 250;
var chart = nv.models.pieChart().x(function(d) {
if (d.Code === "1") {
d.Code = "Good"
} else if (d.Code === "2") {
d.Code = "Error"
}
return d.Code;
}).y(function(d) {
return d.ReasonCount;
}).width(width).height(height).showLabels(false).labelThreshold(.05)//Configure the minimum slice size for labels to show up
.labelType("percent")//Configure what type of data to show in the label. Can be "key", "value" or "percent"
.donut(true)//Turn on Donut mode. Makes pie chart look tasty!
.donutRatio(0.45)//Configure how big you want the donut hole size to be.
;
d3.select(".test2").datum(chartdata()).transition().duration(1200).attr('width', width).attr('height', height).call(chart);
chart.dispatch.on('stateChange', function(e) {
nv.log('New State:', JSON.stringify(e));
});
return chart;
});
Sample JSON:
[{"Code":"1","ReasonCount":"8"},{"Code":"2","ReasonCount":"1"}, ]