循环数据并将其传递到图表

suo*_*suo 1 javascript json apexcharts

我有一组数据用于绘制折线图。我正在使用 ApexCharts。

let testData = [
  {
    cell_id: 5833307,
    datetime: ["2019-05-07 11:28:16.406795+03", "2019-05-07 11:28:38.764628+03", "2019-05-07 12:18:38.21369+03", "2019-05-07 12:33:47.889552+03", "2019-05-08 08:45:51.154047+03"],
    rsrq: ["108", "108", "108", "108", "109"]
  },
  {
    cell_id: 2656007,
    datetime: ["2019-07-23 15:29:16.572813+03", "2019-07-23 15:29:16.71938+03", "2019-07-23 15:29:16.781606+03", "2019-07-23 15:29:50.375931+03", "2019-07-23 15:30:01.902013+03"],
    rsrq: ["120", "119", "116", "134", "114"]
  }
];

let datasetValue = [];

for( let x=0; x<testData.length; x++ )
{
  datasetValue =
    { 
    chart: {
      height: 380,
      width: "100%",
      type: "line"
    },
    stroke: {
      curve: 'smooth',
      width: 1.5,
    },
    markers: {
      size: 4,
    },
    legend: {
      show: true,
      position: 'top'
    },  
    series: [
      {
        name: testData[x].cell_id,
        data: testData[x].rsrq
      }
    ],
    xaxis: {
      categories: testData[x].datetime,
      title: {
        text: "Date"
      }
    },
    yaxis: {
      title: {
        text: "RSSI"
      }
    }
  }                
}
            
var chart = new ApexCharts(document.querySelector("#signal"), datasetValue);

chart.render();
Run Code Online (Sandbox Code Playgroud)
<div id="signal"></div>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>
Run Code Online (Sandbox Code Playgroud)

因此,我将 JSON 数组放入一个for循环中以获取我的数据集。我定义了一个数组变量datasetValue,我分配循环数据并将其传递给我的图表实例:new ApexCharts(document.querySelector("#rssi-signal"), datasetValue);

发生的事情只是传递了最后一个数组对象,这意味着我缺少/未传递某些内容来获取所有数据。

Use*_*863 5

通过对系列和类别进行分组来重构测试数据

let series = [];
let categories = [];

for (let x = 0; x < testData.length; x++) {
  series.push({
    name: testData[x].cell_id,
    data: testData[x].rsrq
  });
  categories.concat(testData[x].datetime);
}
Run Code Online (Sandbox Code Playgroud)

let series = [];
let categories = [];

for (let x = 0; x < testData.length; x++) {
  series.push({
    name: testData[x].cell_id,
    data: testData[x].rsrq
  });
  categories.concat(testData[x].datetime);
}
Run Code Online (Sandbox Code Playgroud)
let testData = [{
    cell_id: 5833307,
    datetime: ["2019-05-07 11:28:16.406795+03", "2019-05-07 11:28:38.764628+03", "2019-05-07 12:18:38.21369+03", "2019-05-07 12:33:47.889552+03", "2019-05-08 08:45:51.154047+03"],
    rsrq: ["108", "108", "108", "108", "109"]
  },
  {
    cell_id: 2656007,
    datetime: ["2019-07-23 15:29:16.572813+03", "2019-07-23 15:29:16.71938+03", "2019-07-23 15:29:16.781606+03", "2019-07-23 15:29:50.375931+03", "2019-07-23 15:30:01.902013+03"],
    rsrq: ["120", "119", "116", "134", "114"]
  }
];

let series = [];
let categories = [];


for (let x = 0; x < testData.length; x++) {
  series.push({
    name: testData[x].cell_id,
    data: testData[x].rsrq
  });
  categories = categories.concat(testData[x].datetime);
}

var chart = new ApexCharts(document.querySelector("#signal"), {
  chart: {
    height: 380,
    width: "100%",
    type: "line"
  },
  stroke: {
    curve: 'smooth',
    width: 1.5,
  },
  markers: {
    size: 4,
  },
  legend: {
    show: true,
    position: 'top'
  },
  series: series,
  xaxis: {
    categories: categories,
    title: {
      text: "Date"
    }
  },
  yaxis: {
    title: {
      text: "RSSI"
    }
  }
});

chart.render();
Run Code Online (Sandbox Code Playgroud)