简体   繁体   English

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

[英]Converted PHP code that built an array to JS and now highcharts doesn't work - what did I do wrong?

So I used to have a block of PHP code inside my JS that built up this string: 因此,我过去在JS中使用了一段PHP代码来构建此字符串:

<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: 我想将JS移至外部文件,因此现在将一些变量传递给JS,并以此构建数组:

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. console.log(aSeries)向我显示,这“正确地”为我提供了一个JS对象,其中包含我要查找的所有属性。

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. 我没有JS错误,但是我的图形无法正确显示。 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. 系列名称的图例显示正确,但是x轴上仅放置了一项(而不是4),并且未绘制任何数据点。

console.log(data): console.log(数据):

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: JS中的数据,现在我首先将其生成为适当的PHP数组(为简洁起见,将其剪切为第一个索引:

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]

Your problem is in here: 您的问题在这里:

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

.join returns a string, which isn't what you need. .join返回一个字符串,这不是您所需要的。 You probably need: 您可能需要:

data: data[aIDs[i]]

Since data[aIDs[i]] already appears to be an array. 由于data[aIDs[i]]已似乎是一个数组。

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. 这应该解析data[aIDs[i]]每个单独的字符串,并生成{x: number, y: number}元素的数组。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM