2016年5月23日 星期一

雙Y軸 google chart(double Y-axes google chart)

之前在使用Google Charts繪圖的時候發現一個問題,
在他的範例裡面沒有雙Y軸(或是我沒找到),
也就是Y軸有左邊軸與右邊軸,兩軸的數字不同

後來也忘了是在哪裡找到的
以下是範例(JavaScript)

<!-- Load Google charts api for JavaScript -->
<script type="text/javascript" src="https://www.google.com/jsapi?autoload={'modules':[{'name':'visualization','version':'1.1','packages':['corechart']}]}"></script>

<script type="text/javascript">
    google.setOnLoadCallback(drawVisualization);

    function drawVisualization() {
        // Some raw data (not necessarily accurate)
        var data = google.visualization.arrayToDataTable([
         ['yyy', 'y1', 'y2'],
         ['2003', 1413000, 111],
         ['2006', 555000, 118],
         ['2007', 9400000, 92],
         ['2008', 3995116, 141],
         ['2009', 4027797, 147],
         ['2010', 4266232, 125],
         ['2011', 4602719, 133],
         ['2015', 3278581, 119],
         ['2016', 2043220, 99]
        ]);

        var options = {
            title: 'title',
            vAxes: { 0: { format: '#,###' }, 1: { format: '#,###' } },
            hAxis: { title: 'xtitle' },
            series: {
                0: { type: "bars", targetAxisIndex: 0, color: "#FFCCFF" },
                1: { type: "line", targetAxisIndex: 1, color: "purple" }
            }
        };

        var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
        chart.draw(data, options);
    }
</script>
<!-- Google charts放置的位置-->
<div id="chart_div" style="width: 600px; height: 350px;"></div>

最後應該會看到此圖

沒有留言:

張貼留言