我正在构建一个包含一些超小值的图表。我的问题是
when the number is e-7, the chart doesn't convert the scientific notation to numeric notation.
这是我的代码。
$(document).ready(function() {
var data = [[1532476800000,9.888550207585507e-7],[1532995200000,9.566888695224366e-7],[1533081600000,9.217248059141058e-7]];
var options = {
chart: {
renderTo: 'container',
type: 'line',
},
title: {
text: 'Fruit Consumption'
},
xAxis:{
type:'datetime'
},
yAxis: {
title: {
text: 'Fruits Amount'
}
},
series: [{}]
};
options.series[0].data = data;
var chart = new Highcharts.Chart(options);
});
And if the series number is e-6, then the conversion doesn't have any problem.
有没有人遇到过这个问题?
This is the fiddle for the same.
谁能帮帮我?
最佳答案
我的理解是 Highcharts 中的标签只是使用数字的值,这就是 Javascript 打印这些数字的方式。例如,在 Node 中演示:
$ node
> 0.0000009888550207585507
9.888550207585507e-7
> 0.000009888550207585507
0.000009888550207585507
那你能做什么?您可以使用 toFixed
( documentation ) 始终以给定的精度表示它。但是请注意,这有大约 20 个的限制,并且需要您选择一个特定的数字并成为一个字符串。然而它很简单(JSFiddle demo):
yAxis: {
labels: {
formatter: function() {
return this.value.toFixed(8);
}
},
}
如果您使用具有太大值的 toFixed
,您可以开始 chop 尾随零以获得可变长度标签,但这可能不是最好的。一个粗略的例子(JSFiddle demo):
yAxis: {
labels: {
formatter: function() {
let strValue = this.value.toFixed(20);
for(let i = strValue.length - 1; i >= 0; i--) {
if(strValue.charAt(i) != '0') {
strValue = strValue.substring(0, i + 1);
break;
}
}
return strValue;
}
}
}
关于javascript - 十进制表示法的科学记数法在 Highcharts 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57221238/