首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何更新vue-chartjs中的道具更改图表

如何更新vue-chartjs中的道具更改图表
EN

Stack Overflow用户
提问于 2022-10-20 20:55:42
回答 1查看 78关注 0票数 0

首先,我要强调的是,我对Vue (以及webdev )来说是一个全新的角色。

我必须创建一个UI,其中必须显示从API中获取的一些数据。但是我对chart.js有一些问题:当道具改变时,我无法更新我的图表。目前,我有以下设置:

Card.vue:

代码语言:javascript
复制
<script>
    import DoughnutChart from './DoughnutChart.js'
    export default {
        name: 'Card',
        components: {
            DoughnutChart
        },
        props: ['scheda'],
        created() {
            console.log(this.scheda)
        }
    }
</script>
<template>
   <DoughnutChart type="abcd" :chartData="this.scheda"/>
</template>

DoughnutChart.js:

代码语言:javascript
复制
import { defineComponent, h } from 'vue'

import { Doughnut } from 'vue-chartjs'
import {
    Chart as ChartJS,
    Title,
    Tooltip,
    Legend,
    ArcElement,
    CategoryScale
} from 'chart.js'

ChartJS.register(Title, Tooltip, Legend, ArcElement, CategoryScale)

export default defineComponent({
    name: 'DoughnutChart',
    components: {
        Doughnut
    },
    props: ['type', 'chartData'],
    setup(props) {
      const chartData = {
      labels: ['A', 'B'],
      datasets: [
        {
          backgroundColor: ["#ff3333", "#131b2e"],
          cutout: "75%",
          borderWidth: 0,
          data: [props.chartData.value, (100 - props.chartData.value)]
        }
      ]
    }  
    const chartOptions = {
      plugins: {
        legend: {
            display: false
        }
      },
      responsive: true,
      maintainAspectRatio: false
    }  
    return () =>
      h(Doughnut, {
        chartData,
        chartOptions
      })
  }
})

this.scheda是一个反应性值,当它改变时,图表应该相应地更新。

我读过“文档”,但我简直无法理解它。我也尝试过在互联网上搜索,但是所有的例子都引用了图表库的旧版本。有人能帮我解决这个问题吗?谢谢!

EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74146228

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档