I am creating a stacked bar chart comparing quarterly results of two different years. One year is plotted as a positive value bar chart and the second year is plotted using the result value * -1 so it plots below the same quarter.
In this example, 2023 Qtr 1 is 469 and 2022 Qtr 1 is really 676. The bottom year values are multiplied by -1 during a query.
How do I make the negative value display below the bottom bar and keep the positive value on top as it is now?
JavaScript:
<script type="text/javascript" src="https://www.gstatic.com/charts/loader.js"></script>
<script type="text/javascript">
google.charts.load('current', { packages: ['corechart'] });
google.charts.setOnLoadCallback(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
var jsonList = @(Json.Serialize(Model.listAllAircraftChartQuarterly))
var title = 'Quarterly Aircraft Transaction Trend';
data.addColumn('string', 'Quarter');
data.addColumn('number', '2023');
data.addColumn('number', '2022');
data.addColumn('number', '% Change');
data.addRows(jsonList.map((p) => {
return [p.yearQuarter, p.year2top, p.year3bottom, p.year2percent];
}));
var formatPercent = new google.visualization.NumberFormat({ pattern: '#.#%' });
formatPercent.format(data, 3);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2,
{
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
},
3,
{
calc: "stringify",
sourceColumn: 3,
type: "string",
role: "annotation"
}
]);
var options = {
title: title,
titlePosition: 'out',
isStacked: true,
seriesType: "bars",
vAxes: {
0: {
textPosition: 'out',
viewWindowMode: 'pretty',
title: "",
viewWindow: { min: -1100, max: 1100 },
gridlines: { color: 'lightgray' },
},
1: {
textPosition: 'out',
viewWindow: { min: -1, max: 1 },
format: 'percent',
gridlines: { color: 'transparent' }
},
},
series: {
0: { targetAxisIndex: 0, color: 'blue' },
1: { targetAxisIndex: 0, color: 'gray' },
2: { targetAxisIndex: 1, color: 'red', type: 'line', lineWidth: 1, lineDashStyle: [4, 4], pointSize: 5 },
},
width: '100%',
height: '300',
legend: { position: 'top' },
chartArea: {
height: '100%',
width: '100%',
top: 48,
left: 60,
right: 60,
bottom: 75
},
annotations: {
highContrast: false,
textStyle: {
color: 'red',
fontSize: 12,
bold: true
},
alwaysOutside: true
},
}
var chart = new google.visualization.ComboChart(document.getElementById('primaryYear2and3'));
chart.draw(view, options);
document.getElementById('downloadimg2and3').innerHTML = '<a download="google-chart-image" href="' + chart.getImageURI() +
'"><button type="button" class="btn btn-outline-dark btn-sm opacity-25 ms-4 mb-3">Download Chart Image</button></a>';
window.addEventListener('resize', drawChart, false);
}
</script>
I have tried researching other posts.
the only option to move the annotations for the second series below the bars,
is to provide a negative stem length using the annotations
configuration option,
within the series
option for the second series...
1: { targetAxisIndex: 0, color: 'gray', annotations: {stem: {length: -80}} }, // <-- negative stem length
see following working snippet...
google.charts.load('current', {
packages: ['corechart']
}).then(drawChart);
function drawChart() {
var data = new google.visualization.DataTable();
//var jsonList = @(Json.Serialize(Model.listAllAircraftChart))
var jsonList = [
{yearQuarter: 'Quarter 1', year2top: 469, year3bottom: -676, year2percent: -0.306},
{yearQuarter: 'Quarter 2', year2top: 580, year3bottom: -739, year2percent: -0.215},
{yearQuarter: 'Quarter 3', year2top: 566, year3bottom: -623, year2percent: -0.091},
{yearQuarter: 'Quarter 4', year2top: 817, year3bottom: -800, year2percent: -0.01}
];
var title = 'Quarterly Aircraft Transaction Trend';
data.addColumn('string', 'Quarter');
data.addColumn('number', '2023');
data.addColumn('number', '2022');
data.addColumn('number', '% Change');
data.addRows(jsonList.map((p) => {
return [p.yearQuarter, p.year2top, p.year3bottom, p.year2percent];
}));
var formatPercent = new google.visualization.NumberFormat({ pattern: '#.#%' });
formatPercent.format(data, 3);
var view = new google.visualization.DataView(data);
view.setColumns([0, 1,
{
calc: "stringify",
sourceColumn: 1,
type: "string",
role: "annotation"
},
2,
{
calc: "stringify",
sourceColumn: 2,
type: "string",
role: "annotation"
},
3,
{
calc: "stringify",
sourceColumn: 3,
type: "string",
role: "annotation"
}
]);
var options = {
title: title,
titlePosition: 'out',
isStacked: true,
seriesType: "bars",
vAxes: {
0: {
textPosition: 'out',
viewWindowMode: 'pretty',
title: "",
viewWindow: { min: -1100, max: 1100 },
gridlines: { color: 'lightgray' },
},
1: {
textPosition: 'out',
viewWindow: { min: -1, max: 1 },
format: 'percent',
gridlines: { color: 'transparent' }
},
},
series: {
0: { targetAxisIndex: 0, color: 'blue' },
1: { targetAxisIndex: 0, color: 'gray', annotations: {stem: {length: -80}} },
2: { targetAxisIndex: 1, color: 'red', type: 'line', lineWidth: 1, lineDashStyle: [4, 4], pointSize: 5 },
},
width: '100%',
height: '300',
legend: { position: 'top' },
chartArea: {
height: '100%',
width: '100%',
top: 48,
left: 60,
right: 60,
bottom: 75
},
annotations: {
highContrast: false,
textStyle: {
color: 'red',
fontSize: 12,
bold: true
},
alwaysOutside: true
},
}
var chart = new google.visualization.ComboChart(document.getElementById('primaryYear2and3'));
chart.draw(view, options);
google.visualization.events.addListener(chart, 'ready', function () {
document.getElementById('downloadimg2and3').innerHTML = '<a download="google-chart-image" href="' + chart.getImageURI() +
'"><button type="button" class="btn btn-outline-dark btn-sm opacity-25 ms-4 mb-3">Download Chart Image</button></a>';
});
window.addEventListener('resize', drawChart, false);
}
<script src="https://www.gstatic.com/charts/loader.js"></script>
<div id="primaryYear2and3"></div>
<div id="downloadimg2and3"></div>