Ruby on rails Amcharts错误地呈现数据
我有一个设置,我使用的是amcharts,它通过AJAX调用的appendData提供数据。调用转到一个URL,该URL使用函数2cos(X/2)+2ln(ln是行号)将Time.now呈现为X和8行。每1秒发出一次AJAX请求 后端总是正确的,并且总是返回一个单点,除非它是一个重复的X并在其中抛出错误。该错误导致无法完成,因此无法调用appendData 有人知道阿姆查特出了什么问题吗?这似乎只是appendData(我需要模拟滑动窗口)的问题 下面是Javascript代码。它假设页面创建了一个包含8个点的折线图,并将其传递给setup_chart_loader。Netcordia.rapid_poller.updateChart用于使用Ajax请求更新图表Ruby on rails Amcharts错误地呈现数据,ruby-on-rails,ajax,extjs,charts,Ruby On Rails,Ajax,Extjs,Charts,我有一个设置,我使用的是amcharts,它通过AJAX调用的appendData提供数据。调用转到一个URL,该URL使用函数2cos(X/2)+2ln(ln是行号)将Time.now呈现为X和8行。每1秒发出一次AJAX请求 后端总是正确的,并且总是返回一个单点,除非它是一个重复的X并在其中抛出错误。该错误导致无法完成,因此无法调用appendData 有人知道阿姆查特出了什么问题吗?这似乎只是appendData(我需要模拟滑动窗口)的问题 下面是Javascript代码。它假设页面创建了
Ext.ns("Netcordia.rapid_poller");
Netcordia.rapid_poller.refresh_rate = 1; //seconds
Netcordia.rapid_poller.pause = false; //causes the AJAX to suspend
Netcordia.rapid_poller.chart = null;
Netcordia.rapid_poller.stop = false;
/* This function does everything that is required to get the chart data correct */
Netcordia.rapid_poller.setup_chart_loader = function(chart){
assert(Netcordia.rapid_poller.displaySizeInMinutes,"No display size");
assert(Netcordia.rapid_poller.delta_url, "Data URL is empty");
assert(Netcordia.rapid_poller.delta_params, "No Data params");
if(typeof(chart) !== 'object'){
chart = document.getElementById(chart);
}
Netcordia.rapid_poller.chart = chart;
// 5 seconds raw polling
var maxPoints = Netcordia.rapid_poller.displaySizeInMinutes * 60 / 5;
var count = 0;
var lastUpdate = '';
debug("max number of points: "+maxPoints);
debug('creating updateChart function');
Netcordia.rapid_poller.updateChart = function(){
debug("Sending Data request");
var params = {last: lastUpdate, max: 1}; //maxPoints};
//I have to do this otherwise amcharts get a lot of data and only renders
// one item, then the counts is off
if(lastUpdate === ''){params['max'] = maxPoints;}
if (Netcordia.rapid_poller.pause){
alert("pausing");
params['historical'] = 1;
params['max'] = maxPoints;
}
Ext.apply(params, Netcordia.rapid_poller.delta_params);
//this might need to be moved to within the Ajax request
// incase things start piling up
if(!Netcordia.rapid_poller.stop){
setTimeout(Netcordia.rapid_poller.updateChart,1000*Netcordia.rapid_poller.refresh_rate);
} else {
debug("skipping next poll");
return;
}
Ext.Ajax.request({
url: Netcordia.rapid_poller.delta_url,
baseParams: Netcordia.rapid_poller.delta_params,
params: params,
success: function(response){
//if(Netcordia.rapid_poller.pause){
// debug("Data stopped");
// return;
//}
var json = Ext.util.JSON.decode(response.responseText);
lastUpdate = json.lastUpdate;
if( json.count === 0 ){
debug("no data to append");
return;
}
debug("appending "+json.count);
var remove = (count + json.count) - maxPoints;
if(remove <= 0){ remove = 0; }
count += json.count;
if(count > maxPoints){ count = maxPoints; }
debug("removing "+remove);
debug("count: "+count);
if(Netcordia.rapid_poller.pause){
alert("Pausing for historical");
//append a zero point and delete the existing data
// amcharts can leak extra points onto the screen so deleting
// twice the number is
chart.appendData("00:00:00;0;0;0;0;0;0;0;0",(count*2).toString());
count = json.count;
remove = 1;
Netcordia.rapid_poller.stop = true;
}
chart.appendData(json.lines.toString(),remove.toString());
}
});
};
};
似乎是Amcharts本身的一个缺陷 有开发者的答案
def get_delta
max = 1
begin
current = Time.parse(params[:last])
rescue
current = Time.now
end
if params[:historical]
max = params[:max].to_i || 10
current = Time.at(current.to_i - (max/2))
end
logger.info(current.to_i)
logger.info(max)
n = current.to_i
m = n+max-1
data = (n..m).collect do |x|
logger.info "For Point: #{x}"
point = Math.cos(x/2)
data = [Time.at(x).strftime("%H:%M:%S")]
for i in (1..8)
data.push(2*point+(2*i));
end
data.join(";")
end
render :json => {count: data.size, lastUpdate: Time.now.strftime('%Y-%m-%d %H:%M:%S'), lines: data.join("\n")}
end