简体   繁体   English

如何在 Vue Chart.js 中使用 Api 数据

[英]How To Use Api Data With Vue Chart.js

So I am new to using data visualization in an application and I am trying to set my data coming from my api as the data the doughnut chart uses to display however I cannot figure out how to properly access the data所以我是在应用程序中使用数据可视化的新手,我试图将来自我的 api 的数据设置为圆环图用来显示的数据,但是我无法弄清楚如何正确访问数据

I have installed vue-chartjs as a way to simplify it for component use我已经安装了 vue-chartjs 作为简化组件使用的一种方式

Here is the Chart component这是图表组件

<script>
import { Doughnut } from 'vue-chartjs'
import {mapGetters} from 'vuex'

export default {
    name: 'FirmChart',
    extends: Doughnut,
    computed: {
        ...mapGetters(['chartEngagements']),
    },
    mounted () {
        this.renderChart({
        labels: ['Scanned', 'Recieved', 'Preparation', 'Review', '2nd Review', 'Complete'],
        datasets: [
                {
                label: 'Data One',
                borderColor: 'black',
                pointBackgroundColor: 'white',
                borderWidth: 1,
                pointBorderColor: 'white',
                backgroundColor: [
                    '#0077ff', 
                    '#0022ff',
                    '#1133bb',
                    '#0088aa',
                    '#11ffdd',
                    '#aabbcc',
                    '#22aabb',
                    ],
                data: [
                    10,
                    10,
                    10,
                    10,
                    10,
                    10,
                    ]
                },
            ]
        }, {responsive: true, maintainAspectRatio: false});
    },
    created() {
        this.$store.dispatch('retrieveEngagementsChartData')
    }
}
</script>

now my data is coming from the chartEngagements getter and this is the display of that data in the console现在我的数据来自chartEngagements getter,这是控制台中该数据的显示

{Complete: Array(1), Review: Array(3), 2nd Review: Array(1), Recieved: Array(7), Preparation: Array(1), …}

My question is how do I set the Complete: Array(1) etc to the data[] attribute in my this.renderChart() method??我的问题是如何在this.renderChart()方法中将Complete: Array(1)等设置为data[]属性?

I have tried doing something like this but It will not display anything我试过做这样的事情,但它不会显示任何东西

mounted () {
        this.renderChart({
        labels: ['Scanned', 'Recieved', 'Preparation', 'Review', '2nd Review' 'Complete'],
        datasets: [
                {
                label: 'Data One',
                borderColor: 'black',
                pointBackgroundColor: 'white',
                borderWidth: 1,
                pointBorderColor: 'white',
                backgroundColor: [
                    '#0077ff', 
                    '#0022ff',
                    '#1133bb',
                    '#0088aa',
                    '#11ffdd',
                    '#aabbcc',
                    '#22aabb',
                    ],
                data: [
                    this.chartEngagements.complete,
                    this.chartEngagements.review,
                    this.chartEngagements.2ndreview,
                    this.chartEngagements.preparation,
                    this.chartEngagements.recieved,
                    this.chartEngagements.scanned,
                    ]
                },
            ]
        }, {responsive: true, maintainAspectRatio: false});

However it doesn't display anything.. any help would be greatly appreciated or a point in the right direction!但是它没有显示任何内容..任何帮助将不胜感激或指向正确的方向!

Have you checked out the examples in the docs? 您是否签出了文档中的示例? https://vue-chartjs.org/guide/#chart-with-api-data https://vue-chartjs.org/guide/#chart-with-api-data

Your problem is, that your data api call is async. 您的问题是,您的数据api调用是异步的。 So your chart is rendered, even if your data is not fully loaded. 因此,即使您的数据没有完全加载,您的图表也会呈现出来。

There is also an example with vuex which is a bit outdated https://github.com/apertureless/vue-chartjs-vuex 还有一个vuex的示例,它有点过时了https://github.com/apertureless/vue-chartjs-vuex

You have to make sure that your data is fully loaded before you render your chart. 呈现图表之前,必须确保数据已完全加载。

I faced similar issue while implementing Charts with API data in one of my Vue JS apps I was working on.在我正在开发的一个 Vue JS 应用程序中使用 API 数据实现 Charts 时,我遇到了类似的问题。 I am using Vuex for state management, a simple solution for this problem is to move "chartData" from "data" to "computed" which would make it reactive.我正在使用 Vuex 进行状态管理,这个问题的一个简单解决方案是将“chartData”从“数据”移动到“计算”,这将使其具有反应性。 Below is the sample code from my app.以下是我的应用程序中的示例代码。

<template>  
    <line-chart v-if="chartData"
      style="height: 100%"
      chart-id="big-line-chart"
      :chart-data="chartData"
      :extra-options="extraOptions"
    >
    </line-chart>
</template>
<script>
import { mapActions, mapGetters } from 'vuex';
import * as expenseTypes from '../../store/modules/expense/expense-types';
import * as chartConfig from "../../components/reports/chart.config";
import BarChart from "../../components/reports/BarChart";

export default {
  components: {
    BarChart,
  },
  data () {
    return {
      extraOptions: chartConfig.chartOptionsMain
    }
  },
  computed: {
    ...mapGetters({
      allExpenses: expenseTypes.GET_ALL_EXPENSES,
      expenseCount: expenseTypes.GET_EXPENSE_COUNT,
    }),
    expenseAmount() {
      let expenseAmount = [];
      this.allExpenses.map((item) => {
        expenseAmount.push(item.amount);
      })
      return expenseAmount;
    },
    expenseLabels() {
      let expenseLabels = [];
      this.allExpenses.map((item) => {
        expenseLabels.push(item.note);
      })
      return expenseLabels;
    },
    chartData() {
      return {
        datasets: [
          {
            data: this.expenseAmount
          },
        ],
        labels: this.expenseLabels
      }
    }
  },
  async mounted() {
    await this.getAllExpenses();
    await this.fillChartData();
  },
  methods: {
    ...mapActions({
      getAllExpenses: expenseTypes.GET_ALL_EXPENSES_ACTION,
    }),
  },
};
</script>

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

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