gpt4 book ai didi

javascript - Chart.js 如何增加段大小以使其分布更均匀

转载 作者:行者123 更新时间:2023-11-28 03:37:04 25 4
gpt4 key购买 nike

我试图表示一些之间存在很大差异的数据,例如 [500, 30, 20, 5, 1, 1],但我的图表并不真正可读。我怎样才能使饼图中的每个部分分布得更均匀。

async function drawPieChart(data, labels) {
if (myChartPie != null) {
myChartPie.destroy();
resetCanvas();
}

let ctx = document.getElementById('myChart').getContext('2d');
myChartPie = new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
label: "text",
data: data,
backgroundColor:(() => {
let bgcolors = []
let count = 0;
for (elem in data) {
count+=2;
bgcolors.push(Chart['colorschemes'].brewer.BuPu9[count])
if (count === 8) {
count = 0;
}
}
return bgcolors
})
}]
},
options: {
percentageInnerCutout: 80,
padding: 10,
plugins: {
datalabels: {
anchor: 'end',
align:'start',
color: '#fff',
borderWidth: 1,
borderColor: '#fff',
offset: -10,
borderRadius:100,
backgroundColor: "black",
font: {
weight: 'bold',
size: '15'
},
formatter: function(value) {
return value.scm_count;
}
}
},
title: {
display: true,
text: 'Count versions of SCM',
fontSize: 25,
fontStyle: 'bold',
fontColor: 'black',
},
legend: {
padding: 30,
display: true,
fontColor: 'rgb(255, 99, 132)',
position: "right",
backgroundColor:(() => {
let bgcolors = []
let count = 0;
for (elem in data) {
count+=2;
bgcolors.push(Chart['colorschemes'].brewer.BuPu9[count])
if (count === 8) {
count = 0;
}
}
return bgcolors
})
},
layout: {
padding: {
left: 0,
right: 0,
top: 0,
bottom: 50
}},
responsive: true,
},
maintainAspectRatio: false,
});
Chart.plugins.register({
beforeDraw: function(c) {
var legends = c.legend.legendItems;
let i = 0;
legends.forEach(function(e) {
i+=2;
e.fillStyle = Chart['colorschemes'].brewer.BuPu9[i];
if (i ===8)
{
i = 0;
}
});
}
});

};

我希望较小的尺寸会增加一点以便可见。

我想以某种方式用百分比来表示它们,但我找不到任何方法来做到这一点。

最佳答案

您确实无法在饼图(或任何)图表中按比例显示非常小的值。

现在您可以更改比例以使用对数(请注意 Math.log(1),例如 ln(1) 为 0)。像这样的事情:

async function drawPieChart(data, labels) {
if (myChartPie != null) {
myChartPie.destroy();
resetCanvas();
}

let ctx = document.getElementById('myChart').getContext('2d');
myChartPie = new Chart(ctx, {
type: 'pie',
data: {
labels: labels,
datasets: [{
label: "text",
data: data,
backgroundColor:(() => {
let bgcolors = []
let count = 0;
for (elem in data) {
count+=2;
bgcolors.push(Chart['colorschemes'].brewer.BuPu9[count])
if (count === 8) {
count = 0;
}
}
return bgcolors
})
}]
},
options: {
percentageInnerCutout: 80,
padding: 10,
plugins: {
datalabels: {
anchor: 'end',
align:'start',
color: '#fff',
borderWidth: 1,
borderColor: '#fff',
offset: -10,
borderRadius:100,
backgroundColor: "black",
font: {
weight: 'bold',
size: '15'
},
formatter: function(value,I) {
//return value.scm_count;
return Math.exp(value).scm_count;
}
}
},
title: {
display: true,
text: 'Count versions of SCM',
fontSize: 25,
fontStyle: 'bold',
fontColor: 'black',
},
legend: {
padding: 30,
display: true,
fontColor: 'rgb(255, 99, 132)',
position: "right",
backgroundColor:(() => {
let bgcolors = []
let count = 0;
for (elem in data) {
count+=2;
bgcolors.push(Chart['colorschemes'].brewer.BuPu9[count])
if (count === 8) {
count = 0;
}
}
return bgcolors
})
},
layout: {
padding: {
left: 0,
right: 0,
top: 0,
bottom: 50
}},
responsive: true,
},
maintainAspectRatio: false,
});

Chart.plugins.register({
beforeDraw: function(c) {
var legends = c.legend.legendItems;
let i = 0;
legends.forEach(function(e) {
i+=2;
e.fillStyle = Chart['colorschemes'].brewer.BuPu9[i];
if (i ===8)
{
i = 0;
}
});
}
});

};

var myChartPie;
var orig_data = [500, 30, 20, 5, 1, 1];
var data = orig_data.map(function(e) {
if (e == 1) { e = 1.2 };
e = Math.log(e);
return e;
});
var labels = ["One", "Two", "Three", "Four", "Five", "Six"];
drawPieChart(data,labels);

https://jsfiddle.net/mattsrinc/e4dLny2b/30/

请参阅代码底部如何使用对数值调整值。不过,您必须找到一种方法来更改标签以显示其原始值。

关于javascript - Chart.js 如何增加段大小以使其分布更均匀,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57623851/

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