2013-08-02 33 views
0

我的任務是動態添加系列並不斷更新它們的數據,這些數據由ajax調用接收。 我知道系列可以動態添加,通過聲明全局的highchart功能。並使用series.addseries()函數,還可以使用settimout請求更新ajax調用並使用series.addpoint()函數更新點。如何動態添加多個系列並動態更新其數據

我已經完成了兩個單獨的工作。但是當我結合這兩種技術時,數據不會添加到高圖中。我在這方面做了大量的研究,而且我沒有找到不添加數據的理由。事實上腳本掛起瀏覽器。 我已檢查了系列對象,它顯示處理x數據和y數據。只有區別我發現isDirty字段和isDirtydata字段設置爲true。不知道原因。 這裏是完整的代碼

var serverUrl = 'http://'+window.location.hostname+':8000' 
Highcharts.setOptions({ 
    global: { 
     useUTC: false 
    } 
}); 
data={} 
$(document).ready(function(){ 

    $(function() { 
     console.log("highcharts") 
     $('#first').highcharts({ 
       chart: { 
        type: 'spline', 
        //marginRight: 150, 
        marginBottom: 5, 
        events: { 
         load: requestData(data) 
          } 
       }, 
       title: { 
        text: 'Server Monitroting Tool' 
       }, 
       subtitle: { 
        text: 'Cpu usage, Memory Usage,Disk Usage,Mongo Usage' 
       }, 
       xAxis: { 
       type: 'datetime', 
       categories: ['TIME'], 
        dateTimeLabelFormats : { 
        hour: '%I %p', 
        minute: '%I:%M %p' 
       } 
       }, 
       yAxis:{ 
        showEmpty:false 
       }, 

        legend: 
        { 
        backgroundColor: '#F5F5F5', 
        layout: 'horizontal', 
        floating: true, 
        align: 'left', 
        verticalAlign: 'bottom', 
        x: 60, 
        y: 9, 
        shadow: false, 
        border: 0, 
        borderRadius: 0, 
        borderWidth: 0 
       }, 
       series: {} 
      }); 
      }); 
    from_date=new Date().getTime()-60000; 

    function requestData(data) 
    { 
     if(!data) 
     { 
      console.log("default ") 
     } 
     else 
     { 
     console.log("requesting") 
     $.ajax({ 
      url:serverUrl+'/api/fetch_params/', 
      type:'GET', 
      data:data, 
      success:function(response) 
      { 
      console.log("in success") 
      //data = {'type':TypeOfParameter,'hostname':hostname,'sub-type':sub_type,'param':sub_type_parameter,'from-date':from_date} 
      var id=data['sub-type']+data['param'] 
      var series = chart.get(id) 
      shift = series.data.length > 100; // shift if the series is longer than 300 (drop oldest point) 

      response= $.parseJSON(response) 
      var x=data['sub-type'] 
      all_data=response.response.data[x] 

    //    console.log(new Date(from_date),'latest timestamp') 
       console.log(series) 
      console.log("data",all_data) 

      from_date=all_data[all_data.length-1][0] 
//   console.log(from_date) 
//    series.isDirty=false 
//    series.isDirtyData=false 
      for (var i = 0; i < all_data.length; i++) 
      { 
       series.addPoint({ x: all_data[i][0],y: all_data[i][1],id: i},false,shift); 
      } 
      console.log("series object",series) 
    //     chart.redraw(); 
       console.log(" parameter",data) 
       data['from-date']=from_date 

      console.log("data",series.data) 
    //    console.log(chart) 
      setTimeout(requestData(data), 10000); 
      console.log("out of success") 
      }, 

      cache:false, 
      error:function() 
      { 
       console.log("err") 

      } 
     }); 
     } 

    } 

    $.ajax({ 
     url:serverUrl+'/api/fetch_all_servers/', 
     type:'GET', 
     success:function(response){ 
      response = $.parseJSON(response) 
      sd = response.response.all_servers 
      $('input[name=select_menue]').optionTree(sd) 
     }, 
     error:function(){ 
      console.log('error') 
     } 
    }); 

    $('.param-button').live('click',function(e){ 
     e.stopPropagation() 
    }) 

    $('param-select').live('hover',function(){ 
     $(this).find('.type-select').show() 
    }); 

    $('.final_value').live('change',function(){ 
     select_name = 'select_menue_' 
     param_list = [] 
     var param=$('select[name="'+select_name+'"] option:selected').attr('value') 
     while(param){ 
      param_list.push(param) 
      select_name += '_' 
      var param=$('select[name="'+select_name+'"] option:selected').attr('value') 
     } 
     console.log(param_list,"param_list") 
     from_date=new Date().getTime()-300000 //5 minute data 
     hostname=param_list[0] 
     TypeOfParameter= param_list[1] 
     sub_type_parameter=param_list[param_list.length-1] 
     data = {'type':TypeOfParameter,'hostname':hostname,'param':sub_type_parameter,'from-date':from_date} 
     var sub_type; 
     if(param_list.length==4){ 
      sub_type=param_list[2] 
      data['sub-type'] = sub_type 
     } 
     else 
     { 
      sub_type=sub_type_parameter 
     } 
//  console.log(hostname,TypeOfParameter,sub_type,sub_type_parameter) 

     data = {'type':TypeOfParameter,'hostname':hostname,'sub-type':sub_type,'param':sub_type_parameter,'from-date':from_date} 
     requestData(data) 

     $('#loadingmessage').show(); // show the loading message. 


     chart = $('#first').highcharts(); 

     if(TypeOfParameter=='cpu') 
     { 
      console.log("adding axis") 
      chart.addAxis({ // Primary yAxis 


          id:'Cpu_axis'+sub_type_parameter, 
          labels: { 
           formatter: function() { 
            return this.value; 
           }, 
           style: { 
            color: '#89A54E' 
            } 
          }, 
          title: { 
           text: "core "+ sub_type+ " "+sub_type_parameter, 
           style: { 
            color: '#89A54E' 
           } 
          }, 

      lineWidth: 1, 
      lineColor: '#08F' 

         }); 



     console.log("adding series") 
     chart.addSeries({ 
      id:sub_type+sub_type_parameter, 
      name: "core "+sub_type+" "+sub_type_parameter, 
      data :[], 

      tooltip : { 
       valueSuffix: ' %' 
        }, 
       yAxis:'Cpu_axis'+sub_type_parameter 
       }) 
      console.log("series out") 


     } 
     if(TypeOfParameter=='memory') 
     { 
      chart.addAxis ({ 
      id:'memory'+sub_type_parameter, 
      labels:{ 
        formatter: function() { 
            return this.value +'%'; 
           }, 
         style: { 
          color: '#89C54F' 
          } 
        }, 

      title: { 
       text:sub_type+" "+sub_type_parameter 
      }, 
      lineWidth: .5, 
      lineColor: '#08F', 
      opposite: true 
     }); 
     chart.addSeries({ 
      id:sub_type+sub_type_parameter, 
      name: sub_type+'memory usage', 
      data: [], 
      tooltip: { 
     valueSuffix: '%' 
       }, 
      yAxis:'memory'+sub_type_parameter 
     }); 
     } 

    if(TypeOfParameter=='disk') 
     { 
      chart = new Highcharts.Chart({ 
      chart: { 
       renderTo: 'second', 
       defaultSeriesType: 'spline', 
       events: { 
        load: requestData 
       } 
      }, 
      title: { 

       text: 'disk Usage' 
      }, 
      xAxis: { 
       type: 'datetime', 
       tickPixelInterval: 150, 
       maxZoom: 20 * 1000 
      }, 
      yAxis: { 
       minPadding: 0.2, 
       maxPadding: 0.2, 
       title: { 
        text: 'disk', 
        margin: 80 
       } 
      }, 
      series: [{ 
       id:sub_type+sub_type_parameter, 
       name: 'disk', 
       data: [] 
      }] 
}); 
     } 
    if(TypeOfParameter=='db') 
     { 
      chart = new Highcharts.Chart({ 
      chart: { 
       renderTo: 'second', 
       defaultSeriesType: 'spline', 
       events: { 
        load: requestData 
       } 
      }, 
      title: { 
       text: 'mongo Usage' 
      }, 
      xAxis: { 
       type: 'datetime', 
       tickPixelInterval: 150, 
       maxZoom: 20 * 1000 
      }, 
      yAxis: { 
       minPadding: 0.2, 
       maxPadding: 0.2, 
       title: { 
        text: 'mmongo', 
        margin: 80 
       } 
      }, 
      series: [{ 
       id:sub_type+sub_type_parameter, 
       name: 'mongo', 
       data: [] 
      }] 
}); 
     } 
    if(TypeOfParameter=='redis') 
     { 
      chart = new Highcharts.Chart({ 
      chart: { 
       renderTo: 'second', 
       defaultSeriesType: 'spline', 
       events: { 
        load: requestData 
       } 
      }, 
      title: { 
       text: 'redis Usage' 
      }, 
      xAxis: { 
       type: 'datetime', 
       tickPixelInterval: 150, 
       maxZoom: 20 * 1000 
      }, 
      yAxis: { 
       minPadding: 0.2, 
       maxPadding: 0.2, 
       title: { 
        text: 'redis', 
        margin: 80 
       } 
      }, 
      series: [{ 
       id:sub_type+sub_type_parameter, 
       name: 'redis', 
       data: [] 
      }] 
    }); 
        } 
       $('#loadingmessage').hide(); // hide the loading message 




     } 
      ) 
      }); 

我被困在這個問題上很長一段時間。仍然無法找出解決方案。 這裏是完整的代碼link 有人請請幫忙。感覺沮喪.. :-(

+0

這個:'series:{}'應該是:'series:[]'。另外,我看到你有兩個圖表,分配給一個變量..?這兩個圖表使用相同的requestData()函數?我建議先從一張圖表開始,然後開始工作,然後開始使用兩張圖表。 –

+0

hey @PawełFus保留系列:{}爲我添加了多個系列dyncamilly。另外requestData正在爲我編寫一個系列。 現在我已經在全球範圍內聲明瞭highchart對象,以便將所有系列添加到一個highchart中。讓我知道我在做什麼錯誤? – jugadengg

+1

對我來說,系列宣言是錯誤的..我很抱歉,但看到和調試這個'活'我無法幫助你。 –

回答

0

正如你所知道的,當處理很多點時,addPoint方法有點危險,建議在處理很多新點時禁用每點重繪 - 更多信息http://api.highcharts.com/highcharts#Series.addPoint(),我注意到你是這樣做已經在循環語句中,但爲什麼你註釋掉了?你是否嘗試過再次啓用。確保chart.redraw的工作原理是添加一個新的重繪圖表事件,並設置警報或控制檯日誌。

嘗試使用,下面作爲ajax的一部分,而不是緩存:false。我在過去有一些問題 標題:{'Cache-Control':'no-cache'}

乾杯