How to make my chartData dynamic using my valid local file(json)?

↘锁芯ラ 提交于 2019-12-10 12:05:50

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!