2016-02-08 97 views
1

我想用谷歌圖表庫創建一個折線圖。數據包含日期(x軸),數字(col 1),數字(col 2),浮點數(col 3)。Google圖表格式化程序不能修改工具提示

我想在第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)); 
} 

我試着加入格式化代碼在代碼的早些時候和之後,試圖將其應用於其他列等,似乎沒有任何工作。第三欄工具提示總是取消小數點。

我在做什麼錯?

回答

0

formatvAxes選項,這是壓倒一切的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>

+0

臨屋nks,我只是通過使用google.visualization.LineChart而不是google.charts.Line來「修復」它,你似乎也工作得如此接受答案。 – Mark

相關問題