Amcharts渲染数据不正确

时间:2009-09-15 21:48:16

标签: ruby-on-rails ajax extjs charts

我有一个设置,我使用的amcharts是通过AJAX调用的appendData提供数据。调用转到一个URL,它简单地使用函数2cos(x / 2)+ 2ln(ln是行号)将Time.now呈现为X和8行。 AJAX请求每1秒发出一次。

后端总是正确的,并且总是返回单个点,除非它是一个重复的X,它会抛出一个错误。该错误导致无法完成,因此不会调用appendData。

任何人都知道amcharts出了什么问题?它似乎仅与appendData有关(我需要模拟滑动窗口)。

Javascript代码如下。它假定页面创建一个带有8个点图的折线图并将其传递给setup_chart_loader。 Netcordia.rapid_poller.updateChart用于使用Ajax请求更新图表

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());
      }
    });
  };
};

返回数据的rails代码如下:

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

alt text

1 个答案:

答案 0 :(得分:1)

似乎是Amcharts本身的一个错误。

Forum Post有开发人员的答案。