Javascript 将变量添加到Jqplot中

Javascript 将变量添加到Jqplot中,javascript,jquery,jqplot,Javascript,Jquery,Jqplot,我正在尝试绘制图表,如果我手动输入数字,一切都会很好。比如 chart = $.jqplot('chartContainer', [[[0, firstActual],[.5, trajectory]], [[0, firstGoal],[1, secondGoal],[2, thirdGoal],[3, fourthGoal],[4, fifthGoal]], [[.5, trajectory]]], { 变量是预先定义好的,可以正常工作。现

我正在尝试绘制图表,如果我手动输入数字,一切都会很好。比如

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函数中是否还有使test出现的方法?

将数组作为数组而不是字符串添加到test变量中

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

chart = $.jqplot('chartContainer', 
    [test,
    [[0, firstGoal],[1, secondGoal],[2, thirdGoal],[3, fourthGoal],[4, fifthGoal]],
    [[.5, trajectory]]], {
如果出于某种原因需要将test格式化为字符串,可以使用
JSON.parse()
将字符串解析为数组

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