Tree map with levels

author(s): Jon Arild Nygård

by nancynancy

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>
</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

var data = [{
  id: 'A',
  name: 'Apples',
  color: "#00188F"
}, {
  id: 'B',
  name: 'Bananas',
  color: "#00AD7F"
}, {
  id: 'O',
  name: 'Oranges',
  color: '#FFAA00'
}, {
  name: 'Anne',
  parent: 'A',
  value: 5
}, {
  name: 'Rick',
  parent: 'A',
  value: 3
}, {
  name: 'Peter',
  parent: 'A',
  value: 4
}, {
  name: 'Anne',
  parent: 'B',
  value: 4
}, {
  name: 'Rick',
  parent: 'B',
  value: 10
}, {
  name: 'Peter',
  parent: 'B',
  value: 1
}, {
  name: 'Anne',
  parent: 'O',
  value: 1
}, {
  name: 'Rick',
  parent: 'O',
  value: 3
}, {
  name: 'Peter',
  parent: 'O',
  value: 3
}, {
  name: 'Susanne',
  parent: 'Kiwi',
  value: 2,
  color: '#661A1A'
}]
Highcharts.chart('container', {
  series: [{
    type: "treemap",
    layoutAlgorithm: 'stripes',
    alternateStartingDirection: true,
    levels: [{
      level: 1,
      layoutAlgorithm: 'sliceAndDice',
      dataLabels: {
        enabled: true,
        align: 'left',
        verticalAlign: 'top',
        style: {
          fontSize: '15px',
          fontWeight: 'bold'
        }
      }
    }],
    data: data
  }],
  title: {
    text: 'Fruit consumption'
  },
  tooltip: {
  	backgroundColor: "#FFFFFF",
    style: {
    	color: "red"
    },
  },
});