【问题标题】:Scientific notation to decimal notation not working in highcharts科学记数法到十进制记数法在 highcharts 中不起作用
【发布时间】:2019-07-26 13:37:24
【问题描述】:

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

当数字为 e-7 时,图表不转换科学 符号转换为数字符号。

这是我的代码。

     $(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);

});

如果序列号是 e-6,那么转换没有任何 问题。

以前有人遇到过这个问题吗?

This is the fiddle for the same.

谁能帮帮我?

【问题讨论】:

    标签: javascript charts highcharts scientific-notation


    【解决方案1】:

    我的理解是 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 值太大,您可以开始切割尾随零以获得可变长度标签,但这可能不是最好的。一个粗略的例子(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;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-30
      • 2012-11-29
      相关资源
      最近更新 更多