gpt4 book ai didi

javascript - 图例中的 Chartjs 百分比

转载 作者:行者123 更新时间:2023-12-01 03:09:32 25 4
gpt4 key购买 nike

我正在努力获取数据点类别之间的百分比以显示在自定义图例中。我知道我已经很接近了(因为我已经让它们为工具提示工作),但我还无法完全破解它。

现在我在每个类别中有 1 个项目,我的工具提示显示每个类别的 25%(正确),但我的图例显示每个类别的 1%,哈哈,显然是错误的。

这是我的配置:

var chart = new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [{
data: getValues,
backgroundColor: getColorValues,
}],
labels: getLabels
},
options: {
responsive: true,
tooltips: {
callbacks: {
label: function(tooltipItem, data) {

var dataset = data.datasets[tooltipItem.datasetIndex];

var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});

var currentValue = dataset.data[tooltipItem.index];

var precentage = Math.floor(((currentValue / total) * 100) + 0.5);

return precentage + "%";
}
}
},
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;

if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
if (labels[i]) {
text.push(labels[i] + ' (' + datasets[0].data[i] + '%)');
}
text.push('</li>');
}
}
text.push('</ul>');
return text.join('');
},
legend: {
display: false,
},
elements: {
arc: {
borderWidth: 0
}
},
cutoutPercentage: 70,
title: {
display: true
},
animation: {
animateScale: true,
animateRotate: true
}
}
});

document.getElementById('js-legend').innerHTML = chart.generateLegend();

我非常感谢任何帮助!

最佳答案

将您的 legendCallback 函数替换为以下内容:

legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');
var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;
if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
if (labels[i]) {
// calculate percentage
var total = datasets[0].data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});
var currentValue = datasets[0].data[i];
var percentage = Math.floor(((currentValue / total) * 100) + 0.5);

text.push(labels[i] + ' (' + percentage + '%)');
}
text.push('</li>');
}
}
text.push('</ul>');
return text.join('');
}

基本上,您还需要计算图例标签的百分比,就像计算工具提示一样。

ᴡᴏʀᴋɪɴɢ ᴇxᴀᴍᴘʟᴇ ⧩

var getValues = [1, 2, 3],
getLabels = ['Jan', 'Feb', 'Mar'],
getColorValues = ['red', 'green', 'blue']
var chart = new Chart(ctx, {
type: 'doughnut',
data: {
datasets: [{
data: getValues,
backgroundColor: getColorValues,
}],
labels: getLabels
},
options: {
responsive: true,
tooltips: {
callbacks: {
label: function(tooltipItem, data) {

var dataset = data.datasets[tooltipItem.datasetIndex];

var total = dataset.data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});

var currentValue = dataset.data[tooltipItem.index];

var precentage = Math.floor(((currentValue / total) * 100) + 0.5);

return precentage + "%";
}
}
},
legendCallback: function(chart) {
var text = [];
text.push('<ul class="' + chart.id + '-legend">');

var data = chart.data;
var datasets = data.datasets;
var labels = data.labels;

if (datasets.length) {
for (var i = 0; i < datasets[0].data.length; ++i) {
text.push('<li><span style="background-color:' + datasets[0].backgroundColor[i] + '"></span>');
if (labels[i]) {

// calculate percentage
var total = datasets[0].data.reduce(function(previousValue, currentValue, currentIndex, array) {
return previousValue + currentValue;
});
var currentValue = datasets[0].data[i];
var precentage = Math.floor(((currentValue / total) * 100) + 0.5);

text.push(labels[i] + ' (' + precentage + '%)');
}
text.push('</li>');
}
}
text.push('</ul>');
return text.join('');
},
legend: {
display: false,
},
elements: {
arc: {
borderWidth: 0
}
},
cutoutPercentage: 70,
title: {
display: true
},
animation: {
animateScale: true,
animateRotate: true
}
}
});

document.getElementById('js-legend').innerHTML = chart.generateLegend();
<script src="https://cdnjs.cloudflare.com/ajax/libs/Chart.js/2.6.0/Chart.min.js"></script>
<canvas id="ctx"></canvas>
<div id="js-legend"></div>

关于javascript - 图例中的 Chartjs 百分比,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45940338/

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