gpt4 book ai didi

javascript - 如何使用 Chart.js 在圆环图中添加文本?

转载 作者:IT王子 更新时间:2023-10-29 02:47:09 44 4
gpt4 key购买 nike

如何在圆环图中呈现文本,我正在使用 ChartJs .

最佳答案

其他答案均未根据文本量和 donut 大小调整文本大小。这是一个小脚本,您可以使用它在中间动态放置任意数量的文本,它会自动调整大小。

示例: http://jsfiddle.net/kdvuxbtj/

Doughnut With Dynamic Text in the Middle

它会在 donut 中放置任意数量的文本,其大小非常适合 donut 。为避免接触边缘,您可以将侧边距设置为圆内直径的百分比。如果您不设置它,它将默认为 20。您还可以设置颜色、字体和文本。该插件负责其余的工作。

插件代码将以 30px 的基本字体大小开始。它会从那里检查文本的宽度,并将其与圆的半径进行比较,并根据圆/文本的宽度比调整它的大小。

它的默认最小字体大小为 20 像素。如果文本超出最小字体大小的界限,它将环绕文本。换行时的默认行高为 25px,但您可以更改它。如果将默认的最小字体大小设置为 false,文本将变得无限小并且不会换行。

它还有一个默认的最大字体大小 75px,以防没有足够的文本并且字体太大。

这是插件代码

Chart.pluginService.register({
beforeDraw: function(chart) {
if (chart.config.options.elements.center) {
// Get ctx from string
var ctx = chart.chart.ctx;

// Get options from the center object in options
var centerConfig = chart.config.options.elements.center;
var fontStyle = centerConfig.fontStyle || 'Arial';
var txt = centerConfig.text;
var color = centerConfig.color || '#000';
var maxFontSize = centerConfig.maxFontSize || 75;
var sidePadding = centerConfig.sidePadding || 20;
var sidePaddingCalculated = (sidePadding / 100) * (chart.innerRadius * 2)
// Start with a base font of 30px
ctx.font = "30px " + fontStyle;

// Get the width of the string and also the width of the element minus 10 to give it 5px side padding
var stringWidth = ctx.measureText(txt).width;
var elementWidth = (chart.innerRadius * 2) - sidePaddingCalculated;

// Find out how much the font can grow in width.
var widthRatio = elementWidth / stringWidth;
var newFontSize = Math.floor(30 * widthRatio);
var elementHeight = (chart.innerRadius * 2);

// Pick a new font size so it will not be larger than the height of label.
var fontSizeToUse = Math.min(newFontSize, elementHeight, maxFontSize);
var minFontSize = centerConfig.minFontSize;
var lineHeight = centerConfig.lineHeight || 25;
var wrapText = false;

if (minFontSize === undefined) {
minFontSize = 20;
}

if (minFontSize && fontSizeToUse < minFontSize) {
fontSizeToUse = minFontSize;
wrapText = true;
}

// Set font settings to draw it correctly.
ctx.textAlign = 'center';
ctx.textBaseline = 'middle';
var centerX = ((chart.chartArea.left + chart.chartArea.right) / 2);
var centerY = ((chart.chartArea.top + chart.chartArea.bottom) / 2);
ctx.font = fontSizeToUse + "px " + fontStyle;
ctx.fillStyle = color;

if (!wrapText) {
ctx.fillText(txt, centerX, centerY);
return;
}

var words = txt.split(' ');
var line = '';
var lines = [];

// Break words up into multiple lines if necessary
for (var n = 0; n < words.length; n++) {
var testLine = line + words[n] + ' ';
var metrics = ctx.measureText(testLine);
var testWidth = metrics.width;
if (testWidth > elementWidth && n > 0) {
lines.push(line);
line = words[n] + ' ';
} else {
line = testLine;
}
}

// Move the center up depending on line height and number of lines
centerY -= (lines.length / 2) * lineHeight;

for (var n = 0; n < lines.length; n++) {
ctx.fillText(lines[n], centerX, centerY);
centerY += lineHeight;
}
//Draw text in center
ctx.fillText(line, centerX, centerY);
}
}
});

并且您在图表对象中使用以下选项

options: {
elements: {
center: {
text: 'Red is 2/3 the total numbers',
color: '#FF6384', // Default is #000000
fontStyle: 'Arial', // Default is Arial
sidePadding: 20, // Default is 20 (as a percentage)
minFontSize: 20, // Default is 20 (in px), set to false and text will not wrap.
lineHeight: 25 // Default is 25 (in px), used for when text wraps
}
}
}

归功于 @Jenna Sloan寻求有关此解决方案中使用的数学的帮助。

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

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