更改Google图表中的Y轴?

时间:2016-06-16 05:53:43

标签: javascript charts bar-chart

这是我试图在Google图表的帮助下显示图表的代码。

exit

这是我试图显示条形图的代码。我只想在Y轴上显示美元($)符号。请帮助。

1 个答案:

答案 0 :(得分:1)

<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
   <div id="chart_div"></div>
<br/>


<script>
    google.charts.load('current', {'packages':['bar']});

  google.charts.setOnLoadCallback(drawChart);
  function drawChart() {
    var data = google.visualization.arrayToDataTable([
      ['Year', 'Sales', 'Expenses', 'Profit'],
      ['2014', 1000, 400, 200],
      ['2015', 1170, 460, 250],
      ['2016', 660, 1120, 300],
      ['2017', 1030, 540, 350]
    ]);

    var options = {
      chart: {
        title: 'Company Performance',
        subtitle: 'Sales, Expenses, and Profit: 2014-2017',
      },
      bars: 'Vertical', // Required for Material Bar Charts.
      vAxis: {format: 'currency'},
      height: 400,
      colors: ['#1b9e77', '#d95f02', '#7570b3']
    };

    var chart = new google.charts.Bar(document.getElementById('chart_div'));

    chart.draw(data, google.charts.Bar.convertOptions(options));

    var btns = document.getElementById('btn-group');

    btns.onclick = function (e) {

      if (e.target.tagName === 'BUTTON') {
        options.hAxis.format = e.target.id === 'none' ? '' : e.target.id;
        chart.draw(data, google.charts.Bar.convertOptions(options));
      }
    }
  }
  </script>

https://jsfiddle.net/wdf6wvpt/