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

Vue.js 如何在Vue Chart.js中使用Api数据,vue.js,chart.js,vuex,vue-chartjs,Vue.js,Chart.js,Vuex,Vue Chartjs,因此,我不熟悉在应用程序中使用数据可视化,我试图将来自api的数据设置为甜甜圈图表用于显示的数据,但我无法确定如何正确访问数据 我安装了vue chartjs,以简化组件使用 这是图表组件 <script> import { Doughnut } from 'vue-chartjs' import {mapGetters} from 'vuex' export default { name: 'FirmChart', extends: Doughnut, c

因此,我不熟悉在应用程序中使用数据可视化,我试图将来自api的数据设置为甜甜圈图表用于显示的数据,但我无法确定如何正确访问数据

我安装了vue chartjs,以简化组件使用

这是图表组件

<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>
我的问题是如何将
Complete:Array(1)
etc设置为我的
this.renderChart()方法中的
data[]
属性

我已经尝试过这样做,但它不会显示任何内容

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

但是它没有显示任何内容。。任何帮助都将不胜感激,或者是一个正确的方向

您查看了文档中的示例了吗?

您的问题是,您的数据api调用是异步的。因此,即使数据未完全加载,也会呈现图表

还有一个vuex的例子,它有点过时

在渲染图表之前,必须确保数据已完全加载

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