Google图表:在柱状图中自定义h轴基线

时间:2019-06-18 09:18:16

标签: angular charts google-visualization

就我而言,我想在h轴或y轴基线上显示一些指示标记。请检查图片以了解更多信息。

在图像中,我们显示的刻度值为[0,10,20,30,40]。因此,对于每个值,我们都需要在h轴基线上显示一条小线。

我可以在h轴上显示基线,但是我无法在h轴基线上显示细线

请帮助我解决此问题。<code>enter image description here</code>

  barTitle = 'Occupancy';
barType = 'ColumnChart';
barData = [
  [{v: 0, f:''}, 0,0,0,0],
   [{v:1,f:'SUN'}, 5,8,10,12],
   [{v:2,f:"MON"}, 6,4,14,10],
   [{v:3,f:"TUE"}, 13,10,3,9],
   [{v:4,f:"WED"}, 10,16,8,6],
   [{v:5,f:"THU"}, 16,9,8,6],
   [{v:6,f:"FRI"}, 5,5,10,14],
   [{v:7,f:"SAT"}, 9,12,4,11]
];
barColumnNames = ['DAY', 'Regular','Compact','Electric','ADA'];
barOptions = { 
  hAxis:{
    title:'DAY',
   titlePosition:'in',

    ticks:[
    {v: 0, f:''},
    {v:1,f:'SUN'}, 
   {v:2,f:"MON"},
   {v:3,f:"TUE"},
   {v:4,f:"WED"},
   {v:5,f:"THU"},
   {v:6,f:"FRI"},
   {v:7,f:"SAT"}
  ]
  },
 vAxis: {

gridlines: {
  color: 'none'
},

ticks: [0, .25, .50, .75, 1],
baselineColor:'#00ff00',

  },
  isStacked:"percent",
  width:550,
  height:300,
  bar:{groupWidth:"25%"},
  legend:{position:"none"},
  series:{

    0:{color:'rgb(185,2,118)'},
    1:{color:'rgb(82,95,107)'},
    2:{color:'rgb(0,142,207)'}
  },


};

1 个答案:

答案 0 :(得分:0)

没有开箱即用的选项可以在轴上显示一条小指示线。

但是您可以在图表的'ready'事件上绘制自己的图片,
参见以下工作片段...

在这里,我们使用chart methods getChartLayoutInterfacegetBoundingBox查找行的位置...

google.charts.load('current', {
  packages: ['corechart']
}).then(function () {
  barTitle = 'Occupancy';
  barType = 'ColumnChart';
  barData = [
    [{v: 0, f:''}, 0,0,0,0],
    [{v:1,f:'SUN'}, 5,8,10,12],
    [{v:2,f:"MON"}, 6,4,14,10],
    [{v:3,f:"TUE"}, 13,10,3,9],
    [{v:4,f:"WED"}, 10,16,8,6],
    [{v:5,f:"THU"}, 16,9,8,6],
    [{v:6,f:"FRI"}, 5,5,10,14],
    [{v:7,f:"SAT"}, 9,12,4,11]
  ];
  barColumnNames = ['DAY', 'Regular','Compact','Electric','ADA'];
  barOptions = {
    hAxis:{
      title:'DAY',
      titlePosition:'in',
      ticks:[
        {v: 0, f:''},
        {v:1,f:'SUN'},
        {v:2,f:"MON"},
        {v:3,f:"TUE"},
        {v:4,f:"WED"},
        {v:5,f:"THU"},
        {v:6,f:"FRI"},
        {v:7,f:"SAT"}
      ]
    },
    vAxis: {
      gridlines: {
        color: 'none'
      },
      ticks: [0, .25, .50, .75, 1],
      baselineColor:'#00ff00',
    },
    isStacked:"percent",
    width:550,
    height:300,
    bar:{groupWidth:"25%"},
    legend:{position:"none"},
    series:{
      0:{color:'rgb(185,2,118)'},
      1:{color:'rgb(82,95,107)'},
      2:{color:'rgb(0,142,207)'}
    },
  };

  barData.splice(0, 0, barColumnNames);
  var dataTable = google.visualization.arrayToDataTable(barData);
  var container = document.getElementById('chart_div');
  var chart = new google.visualization[barType](container);
  google.visualization.events.addListener(chart, 'ready', function () {
    // init variables
    var svg = container.getElementsByTagName('svg')[0];
    var svgNS = svg.namespaceURI;
    var chartLayout = chart.getChartLayoutInterface();
    var baseBounds = chartLayout.getBoundingBox('hAxis#0#baseline');

    // add tick lines
    var tickWidth = 10;
    barOptions.vAxis.ticks.forEach(function (tick, index) {
      // ignore baseline
      if (index === 0) {
        return;
      }

      // add axis tick
      var tickBounds = chartLayout.getBoundingBox('vAxis#0#label#' + index);
      var tickLine = svg.appendChild(document.createElementNS(svgNS, 'rect'));
      tickLine.setAttribute('x', (baseBounds.left - tickWidth));
      tickLine.setAttribute('y', tickBounds.top + (tickBounds.height / 2));
      tickLine.setAttribute('width', tickWidth);
      tickLine.setAttribute('height', 1);
      tickLine.setAttribute('stroke', 'none');
      tickLine.setAttribute('stroke-width', 0);
      tickLine.setAttribute('fill', '#333333');
    });
  });
  chart.draw(dataTable, barOptions);
});
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="chart_div"></div>