【发布时间】:2021-09-03 08:42:19
【问题描述】:
所以我需要从我的 JSON 数据中制作一个看起来像这样的图表
{
"batch": [{
"timestamp": 738127638,
"data": {
"senzor1": 525,
"senzor2": 625,
"senzor3": 425,
"senzor4": 325
}
},
{
"timestamp": 738127640,
"data": {
"senzor1": 528,
"senzor2": 628,
"senzor3": 428,
"senzor4": 328
}
},
{
"timestamp": 738127658,
"data": {
"senzor1": 535,
"senzor2": 635,
"senzor3": 435,
"senzor4": 335
}
}
]
}
问题是我不知道如何在没有硬编码的情况下从传感器获取数据
for (let i = 0; i < this.batch.length; i++) {
this.ts.push(this.batch[i].data.senzor1)
}
有没有人知道如何做到这一点
我也试过这个:
for (let i = 0; i < this.batch.length; i++) {
this.ts.push(...Object.values(this.batch[i].data))
}
但这会将所有内容提取到同一个数组中,并且不会通过传感器分离数据
注意:我正在使用 Vue3 和 apexcharts
这是完整的代码
<template>
<div id="wrapper">
<p>{{batch}}</p>
<p>{{s1}}</p>
<div id="chart-line">
<apexchart
width="500"
type="line"
:options="chartOptions"
:series="series"
></apexchart>
</div>
<div id="chart-line">
<apexchart
width="500"
type="line"
:options="chartOptions"
:series="seriesLine2"
></apexchart>
</div>
</div>
</template>
<script>
import VueApexCharts from "vue3-apexcharts";
export default {
components: {
apexchart: VueApexCharts,
},
data: function() {
return {
batch:[],
s1:[],
s2:[],
s3:[],
s4:[],
chartOptions: {
chart: {
id: "vuechart-example",
},
xaxis: {
categories: [],
},
},
series: [
{
name: "series-1",
data: [],
},
],
seriesLine2:[{
name: "series-2",
data: [],
}]
};
},
methods:{
async fetchBatch(){
const res=await fetch('http://localhost:5000/batch')
const data= await res.json()
return data
},
},
async created(){
this.batch=await this.fetchBatch()
console.log(this.batch)
for (let i = 0; i < this.batch.length; i++) {
this.s1.push(this.batch[i].data.senzor1)
this.s2.push(this.batch[i].data.senzor2)
this.s3.push(this.batch[i].data.senzor3)
this.s4.push(this.batch[i].data.senzor4)
}
this.series=[{name: "series-1",data: this.s1}]
this.seriesLine2=[{name: "series-2",data: this.s2}]
}
}
</script>
编辑!解决了
this.test=Object.values(this.test)
感谢所有帮助过的人
【问题讨论】:
-
预期输出是什么
-
老实说。我不知道我只是在路障 RN
标签: javascript arrays json vuejs3 apexcharts