2013-07-03 127 views
0

我試圖繪製圖表和圖表,如果手動輸入數字,一切都很好。如將變量添加到Jqplot

chart = $.jqplot('chartContainer', 
     [[[0, firstActual],[.5, trajectory]], 
     [[0, firstGoal],[1, secondGoal],[2, thirdGoal],[3, fourthGoal],[4, fifthGoal]], 
     [[.5, trajectory]]], { 

這些變量事先定義好,工作正常。現在,如果我想這樣做

var test = "["+0+"," +firstActual+"], ["+.5+", "+trajectory+"]"; 

chart = $.jqplot('chartContainer', 
     [[test], 
     [[0, firstGoal],[1, secondGoal],[2, thirdGoal],[3, fourthGoal],[4, fifthGoal]], 
     [[.5, trajectory]]], { 

第一行不會打印。我猜這與測試是一個字符串有關嗎?無論如何要讓測試出現在jqplot函數內?

回答

0

將數組添加到測試變量中作爲數組,而不是字符串。

var test = [[0, firstActual], [.5, trajectory]] 

chart = $.jqplot('chartContainer', 
    [test, 
    [[0, firstGoal],[1, secondGoal],[2, thirdGoal],[3, fourthGoal],[4, fifthGoal]], 
    [[.5, trajectory]]], { 

如果你因爲某些原因需要形成測試作爲一個字符串,你可以使用JSON.parse()來分析字符串數組

var test = JSON.parse('[[' + 0 + ', "' + firstActual + '"], [' + .5 + ', "' + trajectory + '"]]');