【问题标题】:rounding value in google chart谷歌图表中的舍入值
【发布时间】:2018-04-11 04:26:28
【问题描述】:

我正在使用谷歌图表,当使用非常准确的十进制数字时,GG 图表倾向于将其四舍五入到小数点后 3 位

你可以在这个例子中看到它:

      google.charts.load('current', {'packages':['corechart']});
  google.charts.setOnLoadCallback(drawChart);

  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Age', 'Weight'],
      [ 8,      3.535778],
      [ 4,      5.5],
      [ 11,     14],
      [ 4,      5],
      [ 3,      3.535678],
      [ 6.5,    7]
    ]);

    var options = {
      title: 'Age vs. Weight comparison',
      hAxis: {title: 'Age', minValue: 0, maxValue: 15},
      vAxis: {title: 'Weight', minValue: 0, maxValue: 15},
      legend: 'none'
    };

    var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));

    chart.draw(data, options);
  }

https://jsfiddle.net/00a1sut0/2/

数据中有2个数值分别为3.535778和3.535678,GG图标记为3.536

我想显示正确的十进制数

任何有想法的人??

【问题讨论】:

    标签: angular typescript charts decimal scatter


    【解决方案1】:

    使用NumberFormat 显示精确值或定义值的模式

    数据表创建后添加该位

    var NumberFormat = new google.visualization.NumberFormat(
    //You can explore various type of patterns in Documentation
    {pattern:'##.#######'}
    );
    NumberFormat.format(data, 1);
    

    NumberFormat 接受数据表和应该具有模式format(dataTable, columnIndex) 的列

    总体代码

      google.charts.load('current', {'packages':['corechart']});
      google.charts.setOnLoadCallback(drawChart);
    
      function drawChart() {
        var data = google.visualization.arrayToDataTable([
          ['Age', 'Weight'],
          [ 8,      3.535778],
          [ 4,      5.5],
          [ 11,     14],
          [ 4,      5],
          [ 3,      3.535678],
          [ 6.5,    7]
        ]);
    
       var NumberFormat = new google.visualization.NumberFormat(
       //You can explore various type of patterns in Documentation
       {pattern:'##.#######'}
       );
       NumberFormat.format(data, 1); // Apply formatter to second column
    
        var options = {
          title: 'Age vs. Weight comparison',
          hAxis: {title: 'Age', minValue: 0, maxValue: 15},
          vAxis: {title: 'Weight', minValue: 0, maxValue: 15},
          legend: 'none'
        };
    
        var chart = new google.visualization.ScatterChart(document.getElementById('chart_div'));
    
        chart.draw(data, options);
      }
    

    在此处查看演示 - JSFiddle

    【讨论】:

    • 谢谢伙计,这个模式对我有用。但是,您是否知道无论如何将轴的范围更改为十进制范围。我的意思是,如果表格仅更改为 [ 3.25, 3.535778],[ 3, 3.535678] 的 2 个值,则图表不会自动放大以获得更好的显示效果
    • @NgocTuanLam 提出另一个问题,我会试一试。即使我做不到,其他人也会:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多