gpt4 book ai didi

javascript - 如何在 Chart.JS 中的半圆环图中间添加一些文本?

转载 作者:行者123 更新时间:2023-11-29 10:27:24 26 4
gpt4 key购买 nike

它应该看起来像这样:

我已经有了图表,只是缺少文字。是否可以选择在图表中间显示文本?

编辑:可以在此处找到此类图表的示例:https://jsfiddle.net/wt4260qf/1/

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

enter image description here

最佳答案

感谢@DTul , 我想到了。标题配置在这里:https://www.chartjs.org/docs/latest/configuration/title.html .

我添加了以下部分:

title: {
display: true,
text: 'Custom Chart Title',
position: 'bottom'
}

选项

整个例子现在看起来像这样:

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

相应的 fiddle 在这里:https://jsfiddle.net/ktq8mb0z/

关于javascript - 如何在 Chart.JS 中的半圆环图中间添加一些文本?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55459077/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com