普通网友 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 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探