简体   繁体   English

将数据从 API 加载到 highcharts vue 组件中

[英]Loading data from API into a highcharts vue component

I'm trying to load data from an express api into my highcharts-vue component, and i'm having trouble with parsing the data into my chart.我正在尝试将快速 api 中的数据加载到我的 highcharts-vue 组件中,但我无法将数据解析到我的图表中。 Express lets me know that my vue component is making a GET request to the API, Postman lets me know the GET request returns an array of date and env_light values but Vue is seemingly not loading, or not updating my chart with the new data Express 让我知道我的 vue 组件正在向 API、Postman 发出 GET 请求,让我知道 GET 请求返回一组日期和 env_light 值,但 Vue 似乎没有加载,或者没有用新数据更新我的图表

ChartDisplay.vue (component) ChartDisplay.vue(组件)

<template>
  <div class="chartElem">
    <div class="row">
      <highcharts :options="chartOptions"></highcharts>
    </div>
  </div>
</template>

<script>
import { Chart } from 'highcharts-vue';
import axios from 'axios';

export default {
  name: 'ChartDisplay',
  components: { highcharts: Chart },
  data() {
    return {
      chartOptions: {
        chart: {
          type: 'spline',
        },
        title: {
          text: 'Environment - Light',
        },
        series: [{
          data: [],
          color: '#6fcd98',
        }],
      },
    };
  },
  mounted() {
    this.fillData();
  },
  methods: {
    async fillData() {
      await axios.get('http://localhost:4000/api/db/env_light')
        .then((response) => {
          this.chartOptions.series[0].data = response.data.map((m) => m.env_light);
        });
    },
  },
};
</script>

The fillData function works as expected when i test it isolated outside of the Vue app and returns an array of values to this.chartOptions.series[0].data, and the chart works and updates automatically if i insert values manually into the data array and save the file.当我在 Vue 应用程序之外测试它时,fillData function 按预期工作,并将值数组返回到 this.chartOptions.series[0].data,如果我手动将值插入数据数组,图表会自动工作和更新并保存文件。

I've also tried to purge my database from 300+ entries to 5, in case there was a array length issue, and i've tried to parse the data into a structure like this - both unsuccessful我还尝试将我的数据库从 300 多个条目清除到 5 个,以防出现数组长度问题,并且我尝试将数据解析为这样的结构 - 均不成功

.then((response) => {
  this.chartOptions = {
    series: [{
      data: response.data.map((m) => m.env_light),
    }],
  };
});

Because the app is making a get request to the API when i refresh sites i know that the fillData method is being executed, and because the app is actually updating changes when i manually insert values into the arary it leads me to believe the default update mechanism for the chart is working.因为当我刷新站点时应用程序正在向 API 发出获取请求,所以我知道正在执行 fillData 方法,并且因为当我手动将值插入数组时应用程序实际上正在更新更改,这让我相信默认的更新机制因为图表正在工作。 Is there still something wrong with how i parse the data?我如何解析数据还有问题吗? This is my first Vue app, so i'm a bit puzzled and short on ideas both for solutions or how to further research the issue - any suggestions would be appreciated!这是我的第一个 Vue 应用程序,所以我对解决方案或如何进一步研究该问题感到有点困惑和缺乏想法 - 任何建议都将不胜感激!

The problem here is that series is an array and not an object.这里的问题是该series是一个数组,而不是 object。 So, you are accessing the data property of the array containing objects instead of the object itself.因此,您正在访问包含对象的数组的data属性,而不是 object 本身。 What you actually want is to access the data property of the first element of the series array.您真正想要的是访问series数组的第一个元素的data属性。

this.chartOptions.series[0].data = ...

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

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