我的代码如下:
var check = parametrTable.length -1;
var data = [];
$.each(parametrTable, function(i, x) { // NOTE: x = parametrTable[i]
$.getJSON("myurlwithparametr", function(json) {
$.each(json, function(j, d) {
data[j] = data[j] || {};
data[j]["count" + i] = d.count;
if (i == 0) {
data[j].category = d.column;
}
});
if(i == check){
loadChart(data);
}
});
});
此部分从我的amchart生成数据,接下来如果'i'=='check'(.each循环结束)我将生成数据发送到带有参数'data'的函数'loadChart'。 我的功能代码:
AmCharts.ready(function() {
chart = new AmCharts.AmSerialChart();
chart.dataProvider = data;
chart.categoryField = "category";
chart.startDuration = 1;
chart.type = "serial";
var categoryAxis = chart.categoryAxis;
categoryAxis.labelRotation = 90;
categoryAxis.axisAlpha = 1;
categoryAxis.insie = true;
categoryAxis.gridPosition = "start";
var valueAxis = new AmCharts.ValueAxis();
valueAxis.title = "title";
valueAxis.axisAlpha = 1;
chart.addValueAxis(valueAxis);
for (i=0;i<data.length;++i) {
var graph = new AmCharts.AmGraph();
graph.valueField = "count"+i+"";
graph.balloonText = "[[value]]";
graph.bullet = "round";
graph.type = "smoothedLine";
graph.lineAlpha = 1;
graph.connect = true;
graph.lineThickness = '2';
//graph.fillAlphas = 1;
chart.addGraph(graph);
}
chart.write('chartdiv');
});
在我的页面上,我的div为id ='chartdiv'。当我检查函数时,表(数据)中的值看起来没问题,但我的图表不会重新生成。 :(当我跳过
AmCharts.ready(function() {
生成图表,但不是所有值。任何sugestions?
答案 0 :(得分:0)
如果没有看到您的实际数据,很难分辨,但问题似乎是您如何创建图表。现在,您要为数据中的每个数据点创建单独的图表。这似乎不对。您应该为parametrTable
中的每个项目创建:
for (i=0;i<=check;++i) {
var graph = new AmCharts.AmGraph();
graph.valueField = "count"+i+"";
graph.balloonText = "[[value]]";
graph.bullet = "round";
graph.type = "smoothedLine";
graph.lineAlpha = 1;
graph.connect = true;
graph.lineThickness = '2';
//graph.fillAlphas = 1;
chart.addGraph(graph);
}
此外,jQuery AJAX调用是异步的。这意味着您的.each()
周期在加载实际数据之前完成。
您需要保留单独的迭代器以跟踪已加载项目的数量。即:
var check = parametrTable.length -1;
var loaded = 0;
var data = [];
$.each(parametrTable, function(i, x) { // NOTE: x = parametrTable[i]
$.getJSON("myurlwithparametr", function(json) {
loaded++;
$.each(json, function(j, d) {
data[j] = data[j] || {};
data[j]["count" + i] = d.count;
if (i == 0) {
data[j].category = d.column;
}
});
if(loaded == check){
loadChart(data);
}
});
});