ChartJS 一切都在一个标签下吗?

huangapple go评论57阅读模式
英文:

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 ?
ChartJS 一切都在一个标签下吗?

&lt;script&gt;


    var ctx = document.getElementById(&quot;racionMix&quot;).getContext(&#39;2d&#39;);
    var racionChart = new Chart(ctx,
        {
            type: &#39;doughnut&#39;,
            data: {
                labels: [],

                datasets: [

                ]
            },
            // Configuration options go here
            options: {
                plugins: {
                    title: {
                        display: true,
                        fontSize: 16,
                        text: &#39;Racion Mix&#39;
                    },
                }
            }
        });



    @for (int index = 0; index &lt; @Model.Count; index++)
    {
        @:addData( &quot;@Model[index].MixName&quot;, &quot;@Model[index].TotalQty&quot;, 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 = &#39;0123456789ABCDEF&#39;;
        var color = &#39;#&#39;;
        for (var i = 0; i &lt; 6; i++) {
            color += letters[Math.floor(Math.random() * 16)];
        }
        return color;
    }




&lt;/script&gt;

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: &#39;doughnut&#39;,
    data: {
        labels: [],
        datasets: [{
            label: &#39;text that appears in the legend&#39;,
            data: [],
            backgroundColor: []
        }]
    },
    // Configuration options go here
    options: {
        plugins: {
            title: {
                display: true,
                fontSize: 16,
                text: &#39;Racion Mix&#39;
            },
        }
    }
});

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

huangapple
  • 本文由 发表于 2023年6月13日 01:23:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458964.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定