2014-03-04 31 views
1

我正在使用此代碼使用AJAX JSON數據呈現器創建Jqplot。它一切正常。我在頁面上有一個按鈕,允許用戶輸入一個新值(通過ajax更新)。這被存儲到數據庫中(與json數據來自同一個地方)。在ajax成功上重新繪製Jqplot

當按下按鈕並將新值添加到數據庫時,我希望再次刷新jqplot(即檢查ajax源)。

我試過了一些方法,要麼不更新圖形,要麼將圖形繪製在彼此之上。

$(document).ready(function(){ 
    // Our ajax data renderer which here retrieves a text file. 
    // it could contact any source and pull data, however. 
    // The options argument isn't used in this renderer. 
    var ajaxDataRenderer = function(url, plot, options) { 
    var ret = null; 
    $.ajax({ 
     // have to use synchronous here, else the function 
     // will return before the data is fetched 
     async: false, 
     url: url, 
     dataType:"json", 
     success: function(data) { 
     ret = data; 
     } 
    }); 
    return ret; 
    }; 

    // The url for our json data 
    var jsonurl = "./jsondata.txt"; 

    // passing in the url string as the jqPlot data argument is a handy 
    // shortcut for our renderer. You could also have used the 
    // "dataRendererOptions" option to pass in the url. 
    var plot2 = $.jqplot('chart2', jsonurl,{ 
    title: "AJAX JSON Data Renderer", 
    dataRenderer: ajaxDataRenderer, 
    dataRendererOptions: { 
     unusedOptionalUrl: jsonurl 
    } 
    }); 
}); 

假設我需要將jqplot添加到函數中並銷燬舊圖,然後調用成功函數? - 不能很好地使用該方法正常工作。

任何幫助將是偉大的。

謝謝

+0

您是否嘗試過使用[jqPlot.reDraw(http://www.jqplot.com/docs/files/jqplot-core-js.html#jqPlot.redraw)。我真的建議你不要使用async:false ajax請求,你可以通過在ajax請求的成功函數中移動plot2 = $ .jqplot片來使它工作。 – JoseM

+0

重繪主要用於縮放。 –

+0

ajax JSON數據呈現器是jqplot推薦的。 http://www.jqplot.com/tests/data-renderers.php我有在同一頁上運行多個圖表使用相同的dataRenderer。如果是在ajax成功的情況下完成,那麼是否會收到messey? –

回答

0

您是否嘗試重新繪製圖表?

var plot2 = $.jqplot('chart2', jsonurl,{ 
     title: "AJAX JSON Data Renderer", 
     dataRenderer: ajaxDataRenderer, 
     dataRendererOptions: { 
     unusedOptionalUrl: jsonurl 
    } 
}); 

plot2.replot(); 

希望它有幫助。

問候, 阿尼什