遍历数据并将其传递到图表

时间:2019-08-21 15:09:29

标签: 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();
<div id="signal"></div>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>

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

正在发生的事情是仅传递了最后一个数组对象,这意味着我缺少/没有传递一些东西来获取我的所有数据。

4 个答案:

答案 0 :(得分:4)

通过将系列和类别分组来重组testData

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);
}

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();
<div id="signal"></div>
<script src="https://cdn.jsdelivr.net/npm/apexcharts"></script>

答案 1 :(得分:2)

因为您在forloop之外声明了一个数组

let datasetValue = { 
    chart: {
      height: 380,
      width: "100%",
      type: "line"
    },
    stroke: {
      curve: 'smooth',
      width: 1.5,
    },
    markers: {
      size: 4,
    },
    legend: {
      show: true,
      position: 'top'
    },  
    series: [],
    xaxis: {
      categories: [],
      title: {
        text: "Date"
      }
    },
    yaxis: {
      title: {
        text: "RSSI"
      }
    }
  };

在for循环中,您应该

    datasetValue.series.push(
          {
            name: testData[x].cell_id,
            data: testData[x].rsrq
          });
    datasetValue.xaxis.categories.push(testData[x].datetime);

您应该将值推送到数组内部,而不是在每次迭代中重新分配

答案 2 :(得分:2)

您要尝试做的第一个错误是将“ datasetValue”定义为数组变量。

datasetValue = yourdata; //wrong in case of pushing data into array

由于循环和分配,您试图将一个对象分配给仅包含最后结果的数组变量。 而是使用array的push方法将数据推入数组。

datasetValue.push(yourdata); //correct way to push data to array

因此,将“ datasetValue”定义为数组没有用。

要实现您的目标,您可以应用以下循环

var datasetValue;
var series = [];
var 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);
}

datasetValue = { 
    chart: {
      height: 380,
      width: "100%",
      type: "line"
    },
    stroke: {
      curve: 'smooth',
      width: 1.5,
    },
    markers: {
      size: 4,
    },
    legend: {
      show: true,
      position: 'top'
    },  
    series,
    xaxis: {
      categories,
      title: {
        text: "Date"
      }
    },
    yaxis: {
      title: {
        text: "RSSI"
      }
    }
  };

var chart = new ApexCharts(document.querySelector("#signal"), datasetValue);

chart.render();

答案 3 :(得分:1)

我将您的for循环移到datasetValue定义之后,仅向其添加序列,并更改xaxis

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

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

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