Tree map with levels
by Vignesh Gopalakrishnan
January 25, 2024
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/treemap.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<figure class="highcharts-figure">
<div id="container"></div>
<p class="highcharts-description">
This chart shows a tree map with a hierarchy, where the
groups are labelled with a different text style from the
child nodes, and the nodes are grouped together by color.
</p>
</figure>
CSS
.highcharts-figure,
.highcharts-data-table table {
min-width: 320px;
max-width: 600px;
margin: 1em auto;
}
.highcharts-data-table table {
font-family: Verdana, sans-serif;
border-collapse: collapse;
border: 1px solid #ebebeb;
margin: 10px auto;
text-align: center;
width: 100%;
max-width: 500px;
}
.highcharts-data-table caption {
padding: 1em 0;
font-size: 1.2em;
color: #555;
}
.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 tr:nth-child(even) {
background: #f8f8f8;
}
.highcharts-data-table tr:hover {
background: #f1f7ff;
}
JavaScript
Highcharts.chart('container', {
series: [{
type: 'treemap',
layoutAlgorithm: 'stripes',
alternateStartingDirection: true,
borderColor: '#fff',
borderRadius: 6,
borderWidth: 2,
dataLabels: {
style: {
textOutline: 'none'
}
},
levels: [{
level: 1,
layoutAlgorithm: 'sliceAndDice',
dataLabels: {
enabled: true,
align: 'left',
verticalAlign: 'top',
style: {
fontSize: '15px',
fontWeight: 'bold'
}
}
}],
data: [{
id: 'A',
name: 'Revenue',
color: '#50FFB1'
}, {
id: 'B',
name: 'Cost',
color: '#F5FBEF'
}, {
name: 'Revenue from solar electricity sales',
parent: 'A',
value: 20.37
}, {
name: 'Revenue from export',
parent: 'A',
value: 1.40
}, {
name: 'Maintenance costs',
parent: 'B',
value: 2.23
}, {
name: 'Export metering',
parent: 'B',
value: 0.96
}]
}],
title: {
text: 'Norwegian regions and counties by area',
align: 'left'
},
subtitle: {
text:
'Source: <a href="https://snl.no/Norge" target="_blank">SNL</a>',
align: 'left'
},
tooltip: {
useHTML: true,
pointFormat:
'The area of <b>{point.name}</b> is <b>{point.value} km<sup>2</sup></b>'
}
});