Google Charts - Avoid showing negative values in y

2020-07-01 06:03发布

问题:

I have the following code:

function drawVisualization() {
  // Create and populate the data table.
  var data = google.visualization.arrayToDataTable([
    ['Year', 'People'],
    ['2010',0]
  ]);

  // Create and draw the visualization.
  new google.visualization.ColumnChart(document.getElementById('visualization')).
      draw(data,
           {title:"Yearly Coffee Consumption by Country",
            width:600, height:400,
            hAxis: {title: "Year"},
            backgroundColor: 'none'
           }
      );
}

Which gives me the following chart

How can I do to avoid showing negative values in the yAxis? I have tried adding vAxis: {minValue:0} without any luck.

There is a playground/sandbox for these charts: Google Charts Playground

回答1:

You need to set viewWindowMode as explicit

vAxis: {viewWindowMode: "explicit", viewWindow:{ min: 0 }}


回答2:

viewWindowMode: "explicit" is deprecated in current version which uses: vAxis.viewWindow.min:

vAxis: {
    viewWindow: {
        min: 0
    }
}