2011-12-14 94 views
1

我想通過Ajax xml獲取數據來渲染jqplot圖形。JQPlot Ajax圖形渲染問題

$.ajax({ 
     type: "POST", 
     url: "/ajaxXML.jsp", 
     data: ({cmd: "report"}), 
     dataType: "xml", 
     success: function(xml) { 
       var data= new Array(); 
       $(xml).find('graph').each(function(){ 
       var points = new Array() 
       var x = $(this).attr('x'); 
       var y = $(this).attr('y'); 
       points.push(x); 
       points.push(y); 
       data.push(points); 
      }); 

      plotGraph(data); 

     } 
    });    


function plotGraph(data){ 
    var line1=[['11-01-11',2052], ['11-02-11',2205], ['11-03-11',1910], ['11-04-11',2085], ['11-05-11',2261], ['11-06-11',1714], ['11-07-11',3123], ['11-08-11',3369], ['11-09-11',3515], ['11-10-11',3380], ['11-11-11',3476], ['11-12-11',3954], ['11-13-11',2799], ['11-14-11',3166], ['11-15-11',2932] ,['11-16-11',3289]]; 
    alert(data); 
    alert(line1); 
    $.jqplot('chart1', [data], { 
     title:'Margin vs Date', 
     axes:{ 
      xaxis:{ 
       renderer:$.jqplot.DateAxisRenderer 
      }, 
      yaxis:{autoscale:true} 
     }, 
     series:[{lineWidth:4}] 

     }); 
} 

的XML是什麼樣子

 <graphs> 
     <graph x="11-28-2011" y="48973"></graph> 
     <graph x="11-29-2011" y="41981"></graph> 
     <graph x="11-30-2011" y="45562"></graph> 
     <graph x="12-01-2011" y="82437"></graph> 
     <graph x="12-02-2011" y="83979"></graph> 
     <graph x="12-03-2011" y="64444"></graph> 
    </graphs> 

但一些如何這個策略是行不通的只有X軸填充了沒有數據點或圖表Y軸。

然而,當我嘗試看看樣本數據即一號線它的工作原理,即使當你分析你的y值推入數組作爲字符串不是整數的XML通過Ajax調用fetchced數據幾乎是相似

回答

2

。嘗試:

$(xml).find('graph').each(function(){ 
    var points = new Array() 
    var x = $(this).attr('x'); 
    var y = $(this).attr('y'); 
    points.push(x); 
    points.push(parseInt(y)); //modified line! 
    data.push(points); 
}); 
+0

謝謝馬克你讓我的一天:) – dpsdce 2011-12-15 15:39:36