英文:
How to fix Chart.js doughnut chart with missing background colors and missing labels when working with props?
问题
我正在使用Chart.js来显示一个圆环图(doughnut chart)。我想使用props来进行一些API调用,并显示不同的圆环图,展示各种数据。我的问题是我的代码没有完全工作。所有不同的部分都显示出来了,但没有背景颜色。有人可以帮助我解决这个问题吗?
以下是您提供的代码中的翻译部分:
<!-- DoughnutChart -->
<template>
<div>
<canvas id="myChart" width="80%" height="80%"></canvas>
<p>这是我的图表 <h1>{{ topLabel }}</h1></p>
</div>
</template>
<script>
import { Chart, ArcElement, DoughnutController } from 'chart.js';
Chart.register(DoughnutController, ArcElement);
export default {
name: "doughnut-chart",
props: {
ValuesLabel: {
type: Array,
required: true,
default: ["value1", "value2", "value3", "value4", "value5"]
},
topLabel: {
type: String,
required: true,
default: "这是我的图表"
},
dataChart: {
type: Array,
required: true,
default: [10, 20, 30, 40, 50]
}
},
mounted() {
console.log("我的圆环图已挂载");
console.log()
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: this.ValuesLabel,
datasets: [
{
label: this.topLabel,
data: this.dataChart,
}
]
}
});
return myChart;
}
};
</script>
这是我的预算视图组件:
<!-- myBudgetView -->
<template>
<div>
<h1>我的预算</h1>
<doughnut-chart
:ValuesLabel="labels"
:topLabel="topLabel"
:dataChart="dataChart"
/>
</div>
</template>
<script>
import DoughnutChart from "../components/Charts/DoughnutChart.vue";
export default {
components: {
DoughnutChart
},
name: "myBudgetView",
// Data section
data() {
return {
labels: ['水果', '蔬菜', '肉类', '奶制品', '零食'],
topLabel: "我的开支",
dataChart: [12, 20, 33, 40, 51]
};
}
};
</script>
这是我得到的结果:
我期望得到与每个部分都有颜色的相同结果,如下所示:
英文:
I am using Chart.js in order to display a doughnut chart. I want to use props so that I could do some API calls and have different doughnut charts with various data showing up. My issue is that my code is not fully working. I have all the different sections showing up but with no background color. Can someone help me fix this ?
<!-- DoughnutChart -->
<template>
<div>
<canvas id="myChart" width="80%" height="80%"></canvas>
<p>Here is my chart <h1>{{ topLabel }}</h1></p>
</div>
</template>
<script>
import { Chart, ArcElement, DoughnutController } from 'chart.js';
Chart.register(DoughnutController, ArcElement);
export default {
name: "doughnut-chart",
props: {
ValuesLabel: {
type: Array,
required: true,
default: ["value1", "value2", "value3", "value4", "value5"]
},
topLabel: {
type: String,
required: true,
default: "Here is my chart"
},
dataChart: {
type: Array,
required: true,
default: [10, 20, 30, 40, 50]
}
},
mounted() {
console.log("My doughnut chart is mounted");
console.log()
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: this.ValuesLabel,
datasets: [
{
label: this.topLabel,
data: this.dataChart,
}
]
}
});
return myChart;
}
};
</script>
here is my budget view component
<-- myBudgetView -->
<template>
<div>
<h1>My Budget</h1>
<doughnut-chart
:ValuesLabel="labels"
:topLabel="topLabel"
:dataChart="dataChart"
/>
</div>
</template>
<script>
import DoughnutChart from "../components/Charts/DoughnutChart.vue";
export default {
components: {
DoughnutChart
},
name: "myBudgetView",
// Data section
data() {
return {
labels: ['fruits', 'veggies', 'meat', 'dairy', 'snacks'],
topLabel: "My expenses",
dataChart: [12, 20, 33, 40, 51]
};
}
};
</script>
here is what I get
enter image description here
I expect the same thing with some color in each section like so :
enter image description here
答案1
得分: 1
在我的代码中存在两个问题:
- 未导入Colors
- 插件 > 启用:false
英文:
there were two issues in my code :
- not importing Colors
- plugin > enabled : false
答案2
得分: 0
我将以更好的方式展示我是如何解决这个问题的。
我定义了一个名为option的对象,如下所示:
const options = {
plugins: {
colors: {
enabled: true
}
}
};
然后,我获取了canvas的上下文对象:
const ctx = document.getElementById('myChart').getContext('2d');
接着,我创建了一个Chart实例:
const myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: this.ValuesLabel,
datasets: [
{
label: this.topLabel,
data: this.dataChart,
}
]
},
options: options
});
最后,我返回了myChart。
使用这种方法,您可以忘记导入'Colors',因为它不再作为库存在,而是作为一个插件存在。
但是,如果您需要在同一个Vue应用程序中创建多个图表,似乎是不可能的,您必须使用一个使用SVG标签而不是Canvas的库。我尝试过使用Chart.js和SVG标签,但是这是不可能的。
英文:
I will show in a better way how I did solve the issue.
I defined an object called option like so :
const options = {
plugins: {
colors: {
enabled: true
}
}
};
const ctx = document.getElementById('myChart').getContext('2d');
const myChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: this.ValuesLabel,
datasets: [
{
label: this.topLabel,
data: this.dataChart,
}
]
},
options : options
});
return myChart;
with this method you can forget about importing 'Colors' because it doesn't exist anymore as a lib. It is now a plug-in.
However, if you need to createt multiple charts in the same vue-app, it seems to be
impossible and you have to use a lib that uses svg tags instead of canvas. I tried
to use svg tags with chart.js but it is impossible.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论