Google图表格式化程序不会修改工具提示

时间:2016-02-08 18:31:18

标签: javascript charts google-visualization

我正在尝试使用谷歌图表库创建折线图。

数据包含,日期(x轴),数字(col 1),数字(col 2),float(col 3)。

我想在第3列工具提示上显示两位小数,同时保持y轴0到100,这是我当前的代码(在这里运行https://jsfiddle.net/uqh56hsu/1/):

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

function drawChart() {

  var data = new google.visualization.DataTable();
  data.addColumn('datetime', 'Hours');
  data.addColumn('number', 'col1');
  data.addColumn('number', 'col2');
  data.addColumn('number', 'percent');

  data.addRows([
[new Date(1454950800*1000),0,0,0],[new Date(1454947200*1000),0,0,0],[new Date(1454943600*1000),2,0,0.00],[new Date(1454940000*1000),24,1,4.17],[new Date(1454936400*1000),12,1,8.33],[new Date(1454932800*1000),64,4,6.25],[new Date(1454929200*1000),176,11,6.25],[new Date(1454925600*1000),142,7,4.93],[new Date(1454922000*1000),114,7,6.14],[new Date(1454918400*1000),0,0,0],[new Date(1454914800*1000),0,0,0],[new Date(1454911200*1000),0,0,0],[new Date(1454907600*1000),0,0,0],[new Date(1454904000*1000),0,0,0],[new Date(1454900400*1000),0,0,0],[new Date(1454896800*1000),0,0,0],[new Date(1454893200*1000),0,0,0],[new Date(1454889600*1000),0,0,0],[new Date(1454886000*1000),0,0,0],[new Date(1454882400*1000),0,0,0],[new Date(1454878800*1000),0,0,0],[new Date(1454875200*1000),0,0,0],[new Date(1454871600*1000),180,10,5.56],      ]);

  var formatter = new google.visualization.NumberFormat({
                fractionDigits: 2,
                suffix: '%'
  });
  formatter.format(data, 3);

  var options = {
    width: 900,
    height: 500,
            backgroundColor: '#f1f1f1',
            colors: ['#ff851b', '#03a9f4', '#8dc859'],
            dateFormat: 'H',
            vAxes:[
                { titleTextStyle: {color: '#FF0000'}},
                { titleTextStyle: {color: '#FF0000'}, minValue: 0, maxValue: 100, format: '#\'%\'', viewWindowMode : 'explicit', viewWindow:{
            max:100,
            min:0
          }}
            ],
            series:[
            {targetAxisIndex:0},
            {targetAxisIndex:0},
            {targetAxisIndex:1}
        ]
  };


  var chart = new google.charts.Line(document.getElementById('linechart_material'));
  chart.draw(data, google.charts.Line.convertOptions(options));
}

我已尝试在代码中添加格式化程序代码,稍后在代码中添加格式代码,尝试将其应用于其他列等,似乎没有任何效果。第3列工具提示始终会删除小数。

我做错了什么?

1 个答案:

答案 0 :(得分:0)

正在format选项中设置vAxes,该选项覆盖了formatter ...

只需将格式设置为 - > format: '#,##0.00\'%\''

此处无需formatter ...



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

function drawChart() {
  var data = new google.visualization.DataTable();
  data.addColumn('datetime', 'Hours');
  data.addColumn('number', 'col1');
  data.addColumn('number', 'col2');
  data.addColumn('number', 'percent');

  data.addRows([
    [new Date(1454950800*1000),0,0,0],
    [new Date(1454947200*1000),0,0,0],
    [new Date(1454943600*1000),2,0,0.00],
    [new Date(1454940000*1000),24,1,4.17],
    [new Date(1454936400*1000),12,1,8.33],
    [new Date(1454932800*1000),64,4,6.25],
    [new Date(1454929200*1000),176,11,6.25],
    [new Date(1454925600*1000),142,7,4.93],
    [new Date(1454922000*1000),114,7,6.14],
    [new Date(1454918400*1000),0,0,0],
    [new Date(1454914800*1000),0,0,0],
    [new Date(1454911200*1000),0,0,0],
    [new Date(1454907600*1000),0,0,0],
    [new Date(1454904000*1000),0,0,0],
    [new Date(1454900400*1000),0,0,0],
    [new Date(1454896800*1000),0,0,0],
    [new Date(1454893200*1000),0,0,0],
    [new Date(1454889600*1000),0,0,0],
    [new Date(1454886000*1000),0,0,0],
    [new Date(1454882400*1000),0,0,0],
    [new Date(1454878800*1000),0,0,0],
    [new Date(1454875200*1000),0,0,0],
    [new Date(1454871600*1000),180,10,5.56]
  ]);

  var options = {
    width: 900,
    height: 500,
    backgroundColor: '#f1f1f1',
    colors: ['#ff851b', '#03a9f4', '#8dc859'],
    dateFormat: 'H',
    vAxes:[
      {
        titleTextStyle: {color: '#FF0000'}
      },
      {
        titleTextStyle: {
          color: '#FF0000'
        },
        minValue: 0,
        maxValue: 100,

        format: '#,##0.00\'%\'',  // set format here -- formatter not needed

        viewWindowMode : 'explicit',
        viewWindow: {
          max:100,
          min:0
        }
      }
    ],
    series:[
      {targetAxisIndex:0},
      {targetAxisIndex:0},
      {targetAxisIndex:1}
    ]
  };

  var chart = new google.charts.Line(document.getElementById('linechart_material'));
  chart.draw(data, google.charts.Line.convertOptions(options));
}

<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="linechart_material"></div>
&#13;
&#13;
&#13;