gpt4 book ai didi

javascript - chart.js 不允许使用对数刻度的 y 轴步长

转载 作者:数据小太阳 更新时间:2023-10-29 05:46:20 25 4
gpt4 key购买 nike

我不得不解析科学记数法,这是对我的图表实现对数刻度的结果,但它打印出了图表中每一行的每个值。似乎没有任何步骤方法有效。

RESULTING CHART IMG

var packetsElement = $("#packetsGraph");
pckBarChart = new Chart(packetsElement, {
type: 'bar',
data: {
labels: ["Received", "Errors", "Lost"],
datasets: [{
label: '# of Packets',
data: packetsArr,
backgroundColor: [
'rgba(55,102,245,0.3)',
'rgba(55,102,245,0.2)',
'rgba(55,102,245,0.1)'
],
borderColor: [
'#3766F5',
'#3766F5',
'#3766F5'],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Packets',
fontSize: 20
},
scales: {
yAxes: [{
type: 'logarithmic',
ticks: {
min: 1,
stepSize: 1000000,
steps: 1000000,
stepValue: 1000000,
callback: function(value, index, values) {
return parseFloat(value);
}
}
}]
}
}
});

最佳答案

我想通了,这是我所做的,以防其他人需要它:

var packetsElement = $("#packetsGraph");
pckBarChart = new Chart(packetsElement, {
type: 'bar',
data: {
labels: ["Received", "Errors", "Lost"],
datasets: [{
label: '% of Packets (Logarithmic)',
data: packetsArr,
backgroundColor: [
'rgba(55,102,245,0.3)',
'rgba(55,102,245,0.2)',
'rgba(55,102,245,0.1)'
],
borderColor: [
'#3766F5',
'#3766F5',
'#3766F5'],
borderWidth: 1
}]
},
options: {
responsive: true,
maintainAspectRatio: false,
title: {
display: true,
text: 'Packets',
fontSize: 20
},
scales: {
yAxes: [{
type: 'logarithmic',
ticks: {
min: 0,
max: 100,
callback: function(value, index, values) {//needed to change the scientific notation results from using logarithmic scale
return Number(value.toString());//pass tick values as a string into Number function
}
},
afterBuildTicks: function(pckBarChart) {
pckBarChart.ticks = [];
pckBarChart.ticks.push(0);
pckBarChart.ticks.push(25);
pckBarChart.ticks.push(50);
pckBarChart.ticks.push(75);
pckBarChart.ticks.push(100);
}
}]
},
tooltips: {
enabled: true,
mode: 'single',
callbacks: {
label: function(tooltipItems, data) {
return tooltipItems.yLabel + ' %';
}
}
},
}
});

enter image description here

关于javascript - chart.js 不允许使用对数刻度的 y 轴步长,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40183188/

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