Google图表 - 避免在yAxis中显示负值

时间:2012-06-07 18:06:03

标签: javascript google-visualization

我有以下代码:

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

这给了我以下图表chart

如何避免在yAxis中显示负值?我试过添加 vAxis: {minValue:0}没有任何运气。

这些图表有一个游乐场/沙箱:Google Charts Playground

3 个答案:

答案 0 :(得分:53)

您需要将viewWindowMode设置为显式

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

答案 1 :(得分:4)

viewWindowMode: "explicit"在当前版本中已弃用,该版本使用:vAxis.viewWindow.min

vAxis: {
    viewWindow: {
        min: 0
    }
}

答案 2 :(得分:0)

这对我不起作用。我需要添加最大值:max:1

负轴仅在所有值都为 0 时出现,因此,为了不限制在最大值中,如果没有正值(使用 PHP),我只放置 vAxis。

我的代码示例是:

                $hayPositivos = false;
            foreach($valores as $valor){
                $html[]=",
                ['".$valor['dia']."', ".intval($valor['validadas']).",      ".intval($valor['totales'])."]";
                if(!$hayPositivos && (intval($valor['validadas']) >0 || intval($valor['totales']) >0)){
                    $hayPositivos = true;
                }
            }

                    $html[]="]);

                    var options = {
                      title: '".t('News by time:').$periodo_nombre."',
                      legend: { position: 'bottom' }";
                    if(!$hayPositivos){
                        $html[] = ",
                        vAxis: {
                          viewWindow: {min: 0, max:1}
                        }";
                    }
                      
                    $html[] = "};

这是怎么看的:

If all values are negative

If there is any positive value