首先,我要强调的是,我对Vue (以及webdev )来说是一个全新的角色。
我必须创建一个UI,其中必须显示从API中获取的一些数据。但是我对chart.js有一些问题:当道具改变时,我无法更新我的图表。目前,我有以下设置:
Card.vue:
<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:
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是一个反应性值,当它改变时,图表应该相应地更新。
我读过“文档”,但我简直无法理解它。我也尝试过在互联网上搜索,但是所有的例子都引用了图表库的旧版本。有人能帮我解决这个问题吗?谢谢!
https://stackoverflow.com/questions/74146228
复制相似问题