google.charts.load('current', {packages:['bar']});
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = google.visualization.arrayToDataTable([
["BusinessUnit", "Year", "AvgEmpCost"],
["BU-1", 2014, 119329],
["BU-1", 2015, 125542],
["BU-1", 2016, 120894],
["BU-1", 2017, 130776],
["BU-1", 2018, 141465],
["BU-1", 2019, 153028],
["BU-2", 2014, 130722],
["BU-2", 2015, 131218],
["BU-2", 2016, 131451],
["BU-2", 2017, 134897],
["BU-2", 2018, 138432],
["BU-2", 2019, 142061],
]);
var options = {
chart: {
title: 'buspentamt'
},
height: 400
};
// init column arrays
var aggColumns = [];
var viewColumns = [0];
// build view & agg columns for each year
data.getDistinctValues(1).forEach(function (year, index) {
// add view column for year
viewColumns.push({
calc: function (dt, row) {
if (dt.getValue(row, 1) === year) {
return dt.getValue(row, 2);
}
return null;
},
label: year,
type: 'number'
});
// add agg column
aggColumns.push({
aggregation: google.visualization.data.sum,
column: index + 1,
label: year,
type: 'number'
});
});
// set view columns
var view = new google.visualization.DataView(data);
view.setColumns(viewColumns);
// agg view by business unit
var aggData = google.visualization.data.group(
view,
[0],
aggColumns
);
var chart = new google.charts.Bar(document.getElementById('columnchart_material'));
// use aggData to draw chart
chart.draw(aggData, google.charts.Bar.convertOptions(options));
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="columnchart_material"></div>