2017-10-12 76 views
2

如何在具有雙x軸的谷歌條形圖上設置數字值的格式? 帶標籤支持的頂部軸應至少有四位小數位,如工具提示中顯示的值。帶雙x軸(十進制)的谷歌條形圖格式號碼

我試過的是this的方法,但它似乎沒有工作。

我的代碼:

data.addColumn('string', 'RuleName'); 
     data.addColumn('number', 'Lift'); 
     data.addColumn('number', 'Support'); 


     for (var i = 0; i < chartsdata.length; i++) { 
      data.addRow([rule, Lift,Support)]); 
     } 

     // format numbers in second column to 5 decimals 
     var formatter = new google.visualization.NumberFormat({ 
      pattern: '#,##0.00000' 
     }); // This does work, but only for the value in the tooltip. 

     formatter.format(data, 2); 

     // Passing in some options  
     var chart = new google.charts.Bar(document.getElementById('barChart')); 
     var options = { 

      title: "Association Rules by lift and support", 
      bars: 'horizontal', 
      series: { 
       0: { axis: 'Lift', targetAxisIndex: 0, }, 
       1: { axis: 'Support', targetAxisIndex: 1} 

      }, 

      axes: { 
       x: { 
        Lift: { label: 'Lift', format: '0,000' //Doesn't work, }, // Bottom x-axis. 
        Support: { side: 'top', label: 'Support' } // Top x-axis. 
       } 
      }, .......... 

我也試圖從谷歌文檔這種方法:

series:{hAxes:{1:{title:'abc', format: '0,0000'}} 

The top axes with the label support should have at least four decimal places

任何幫助將不勝感激!

回答

1

有不受材料圖表
查看支持的幾個選項 - >Tracking Issue for Material Chart Feature Parity

雖然format沒有列出,也有不支持的幾個選項 - >{hAxis,vAxis,hAxes.*,vAxes.*}

所以這可能是問題
注意:上述選項應該是獨立的,不包括在series選項中,
如問題中所示(我也試過...

您可以通過使用hAxis.format
同時改變x軸格式,但不認爲你就可以只改變一個

見下工作片斷...

google.charts.load('current', { 
 
    packages: ['bar'] 
 
}).then(function() { 
 
    var data = new google.visualization.DataTable(); 
 
    data.addColumn('string', 'RuleName'); 
 
    data.addColumn('number', 'Lift'); 
 
    data.addColumn('number', 'Support'); 
 
    for (var i = 0; i < 10; i++) { 
 
    data.addRow([i.toString(), i+2, i+3]); 
 
    } 
 

 
    var formatter = new google.visualization.NumberFormat({ 
 
    pattern: '#,##0.00000' 
 
    }); 
 
    formatter.format(data, 2); 
 

 
    var chart = new google.charts.Bar(document.getElementById('barChart')); 
 
    var options = { 
 
    chart: { 
 
     title: 'Association Rules by lift and support' 
 
    }, 
 
    bars: 'horizontal', 
 
    series: { 
 
     0: {axis: 'Lift'}, 
 
     1: {axis: 'Support'} 
 
    }, 
 
    axes: { 
 
     x: { 
 
     Lift: {label: 'Lift'}, 
 
     Support: {side: 'top', label: 'Support'} 
 
     } 
 
    }, 
 
    hAxis: { 
 
     format: '#,##0.00000' 
 
    }, 
 
    height: 320 
 
    }; 
 
    chart.draw(data, google.charts.Bar.convertOptions(options)); 
 
});
<script src="https://www.gstatic.com/charts/loader.js"></script> 
 
<div id="barChart"></div>

+1

希望這有助於與_Material_,請務必在繪製前使用'convertOptions' ... – WhiteHat

+0

這絕對有幫助!我確實嘗試過'hAxis:'格式:'#,## 0.00000' }'選項,但不知道'convertOptions'。這解決了我的問題,通過格式化這兩個軸。 – Chris