如何在myschart中实现mysql数据?
amchart是一个javascript,代码如下:
var chartData = [{ country: "USA", visits: 4252 },
{ country: "China", visits: 1882 },
{ country: "Japan", visits: 1809 },
{ country: "Germany", visits: 1322 },
{ country: "UK", visits: 1122 },
{ country: "France", visits: 1114 },
{ country: "India", visits: 984 },
{ country: "Spain", visits: 711 },
{ country: "Netherlands", visits: 665 },
{ country: "Russia", visits: 580 },
{ country: "South Korea", visits: 443 },
{ country: "Canada", visits: 441 },
{ country: "Brazil", visits: 395 },
{ country: "Italy", visits: 386 },
{ country: "Australia", visits: 384 },
{ country: "Taiwan", visits: 338 },
{ country: "Poland", visits: 328}];
我想将数据插入到chartData中,以便成功显示图表。
答案 0 :(得分:2)
您可以在服务器端查询数据库并操纵php数组。然后使用以下方法将php数组转换为服务器端的json数组。
json_encode(your php array);
然后将其传递给客户端(浏览器)。如果你不知道如何处理mysql数据到php数组请问。
答案 1 :(得分:1)
如果使用版本4
chart.dataSource.url = "path/to/yourdata.php";
chart.dataSource.parser = new am4core.JSONParser();
chart.dataSource.parser.options.emptyAs = 0;
如果使用版本3
"dataLoader": {
"url": "path/to/yourdata.php"
},
答案 2 :(得分:0)
从你的控制器你可以简单地返回数组中的mysql数据 data_array =从查询返回的mysql数据 并使用json发送此数组并将其用于您请求amcharts的javascript函数
并在您的javascript函数中使用以下代码 -
function create_chart(data_array){
$.each(data_array, function(key, value) {
dataSet = {
country: value1,
visits: value2
};
chartData.push(dataSet);
});
chart = new AmCharts.AmPieChart();
chart.dataProvider = chartData;
chart.titleField = "country";
chart.valueField = "visits";
chart.write("chartdiv");
}