将变量添加到Jqplot中

时间:2013-07-03 10:12:27

标签: javascript jquery 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函数中?

1 个答案:

答案 0 :(得分: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 + '"]]'); 
相关问题