2016-07-21 15 views
1

我嘗試使用JSON格式的jqplot製作圖表。Json到jqplot圖表

file.txt的包含:

<script class="code" type="text/javascript"> 
$(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; 
     }, error:function (xhr, ajaxOptions, thrownError){ 
     alert(xhr.responseText);} 
    }); 
    return ret; 
    }; 

    // The url for our json data 
    var jsonurl = "file.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 
    } 

    }); 
}); 
</script> 

當我運行這段代碼我有一個空的圖表那樣: enter image description here

山楂可以解決這個

[[["2008-09-30 4:00PM",15],["2008-10-30 4:00PM",8],["2008-11-30 4:00PM",17],["2008-12-30 4:00PM",10]]] 
在jqplot

?以及我們如何定義軸x =日期和y =值?

回答

0

看起來數據並沒有「到達」情節。

我假定您位於基於Linux \ UNIX的服務器上:將"file.txt"更改爲"./file.txt"correctly access當前文件夾中的文件。

要使用日期渲染器渲染x-axis,請參閱this post