Google图表格式问题

时间:2014-08-05 11:35:17

标签: javascript google-visualization

我有一个折线图,显示Y轴为20,000,000,但我想将格式更改为20K,如何更改谷歌图表中的foramt。请帮忙。 代码:

<html>
  <head>
    <script type="text/javascript" src="https://www.google.com/jsapi"></script>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      function drawChart() {
          var data1=[["Year","sss","SSE"],["JAN",1234,7834],["FEB",0,0],["MAR",0,0],["APR",0,0],["MAY",0,0],["JUN",0,0],["JUL",0,0],["AUG",0,0],["SEP",0,0],["OCT",0,0],["NOV",0,0],["DEC",0,0]];

        var data = google.visualization.arrayToDataTable(data1);

        var options = {
                  legend: {position: 'top',alignment:'center'},
                  vAxis: {viewWindow: {min:0},
               // ticks: [{v: 2000000000, f: '2k'}, {v: 2000000000, f: '4k'}, {v: 6000000000, f: '6k'}, {v: 8000000000, f: '8k'}, {v: 10000000000, f: '10k'},{v:12000000000,f: '12k'}],

                  title:'ROW & Returned($000)',
               // format:'#K',
             format:'$###,###',
                gridlines:  {color:'#e4e4e4', count: 7}},
                lineWidth: 4,
                colors: ['#69699f','#d53200','#69699f']
        };

        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        chart.draw(data, options);
      }
    </script>
  </head>
  <body>
    <div id="chart_div" style="width: 900px; height: 500px;"></div>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

轴格式化选项不支持标量格式,因此您必须提供轴的位置和标签列表,以通过vAxis.ticks选项放置刻度线。您的代码包含一个注释掉的行,如果您取消注释,它应该处理这个:

ticks: [{v: 2000000000, f: '2k'}, {v: 4000000000, f: '4k'}, {v: 6000000000, f: '6k'}, {v: 8000000000, f: '8k'}, {v: 10000000000, f: '10k'},{v:12000000000,f: '12k'}]

答案 1 :(得分:-1)

如果您知道最大值,则可以动态创建刻度:

var max = _getMaxValue;
options.ticks = _readableTicks(max);

function _readableTicks(max) {
  var ret = [(v:0)];    //first tick
  if (max > 0) {
    var i = Math.floor(Math.log(max) / Math.log(1000));
    max = Math.round(max / Math.pow(10000, i)) * Math.pow(10000, i);
    for (var tick = 1; tick < 5; tick++ ){
      var v = max * tick/4;
      var f = (v / Math.pow(1000, i)).toFixed() * 1 + ' ' + [' ', 'k', 'M', 'G', 'T'][i];
      ret.push({v: v, f: f});
    }
  }
  return ret;
}