【问题标题】:Google Chart Format issue谷歌图表格式问题
【发布时间】:2017-02-03 00:30:58
【问题描述】:

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

<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>

【问题讨论】:

    标签: javascript google-visualization


    【解决方案1】:

    坐标轴格式选项不支持标量格式,因此您必须提供坐标轴的位置和标签列表,以便通过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'}]
    

    【讨论】:

    • 嗨,但是听说值是动态的,所以对于动态值我该如何设置它????
    • 您可以动态生成该数组;您想使用什么标准来确定最小和最大刻度线、刻度线的数量和/或刻度线之间的间距?
    【解决方案2】:

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

    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;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-07
      相关资源
      最近更新 更多