0

我的示例目前如下所示:

var ctx = document.getElementById("myChart");
var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: [{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }]
    },
    options: {
        rotation: 1 * Math.PI,
        circumference: 1 * Math.PI,
        title: {
           display: true,
           text: 'Custom Chart Title',
           position: 'bottom'
        }
     }
});

现在我想将值的总和显示data: [12, 19, 3, 5, 2, 3]为图表的标题(因此,在这种情况下,标题为 44)。一些示例小提琴在这里:https ://jsfiddle.net/ktq8​​mb0z/ 。我怎样才能在 Chart.Js 中实现这一点?

4

2 回答 2

2

您可以用户减少功能

dataset[0].data.reduce((x,y)=>x+y)

或者你也可以使用这个

dataset.reduce((t,d) => t + d.data.reduce((a,b) => a+b),0)

var ctx = document.getElementById("myChart");

var dataset=[{
            label: '# of Votes',
            data: [12, 19, 3, 5, 2, 3],
            backgroundColor: [
                'rgba(255, 99, 132, 0.2)',
                'rgba(54, 162, 235, 0.2)',
                'rgba(255, 206, 86, 0.2)',
                'rgba(75, 192, 192, 0.2)',
                'rgba(153, 102, 255, 0.2)',
                'rgba(255, 159, 64, 0.2)'
            ],
            borderColor: [
                'rgba(255,99,132,1)',
                'rgba(54, 162, 235, 1)',
                'rgba(255, 206, 86, 1)',
                'rgba(75, 192, 192, 1)',
                'rgba(153, 102, 255, 1)',
                'rgba(255, 159, 64, 1)'
            ],
            borderWidth: 1
        }];
var myChart = new Chart(ctx, {
    type: 'doughnut',
    data: {
        labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
        datasets: dataset
    },
    options: {
        rotation: 1 * Math.PI,
        circumference: 1 * Math.PI,
        title: {
           display: true,
           text: 'Custom Chart Title and Sum is : '+dataset.reduce((t,d) => t + d.data.reduce((a,b) => a+b),0),
           position: 'bottom'
        }
     }
});
.snippet-code .snippet-result .snippet-result-code{
    min-height:640px!important;
    height:640px!important;
}
<script src="https://cdn.jsdelivr.net/npm/chart.js@2.8.0"></script>
<canvas id="myChart" width="400" height="400"></canvas>

于 2019-04-02T10:18:17.503 回答
0

从 ChartJS 版本 3.xx 开始:

  • half-doughnut 配置有度数
  • 标题是“插件”的一部分

因此,使用此配置:

options: {
  rotation: -90,
  circumference: 180,
    plugins: {
      title: {
        display: true,
        text: 'Total: ' + data.reduce((total, dataPoint) => total + dataPoint, 0),
        position: 'bottom',
      },
    },
}
于 2021-11-23T11:08:19.867 回答