2014-10-20 23 views
0

所以我打算在自己的div中繪製多個flot圖表。數據是在json中我已經解析成單個jsons。然後我想要爲每個單獨的json創建一個div並繪製單個json。填充每個循環時,繪圖圖表爲空

但是,這似乎並沒有工作!我得到了(在這個例子中)3個div和繪圖區域顯示,但沒有數據 - 我測試了一個單一的json(沒有循環),它的工作就像一個魅力。

我錯過了什麼?

代碼可以在這裏找到:http://dinbab.dk/frank_test/

+0

請包括代碼和/或在你的問題的簡單重複的例子。 – Mark 2014-10-20 19:56:19

+0

對不起,我只是遇到了代碼標記的問題,我認爲會有很多代碼......但我會在下次做。 – frankmyhre 2014-10-21 06:25:53

回答

1

您可以使用jQuery的距離(https://api.jquery.com/jquery.parsejson/)的parseJson功能。 這樣你就不必做字符串操作。

取而代之的是:

var shorted_json = json.substring(2,json.length-2);  // Cut away leading and trailing characters from outer json-array 
var replaced_json = shorted_json.replace(/},{/g,'o'); // Replace json-object seperator },{ with an 'o' 
var splitted_json = replaced_json.split('o');   // Split the json-string for every occurence of the character o; 
$.each(splitted_json, function(index, value){ 
    var single_json = "{"+value+"}"; 
    var element = "<div id='placeholder_"+index+"'></div><br>"; 
    $(document.body).append(element); 
    $('#placeholder_'+index).css('height', 500); 
    $('#placeholder_'+index).css('width', '100%'); 
    var options = { ... }; 

    $.plot('#placeholder_'+index,[single_json],options); 

}); 

做到這一點:

$.each($.parseJSON(json), function(index, value){ 
    var element = "<div id='placeholder_"+index+"'></div><br>"; 
    $(document.body).append(element); 
    $('#placeholder_'+index).css('height', 500); 
    $('#placeholder_'+index).css('width', '100%'); 
    var options = { ... }; 

    $.plot('#placeholder_'+index, [value['data']],options); 

}); 

見該工作示例:http://jsfiddle.net/L8fb72h5/

+0

非常感謝eee – frankmyhre 2014-10-21 06:24:56