代码之家  ›  专栏  ›  技术社区  ›  John K

AMCharts呈现数据不正确

  •  0
  • John K  · 技术社区  · 16 年前

    我有一个使用amcharts的设置,它通过ajax调用的appenddata提供数据。调用转到一个URL,该URL只呈现时间。现在,作为x和8行,使用函数2cos(x/2)+2ln(ln是行号)。Ajax请求每1秒发出一次。

    后端总是正确的,并且总是返回一个单点,除非它是一个重复的x,在其中引发错误。该错误导致未完成,因此不调用AppendData。

    有人知道amcharts出了什么问题吗?这似乎只是AppendData的问题(我需要模拟一个滑动窗口)。

    javascript代码如下。它假定页面创建一个带有8点图形的折线图,并将其传递给设置图表加载程序。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 回复  |  直到 16 年前
        1
  •  1
  •   John K    16 年前

    似乎是amcharts本身的一个bug。

    Forum Post 有开发者的答案。