Bar with negative stack
author(s):
Torstein Hønsi
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/export-data.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<figure class="highcharts-figure">
<div id="container"></div>
<p class="highcharts-description">
Bar chart showing Andorran population distribution by using a mirrored
horizontal column chart with stacking and two x-axes.
</p>
</figure>
CSS
body {
font-family:
-apple-system,
BlinkMacSystemFont,
"Segoe UI",
Roboto,
Helvetica,
Arial,
"Apple Color Emoji",
"Segoe UI Emoji",
"Segoe UI Symbol",
sans-serif;
background: var(--highcharts-background-color);
color: var(--highcharts-neutral-color-100);
}
#container {
height: 400px;
}
.highcharts-figure,
.highcharts-data-table table {
min-width: 310px;
max-width: 800px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid var(--highcharts-neutral-color-10, #e6e6e6);
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: var(--highcharts-neutral-color-60, #666);
}
.highcharts-data-table th {
font-weight: 600;
padding: 0.5em;
}
.highcharts-data-table td,
.highcharts-data-table th,
.highcharts-data-table caption {
padding: 0.5em;
}
.highcharts-data-table thead tr,
.highcharts-data-table tbody tr:nth-child(even) {
background: var(--highcharts-neutral-color-3, #f7f7f7);
}
.highcharts-description {
background: light-dark(#f2f6f2, #323232);
border-radius: 0 4px 4px 0;
border-left: 2px solid light-dark(#1d6aff, #fff);
font-size: 0.8rem;
font-style: italic;
padding: 8px 16px;
margin: 2em 10px 1em;
}
JavaScript
// Custom template helper
Highcharts.Templating.helpers.abs = value => Math.abs(value);
// Age categories
const categories = [
'0-4', '5-9', '10-14', '15-19', '20-24', '25-29', '30-34', '35-40', '40-45',
'45-49', '50-54', '55-59', '60-64', '65-69', '70-74', '75-79', '80-84',
'80+'
];
Highcharts.chart('container', {
chart: {
type: 'bar'
},
title: {
text: 'Population pyramid for Andorra, 2023'
},
subtitle: {
text: 'Source: <a ' +
'href="https://countryeconomy.com/demography/population-structure/andorra"' +
'target="_blank">countryeconomy.com</a>'
},
accessibility: {
point: {
valueDescriptionFormat: '{index}. Age {xDescription}, {value}%.'
}
},
xAxis: [{
categories: categories,
reversed: false,
labels: {
step: 1
},
accessibility: {
description: 'Age (male)'
}
}, { // mirror axis on right side
opposite: true,
reversed: false,
categories: categories,
linkedTo: 0,
labels: {
step: 1
},
accessibility: {
description: 'Age (female)'
}
}],
yAxis: {
title: {
text: null
},
labels: {
format: '{abs value}%'
},
accessibility: {
description: 'Percentage population',
rangeDescription: 'Range: 0 to 5%'
}
},
plotOptions: {
series: {
stacking: 'normal',
borderRadius: '50%'
}
},
tooltip: {
format: '<b>{series.name}, age {point.category}</b><br/>' +
'Population: {(abs point.y):.2f}%'
},
series: [{
name: 'Male',
data: [
-1.38, -2.09, -2.45, -2.71, -2.97,
-3.69, -4.04, -3.81, -4.19, -4.61,
-4.56, -4.21, -3.53, -2.55, -1.82,
-1.46, -0.78, -0.71
...