普通网友 2015-11-27 11:11
浏览 49
已采纳

将构建数组的转换后的PHP代码转换为JS,现在是highcharts不起作用 - 我做错了什么?

So I used to have a block of PHP code inside my JS that built up this string:

<snip>
    series: [{
        marker: {
            fillColor:'#66aaff'
        },
        color: '#66aaff',
        name: 'Person 1',
        data: [{x:0, y: 12},{x:1, y: 16},{x:2, y: 18},{x:3, y: 14}]
    }, {
        marker: {
            fillColor:'#ff8888'
        },
        color: '#ff8888',
        name: 'Person 2',
        data: [{x:0, y: 26},{x:1, y: 17},{x:2, y: 22},{x:3, y: 17}]
    }]
<snip>

I want to move my JS to an external file, so I'm now passing a few variables through to JS, and building the array with this:

var aSeries = [];
for (i = 0; i < aIDs.length; i++) {
    aSeries.push({
        marker: {
            fillColor: '#' + aSeriesColors[i]
        },
        color: '#' + aSeriesColors[i],
        name: aNames[aIDs[i]],
        data: [data[aIDs[i]].join(',')]
    });
}

console.log(aSeries) shows me that this "correctly" gives me a JS object with all of the properties I'm looking for.

I've then changed the first code block above simply to:

<snip>
    series: aSeries
<snip>

I get no JS errors, but my graph doesn't show correctly. The legend of series names shows correctly, but only one item is placed on the x-axis (instead of 4) and no data points are plotted.

console.log(data):

Object {154: Array[4], 156: Array[4], 307: Array[4], 994: Array[4]}
154: Array[4]
0: "{x:0, y: 26.145225241042}"
1: "{x:1, y: 17.211534431451}"
2: "{x:2, y: 22.184885666481}"
3: "{x:3, y: 17.898072988406}"
length: 4
__proto__: Array[0]
156: Array[4]
0: "{x:0, y: 12.555414124567}"
1: "{x:1, y: 16.300627296478}"
2: "{x:2, y: 18.353667038483}"
3: "{x:3, y: 14.082830741251}"
length: 4
__proto__: Array[0]
307: Array[4]
0: "{x:0, y: 37.967636688174}"
1: "{x:1, y: 30.79271274292}"
2: "{x:2, y: 34.540574456219}"
3: "{x:3, y: 37.892991347838}"
length: 4
__proto__: Array[0]
994: Array[4]
0: "{x:0, y: 4.1734334079504}"
1: "{x:1, y: 0.35625969235927}"
2: "{x:2, y: 6.3747908533185}"
3: "{x:3, y: 0.62718142794101}"
length: 4
__proto__: Array[0]
__proto__: Object

the data in JS now that I generate it as a proper PHP array first (snipped to first indice for brevity:

Object {154: Array[4], 156: Array[4], 307: Array[4], 994: Array[4]}
154: Array[4]
0: Object
x: "0"
y: "26.145225241042"
__proto__: Object
1: Object
x: "1"
y: "17.211534431451"
__proto__: Object
2: Object
x: "2"
y: "22.184885666481"
__proto__: Object
3: Object
x: "3"
y: "17.898072988406"
__proto__: Object
length: 4
__proto__: Array[0]
  • 写回答

1条回答 默认 最新

  • douchong4730 2015-11-27 11:14
    关注

    Your problem is in here:

    data: [data[aIDs[i]].join(',')]
    

    .join returns a string, which isn't what you need. You probably need:

    data: data[aIDs[i]]
    

    Since data[aIDs[i]] already appears to be an array.

    However, since it's an array of string, there's one last step:

    data: data[aIDs[i]].map(JSON.parse)
    

    This should parse each individual string in data[aIDs[i]], and result in an array of {x: number, y: number} elements.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

    报告相同问题?

    悬赏问题

    • ¥15 易语言编写cs.16 绘制 矩阵出现了点问题 不知道这个矩阵可不可以使用
    • ¥15 k210显示failed init to model
    • ¥15 Evil-droid生成的APK手机已经下载但无法建立任务
    • ¥25 c语言韩信点兵的变式
    • ¥15 怎么根据书上的例子完成这个问题呢?
    • ¥15 ECharts 增加Zoom,整行包括右边的Text一起滑动
    • ¥15 关于网上一个easyx制作的见缝插针小游戏(c++)
    • ¥15 开地址法双散列函数处理碰撞
    • ¥15 想问一下这个是什么情况 虚拟机Linux打不开了
    • ¥15 联通光猫掉注册了怎么重新注册上去