简化JavaScript数组变量

时间:2018-12-16 13:48:59

标签: javascript java spring-mvc

我正在寻求简化此代码。有什么办法吗? Spring MVC + Apex图表

var d = /*[[${s0}]]*/ null`;  <-- It is sent via the Spring Framework. Basically represents datetime(in millis) at `d[0]`, `d[3]`,... Temperature at `d[1]`, `d[4]`,... and Humidity at `d[2]`, `d[5]`,...


                        <script type="text/javascript" th:inline="javascript">
                        var d = /*[[${s0}]]*/ null;
                        var options = {
                            chart: {
                                type: 'area',
                                height: 300
                            },
                            series: [
                                {
                                    name: 'Temperature',
                                    data: [
                                        [d[0], d[1]],
                                        [d[3], d[4]],
                                        [d[6], d[7]],
                                        [d[9], d[10]],
                                        [d[12], d[13]],
                                        [d[15], d[16]],
                                        [d[18], d[19]],
                                        [d[21], d[22]],
                                        [d[24], d[25]],
                                        [d[27], d[28]],
                                        [d[30], d[31]],
                                        [d[33], d[34]],
                                        [d[36], d[37]],
                                        [d[39], d[40]],
                                        [d[42], d[43]],
                                        [d[45], d[46]],
                                        [d[48], d[49]],
                                        [d[51], d[52]],
                                        [d[54], d[55]],
                                        [d[57], d[58]],
                                        [d[60], d[61]],
                                        [d[63], d[64]],
                                        [d[66], d[67]],
                                        [d[69], d[70]]
                                    ]
                                },
                                {
                                    name: "Humidity",
                                    data: [
                                        [d[0], d[2]],
                                        [d[3], d[5]],
                                        [d[6], d[8]],
                                        [d[9], d[11]],
                                        [d[12], d[14]],
                                        [d[15], d[17]],
                                        [d[18], d[20]],
                                        [d[21], d[23]],
                                        [d[24], d[26]],
                                        [d[27], d[29]],
                                        [d[30], d[32]],
                                        [d[33], d[35]],
                                        [d[36], d[38]],
                                        [d[39], d[41]],
                                        [d[42], d[44]],
                                        [d[45], d[47]],
                                        [d[48], d[50]],
                                        [d[51], d[53]],
                                        [d[54], d[56]],
                                        [d[57], d[59]],
                                        [d[60], d[62]],
                                        [d[63], d[65]],
                                        [d[66], d[68]],
                                        [d[69], d[71]]
                                    ]
                                }
                            ],
                            xaxis: {
                                type: 'datetime'
                            },
                            yaxis: [
                                {
                                    axisTicks: {
                                        show: true
                                    },
                                    axisBorder: {
                                        show: true,
                                    },
                                    title: {
                                        text: "Temperature"
                                    }
                                }, {
                                    min: 0,
                                    max: 100,
                                    opposite: true,
                                    axisTicks: {
                                        show: true
                                    },
                                    axisBorder: {
                                        show: true,
                                    },
                                    title: {
                                        text: "Humidity"
                                    }
                                }
                            ],
                            legend: {
                                position: 'top',
                                horizontalAlign: 'center'
                            },
                            tooltip: {
                                x: {
                                    format: 'HH:mm dd/MM/yy'
                                },
                            }
                        }
                        var chart = new ApexCharts(document.querySelector("#chart0"), options);
                        chart.render();
                    </script>

我只需要简化通过d[0]d[1]等发送数据的过程。是否有任何循环或其他可用的方法?

3 个答案:

答案 0 :(得分:2)

您可以使用一个函数,该函数获取所需元素的数据和模式,并为下一行添加增量。

function mapByPattern(data, pattern, offset) {
    var result = [], i = 0;
    while (i < data.length) {
        result.push(pattern.map(j => data[i + j]));
        i += offset;
    }
    return result;
}

var data = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11],
    result = { series: [
        { name: 'Temperature', data: mapByPattern(data, [0, 1], 3) },
        { name: "Humidity",    data: mapByPattern(data, [0, 2], 3) }
    ]};

console.log(result);

答案 1 :(得分:0)

谢谢你,妮娜。代码无法完全按照我的要求工作,但是对修复自己的代码很有帮助。非常感谢!这是一些固定的代码:)

var data = /*[[${s0}]]*/ null;

                        function mapByPattern(data, pattern, offset) {
                            var result = [], i = 0;
                            while (i < data.length) {
                                result.push(pattern.map(j => data[i + j]));
                                i += offset;
                            }
                            return result;
                        }

                        var options = {
                            chart: {
                                type: 'area',
                                height: 300
                            },
                            series: [
                                {
                                    name: 'Temperature',
                                    data: mapByPattern(data, [0, 1], 3)
                                },
                                {
                                    name: "Humidity",
                                    data: mapByPattern(data, [0, 2], 3)
                                }
                            ],
                            xaxis: {
                                type: 'datetime'
                            },
                            yaxis: [
                                {
                                    axisTicks: {
                                        show: true
                                    },
                                    axisBorder: {
                                        show: true,
                                    },
                                    title: {
                                        text: "Temperature"
                                    }
                                }, {
                                    min: 0,
                                    max: 100,
                                    opposite: true,
                                    axisTicks: {
                                        show: true
                                    },
                                    axisBorder: {
                                        show: true,
                                    },
                                    title: {
                                        text: "Humidity"
                                    }
                                }
                            ],
                            legend: {
                                position: 'top',
                                horizontalAlign: 'center'
                            },
                            tooltip: {
                                x: {
                                    format: 'HH:mm dd/MM/yy'
                                },
                            }
                        }
                        var chart = new ApexCharts(document.querySelector("#chart0"), options);
                        chart.render();

答案 2 :(得分:-2)

尝试这样(删除重复和无关的代码)

vertex.color = adjustcolor(colorvector,alpha)