英文:
ChartJS Everything under one label?
问题
I have created a doughnut chart using Chart.js, but I'm facing a small issue. The data under the chart's main label is not filtering correctly; instead, it's all grouped under one label. I'm also experiencing a problem where my chart looks like concentric circles instead of a typical doughnut chart, similar to the example here: Chart.js Doughnut Example.
Here's your translated code snippet:
<script>
var ctx = document.getElementById("racionMix").getContext('2d');
var racionChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [],
datasets: []
},
options: {
plugins: {
title: {
display: true,
fontSize: 16,
text: 'Racion Mix'
},
}
}
});
@for (int index = 0; index < @Model.Count; index++) {
@:addData("@Model[index].MixName", "@Model[index].TotalQty", getRandomColor())
}
function addData(label, value, color) {
this.racionChart.data.labels.push(label);
var newDataset = {
label: label,
backgroundColor: color,
data: value,
}
this.racionChart.data.datasets.push(newDataset);
this.racionChart.update();
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
If you have any specific questions or need assistance with resolving the issues you mentioned, please feel free to ask.
英文:
I have to create doughnut chart with chartjs, and I had manage to create.
But there is small issue, under let's call content of chart I have different colors and data, but from main label from where should filter it I get all data under one label, why, what I did wrong ?
<script>
var ctx = document.getElementById("racionMix").getContext('2d');
var racionChart = new Chart(ctx,
{
type: 'doughnut',
data: {
labels: [],
datasets: [
]
},
// Configuration options go here
options: {
plugins: {
title: {
display: true,
fontSize: 16,
text: 'Racion Mix'
},
}
}
});
@for (int index = 0; index < @Model.Count; index++)
{
@:addData( "@Model[index].MixName", "@Model[index].TotalQty", getRandomColor())
}
function addData(label, value, color) {
this.racionChart.data.labels.push(label);
var newDataset = {
label: label,
backgroundColor: color,
data: value,
}
this.racionChart.data.datasets.push(newDataset);
this.racionChart.update();
}
function getRandomColor() {
var letters = '0123456789ABCDEF';
var color = '#';
for (var i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
</script>
Also there one more think which is confusing me why I do not get chart
like here https://www.chartjs.org/docs/latest/samples/other-charts/doughnut.html
On my chart seems like circle in circle...
答案1
得分: 1
这是因为你创建了新的数据集,而不是将数据添加到现有数据集中。每个数据集都是独立的圆圈。如果你将 addData
函数和图表实例化更改为以下内容,它将按预期工作:
var racionChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [],
datasets: [{
label: '在图例中显示的文本',
data: [],
backgroundColor: []
}]
},
// 配置选项在此处
options: {
plugins: {
title: {
display: true,
fontSize: 16,
text: 'Racion混合'
},
}
}
});
function addData(label, value, color) {
this.racionChart.data.labels.push(label);
this.racionChart.data.datasets[0].data.push(value);
this.racionChart.data.datasets[0].backgroundColor.push(color);
this.racionChart.update();
}
英文:
This is happening because you make new datasets instead of adding to a dataset. Each dataset is its own circle. If you change your addData
function and chart instantiation to the following it will work as expected:
var racionChart = new Chart(ctx, {
type: 'doughnut',
data: {
labels: [],
datasets: [{
label: 'text that appears in the legend',
data: [],
backgroundColor: []
}]
},
// Configuration options go here
options: {
plugins: {
title: {
display: true,
fontSize: 16,
text: 'Racion Mix'
},
}
}
});
function addData(label, value, color) {
this.racionChart.data.labels.push(label);
this.racionChart.data.datasets[0].data.push(value);
this.racionChart.data.datasets[0].backgroundColor.push(color);
this.racionChart.update();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论