Google Chart: How to draw the vertical axis for LineChart?

后端 未结 2 1256
醉话见心
醉话见心 2020-12-03 16:46

I want to draw a Google\'s line chart in my web page! Here is my js code:

function drawVisualization() {
  // Create and populate the data table.
  var data          


        
2条回答
  •  借酒劲吻你
    2020-12-03 17:13

    There is a more elegant solution by using xticks: To do that we define a datatable like this:

    var data = new google.visualization.DataTable();
            data.addColumn('number', 'Month');
            data.addColumn('number', 'Sales');
            data.addRows([
            [{v: 0, f:'Jan'}, 1000],
            [{v: 1, f:'Feb'}, 1170],
            [{v: 2, f:'Mar'}, 660],
            [{v: 3, f:'Apr'}, 1030]
        ]);
    

    Where we set number values but also string labels for the Month axis.
    With just this and the format attribute removed, we would get vertical lines but numbers instead of the strings for the x axis labels, which is not what we want.
    To fix this we can set xticks to force the correct labels in the plot.

    var options = {
            title: '',
            hAxis: {
                title: 'Month',
                titleTextStyle: {
                    color: '#333'
                },
                baseline: 0,
                gridlines: {
                    color: '#f3f3f3',
                  count: 4
                },
               ticks: [{v: 0, f:'Jan'},{v: 1, f:'Feb'},{v: 2, f:'Mar'},{v: 3, f:'Apr'}], // <------- This does the trick
            },
            vAxis: {
                minValue: 0,
                gridlines: {
                    color: '#f3f3f3',
                    count: 5
                }
            }
        };
    

    Hereby a complete working fiddle to show you how it can be done: http://jsfiddle.net/j29Pt/417/

提交回复
热议问题