如何在块中间移动vAxis值

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在块中间移动vAxis值相关的知识,希望对你有一定的参考价值。

我在这里有我的googleCombot图表,我希望我的vAxis(垂直轴[0,1,2,3,4,5,6])值位于块的中间,而不是坐在线上。我怎样才能实现它并让我的价值观在范围内?

这是想要实现google Combo graph的结果

这是我的小提琴

function drawChart () {
    var data = new google.visualization.DataTable();
    data.addColumn('number', 'x');
    data.addColumn('number', 'y');
    data.addColumn('number', 'color band 1');
    data.addColumn('number', 'color band 2');
    data.addColumn('number', 'color band 3');
    data.addColumn('number', 'color band 4');
    data.addColumn('number', 'color band 5');
    
    var vticks = [];
    var weight = [1,3,2,7,4,6];
    var hticks = [];
    var i;
    for(var i = 0; i< weight.length; i++){ 
      hticks.push(i+1);
    }
    var vticksMin = vticks[0];
    var vticksMax = vticks[vticks.length - 1];
    var br = 1, by = 1, ty = 1, tr = 1, mg = 3;
    for (i = 0; i < 7; i++){
      vticks.push(i);
    }
	
    for (var i = 1; i < weight.length+1; i++) {
    
        data.addRow([i, parseInt(weight[i-1]), br, by,mg,ty,tr]);
    }

	var view = new google.visualization.DataView(data);
	view.setColumns([0,1, {
		type: 'string',
		role: 'annotation',
		calc: function (dt, row) {
			return dt.getValue(row, 1).toString();
		}
	}, 2, 3,4, 5,6]);
    
    var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));
    
    chart.draw(view, {
        height: 700,
        width: 900,
        isStacked: true,
        hAxis: {
	            ticks: hticks
        },
        vAxis: {
	            viewWindow: {
	                min: vticksMin,
	                max: vticksMax
	            },
	            ticks: vticks
        },
        series: {
            0: {
                type: 'line',
    			color: '#000'
            },
            1: {
    			areaOpacity: 0.75,
    			color: '#E91818',
                lineWidth: 0,
                type: 'area',
                visibleInLegend: false,
                enableInteractivity: false
            },
            2: {
    			areaOpacity: 0.7,
    			color: '#FFD91E',
                lineWidth: 0,
                type: 'area',
                visibleInLegend: false,
                enableInteractivity: false
            },
            3: {
    			areaOpacity: 0.7,
    			color: '#00D717',
                lineWidth: 0,
                type: 'area',
                visibleInLegend: false,
                enableInteractivity: false
            },
            4: {
    			areaOpacity: 0.7,
    			color: '#FFD91E',
                lineWidth: 0,
                type: 'area',
                visibleInLegend: false,
                enableInteractivity: false
            },
            5: {
    			areaOpacity: 0.75,
    			color: '#E91818',
                lineWidth: 0,
                type: 'area',
                visibleInLegend: false,
                enableInteractivity: false
            }
        }
    });
}


google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>
<div id="creativeCommons" style="text-align: center; width: 400px;">
    <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/InteractiveResource" property="dct:title" rel="dct:type">Code to add colored horizontal bands as a chart background</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Jonathan Ngbonga</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
</div>
答案

将主要网格线的颜色设置为透明...

gridlines: {
    color: 'transparent',
},

并在每个主要之间添加1个小网格线...

minorGridlines: {
    color: 'black',
    count: 1
},

请参阅以下工作代码段...

function drawChart () {
  var data = new google.visualization.DataTable();
  data.addColumn('number', 'x');
  data.addColumn('number', 'y');
  data.addColumn('number', 'color band 1');
  data.addColumn('number', 'color band 2');
  data.addColumn('number', 'color band 3');
  data.addColumn('number', 'color band 4');
  data.addColumn('number', 'color band 5');

  var vticks = [];
  var weight = [1,3,2,7,4,6];
  var hticks = [];
  var i;
  for(var i = 0; i< weight.length; i++){
    hticks.push(i+1);
  }
  var br = 1, by = 1, ty = 1, tr = 1, mg = 3;
  for (i = 0; i < 7; i++){
    vticks.push(i);
  }
  var vticksMin = vticks[0];
  var vticksMax = vticks[vticks.length - 1];

  for (var i = 1; i < weight.length+1; i++) {
    data.addRow([i, parseInt(weight[i-1]), br, by,mg,ty,tr]);
  }

  var view = new google.visualization.DataView(data);
  view.setColumns([0,1, {
    type: 'string',
    role: 'annotation',
    calc: function (dt, row) {
      return dt.getValue(row, 1).toString();
    }
  }, 2, 3,4, 5,6]);

  var chart = new google.visualization.ComboChart(document.getElementById('chart_div'));

  chart.draw(view, {
    height: 700,
    width: 900,
    isStacked: true,
    hAxis: {
      ticks: hticks
    },
    vAxis: {
      minorGridlines: {
        color: 'black',
        count: 1
      },
      gridlines: {
        color: 'transparent',
      },
      viewWindow: {
        min: vticksMin,
        max: vticksMax
      },
      ticks: vticks
    },
    series: {
      0: {
        type: 'line',
        color: '#000'
      },
      1: {
        areaOpacity: 0.75,
        color: '#E91818',
        lineWidth: 0,
        type: 'area',
        visibleInLegend: false,
        enableInteractivity: false
      },
      2: {
        areaOpacity: 0.7,
        color: '#FFD91E',
        lineWidth: 0,
        type: 'area',
        visibleInLegend: false,
        enableInteractivity: false
      },
      3: {
        areaOpacity: 0.7,
        color: '#00D717',
        lineWidth: 0,
        type: 'area',
        visibleInLegend: false,
        enableInteractivity: false
      },
      4: {
        areaOpacity: 0.7,
        color: '#FFD91E',
        lineWidth: 0,
        type: 'area',
        visibleInLegend: false,
        enableInteractivity: false
      },
      5: {
        areaOpacity: 0.75,
        color: '#E91818',
        lineWidth: 0,
        type: 'area',
        visibleInLegend: false,
        enableInteractivity: false
      }
    }
  });
}
google.load('visualization', '1', {packages: ['corechart'], callback: drawChart});
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<div id="chart_div"></div>
<div id="creativeCommons" style="text-align: center; width: 400px;">
    <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US"><img alt="Creative Commons License" style="border-width:0" src="http://i.creativecommons.org/l/by-nc-sa/3.0/88x31.png" /></a><br /><span xmlns:dct="http://purl.org/dc/terms/" href="http://purl.org/dc/dcmitype/InteractiveResource" property="dct:title" rel="dct:type">Code to add colored horizontal bands as a chart background</span> by <span xmlns:cc="http://creativecommons.org/ns#" property="cc:attributionName">Jonathan Ngbonga</span> is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by-nc-sa/3.0/deed.en_US">Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License</a>.
</div>

以上是关于如何在块中间移动vAxis值的主要内容,如果未能解决你的问题,请参考以下文章

谷歌图表vAxis勾选多种颜色和相应的浅色背景

如何将 View 类中的代码片段移动到 OnAppearing() 方法?

如何更新所有片段? onProgressUpdate 不更新所有片段

如何在块内制作边框底部? [关闭]

在同一页面上的多个图表上将vAxis设置为相同比例

Google Chart打印vAxis格式选择器以及编号