Sankey diagram

author(s): Torstein Hønsi

by djakacki

HTML

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/sankey.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">
        Sankey charts are used to visualize data flow and volume
        between nodes. The wider lines indicate larger volumes.
    </p>
</figure>

CSS

#csv {
	display: none;
}

.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 #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', {

    title: {
        text: 'Highcharts Sankey Diagram'
    },
    accessibility: {
        point: {
            valueDescriptionFormat: '{index}. {point.from} to {point.to}, {point.weight}.'
        }
    },
    series: [{
        keys: ['from', 'to', 'weight'],
        data: [
          ['Ashley', '1st year', 1],
            ['Ashley', 'Economics', 1],
            ['Audrey', '1st year', 1],
            ['Audrey', 'Biochemistry/Cell Biology', 1],
            ['Audrey', 'Chemistry Club', 1],
            ['Chemistry Club', 'Activity', 1],
            ['Bella', '2nd year', 1],
            ['Bella', 'Political Science', 1],
            ['Bella', 'Greek life', 1],
            ['Bella', 'Colleges Against Cancer', 1],
            ['Colleges Against Cancer', 'Activity', 1],
            ['Ben', '1st year', 1],
            ['Ben', 'Undecided', 1],
            ['Undecided', 'Arts & Sciences', 1],
            ['Cade', '2nd year', 1],
            ['Cade', 'Sociology', 1],
            ['Cade', 'Football', 1],
            ['Football', 'Sports', 2],
            ['1st year', '2024', 15],
            ['2nd year', '2023', 6],
            ['4th year', '2021', 1],
            ['Undecided', 'Arts & Sciences', 5],
            ['Accounting', 'Management', 1],
            ['Political Science', 'Arts & Sciences', 2],
            ['Psychology', 'Arts & Sciences', 3],
            ['Biochemistry/Cell Biology', 'Arts & Sciences', 1],
            ['Biology', 'Arts & Sciences', 1],
            ['Chemical Engineering', 'Engineering', 1],
            ['Economics', 'Arts & Sciences', 3],
            ['Education', 'Arts & Sciences', 1],
            ['International Relations', 'Arts & Sciences', 1],
            ['Mathematical Economics', 'Arts & Sciences', 1],
            ['Mechanical Engineering', 'Engineering', 1],
            ['MIDI', 'Management', 1],
            ['Digital Humanities', 'Arts & Sciences', 1],
            ['Sociology', 'Arts & Sciences', 1],
      ...