javascript - 十进制表示法的科学记数法在 Highcharts 中不起作用

标签 javascript charts highcharts scientific-notation

我正在构建一个包含一些超小值的图表。我的问题是

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/

相关文章:

javascript - 如何防止用户在电子邮件输入字段中添加空格?

javascript - 避免简单增量器中的状态

PHP 图表 - 用值填充缺失的日期

jquery - highcharts 通过 JSON 获取数据?

highcharts - Highchart 内部 donut 名称作为外部 donut 工具提示的标题

javascript - 如何根据 Highcharts 中的选择获取系列名称

javascript - Three.js 使用 Node 模块的路径

javascript - jquery自动完成功能不适用于克隆行动态表单集django

javascript - 谷歌图表改变单个直方图数据点的颜色

ios - Core-Plot 选择一个点