如何按月份对Google Graph上的堆积柱形图进行分组

时间:2019-02-28 14:13:07

标签: graph charts google-visualization grouping column-chart

我看到了许多柱形图和条形图,它们已经分组,并且我第一次开始使用Google Graph。

我创建了一个显示2年数据的图表,我需要将2018年1月/ 2019年1月彼此并列,然后进行间隔,同样,将2月18日/ 2月19日作为柱状图粘贴在一起-这可能吗?

如果您想看看我的逻辑是否正确,我有created a JSFiddle for this

我正在使用以下格式的数据...

    var monthdata = google.visualization.arrayToDataTable([
  ['Month', 'Money Received', 'Money Outstanding', 'Still to be invoiced', 'Ahead of Budget Achieved'],
  ['January 2019', 9145.600, 1000.400, 0, 900.4],
  ['January 2018', 8123.100, 0, 0, 0],
  ['February 2019', 7030.700, 200.300, 999.75, 0],
  ['February 2018', 7311.190, 0, 0, 0]])

等...(JSFilddle部分中的完整代码)

非常感谢。

1 个答案:

答案 0 :(得分:0)

没有任何可用的组选项。

为了在月份对之间创建间隔,
您可以在数据表中添加空白行,
在每对之间,例如

['January 2019', 9145.600, 1000.400, 0, 900.4],
['January 2018', 8123.100, 0, 0, 0],
[null, null, null, null, null],
['February 2019', 7030.700, 200.300, 999.75, 0],
['February 2018', 7311.190, 0, 0, 0],
[null, null, null, null, null],
['March 2019', 8784.600, 1000.000, 0, 0],
['March 2018', 9945.200, 0, 0, 0],

使月份对更加接近,
您可以使用bar.groupWidth选项...

bar: {
  groupWidth: '90%'
},

请参阅以下工作片段...

google.charts.load('current', {
  packages: ['corechart']
}).then(drawChart);

function drawChart() {
  var monthdata = google.visualization.arrayToDataTable([
    ['Month', 'Money Received', 'Money Outstanding', 'Still to be invoiced', 'Ahead of Budget Achieved'],
    ['January 2019', 9145.600, 1000.400, 0, 900.4],
    ['January 2018', 8123.100, 0, 0, 0],
    [null, null, null, null, null],
    ['February 2019', 7030.700, 200.300, 999.75, 0],
    ['February 2018', 7311.190, 0, 0, 0],
    [null, null, null, null, null],
    ['March 2019', 8784.600, 1000.000, 0, 0],
    ['March 2018', 9945.200, 0, 0, 0],
    [null, null, null, null, null],
    ['April 2019', 11398.300, 1019.8300, 0, 1139.83],
    ['April 2018', 9878.500, 0, 0, 0],
    [null, null, null, null, null],
    ['May 2019', 8486.800, 524.670, 1586.44, 0],
    ['May 2018', 12050.300, 0, 0, 0],
    [null, null, null, null, null],
    ['June 2019', 10186.0100, 1000.4000, 423.3, 0],
    ['June 2018', 10321.120, 0, 0, 0],
    [null, null, null, null, null],
    ['July 2019', 11287.2200, 1823.8800, 0, 1212.99],
    ['July 2018', 11555.000, 0, 0, 0],
    [null, null, null, null, null],
    ['August 2019', 8491.1600, 892.1200, 2197.88, 0],
    ['August 2018', 11288.120, 0, 0, 0],
    [null, null, null, null, null],
    ['September 2019', 6496.3300, 4221.3200, 0, 132.44],
    ['September 2018', 13892.500, 0, 0, 0],
    [null, null, null, null, null],
    ['October 2019', 4905.5300, 2121.1200, 1878.88, 0],
    ['October 2018', 8723.100, 0, 0, 0],
    [null, null, null, null, null],
    ['November 2019', 3296.7000, 1834.1200, 3165.88, 0],
    ['November 2018', 7011.700, 0, 0, 0],
    [null, null, null, null, null],
    ['December 2019', 5110.1000, 1200.3600, 0, 1310.46],
    ['December 2018', 9382.500, 0, 0, 0]
  ]);

  var options = {
    bar: {
      groupWidth: '90%'
    },
    title: 'Invoicing and Revenue',
    isStacked: 'true',
    legend: {
      position: 'top',
      alignment: 'start'
    },
    tooltip: {
      isHtml: true
    },
    hAxis: {
      titleTextStyle: {
        color: '#78909c'
      },
      hAxis: {
        title: 'Date'
      },
      slantedText: true,
      slantedTextAngle: 70,
      gridlines: {
        count: 24
      },
    },
    vAxis: {
      minValue: 0,
      gridlines: {
        color: '#cfd8dc',
        count: 4
      },
      vAxis: {
        title: '€'
      },
      title: '€',
      titleTextStyle: {
        color: '#78909c'
      },
      baselineColor: 'transparent',
      textStyle: {
        color: '#1a71a8',
        fontName: 'Roboto',
        fontSize: '9',
        bold: true
      }
    },
    colors: ["#1a71a8", "#7b848e", "#c65217", "#73721c"],
    backgroundColor: '#ffffff',
    chartArea: {
      backgroundColor: "#ffffff",
      width: '88%',
      height: '68%'
    },
    animation: {
      duration: 1200,
      easing: 'linear',
      startup: true
    }
  };

  var chart = new google.visualization.ColumnChart(document.getElementById('containerx'));
  chart.draw(monthdata, options);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="containerx"></div>