问题
I have a pieChart(sample working link below), but what I want is to get my chartData from my local file so it would not be hard coded. I'm having difficulty in this one.
Note that I have valid JSON file, which I test and it works.
When I did is to assign my JSON file to the var chartData = "validJsonFile.json"
, it is not working. my chart displays nothing at all.
I hope someone can help me. Thanks.
Sample Working ChartLink is here
the thing i tried but didnt work:
var chartData = "validjsonfile.json";
the thing that works, but it is not dynamic.
var chartData = [{
"title": "Marketing",
"value": 23,
"url":"#",
"description":"click to drill-down",
"data": [
{ "title": "Jan", "value": 1, "data": [
{ "title": "AAA", "value": 2 },
{ "title": "BBB", "value": 5 },
{ "title": "CCC", "value": 1 },
] }, /* and so on...... */
Please help.. Thanks you so much in advance...
回答1:
Well, after so many tries i found out an answer.. haha! I'm so happy..
var chartData;
AmCharts.loadFile("dataPieJson.json", {}, function(data) {
var chartData = AmCharts.parseJSON(data);
console.log(chartData); // this will output an array
chart.dataProvider = chartData;
chart.validateData();
});
// create pie chart
var chart = AmCharts.makeChart("chartdiv", {
"type": "pie",
"dataProvider": chartData,
"valueField": "value",
"titleField": "title",
"labelText": "[[title]]: [[value]]",
"pullOutOnlyOne": true,
"titles": [{
"text": "Most Sold Products"
}],
"allLabels": []
});
来源:https://stackoverflow.com/questions/40903881/how-to-make-my-chartdata-dynamic-using-my-valid-local-filejson