Column range chart

author(s): Øystein Moseng

by oysteinmoseng

HTML

<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-more.js"></script>
<script src="https://code.highcharts.com/modules/data.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>
<script src="https://code.highcharts.com/themes/high-contrast-light.js"></script>

<h1>Column range chart</h1>
<h2>Chart</h2>
<figure class="highcharts-figure">
    <div id="container"></div>
    <p class="highcharts-description">
        Column range chart with time axis.
    </p>
</figure>
<h2>CSV data</h2>
<p>
Source: <a href="https://data.world/newns92/abbott-world-marathon-majors-winners">data.world</a>.
</p>
<pre id="csv">Country,Minimum time,Maximum time
Kenya,02:02:57,02:14:13
Ethiopia,02:03:03,02:12:45
United States,02:05:38,02:55:10
United Kingdom,02:07:13,02:15:42
Japan,02:08:27,02:27:45
Mexico,02:08:30,02:11:21
Germany,02:15:19,02:47:08</pre>

CSS

body {
  font-family: Verdana, sans-serif;
  padding-left: 10px;
}

h2 {
  font-size: 1.3rem;
  margin-left: 10px;
  margin-bottom: 0;
  margin-top: 1.5rem;
}

p {
    margin-left: 1rem;    
}

pre {
  margin-left: 1rem;
  border: 1px solid #cde;
  max-width: 600px;
  padding: 10px;
  background-color: #f9f9f9;
  border-radius: 10px;
}

#container {
    height: 400px;
}

.highcharts-figure, .highcharts-data-table table {
    min-width: 320px;
    max-width: 600px;
    margin: 1em;
}

.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;
}


input[type="number"] {
	min-width: 50px;
}

JavaScript

function timestampToMs(timestamp) {
    var timeArr = timestamp.split(':');
    return (
    	parseFloat(timeArr[0]) * 60 * 60 +
        parseFloat(timeArr[1]) * 60 +
        parseFloat(timeArr[0])
    ) * 1000;
}

function msToTimestamp(ms) {
	function formatNum(num) {
		return (num < 10 ? '0' : '') + num;
    }

	var timeObj = new Date(ms);
    return timeObj.getUTCHours() + ':' + formatNum(timeObj.getUTCMinutes()) + ':' + formatNum(timeObj.getUTCSeconds());
}

// Configure the visualization
var chart = Highcharts.chart('container', {
    chart: {
        type: 'columnrange',
        inverted: true
    },
    title: {
        text: 'Marathon winning time range by country'
    },
    subtitle: {
    	text: 'Source: <a href="https://data.world/newns92/abbott-world-marathon-majors-winners">data.world</a>'
    },
    tooltip: {
    	backgroundColor: 'rgba(255, 255, 255, 0.9)',
	    stickOnContact: true,
		formatter: function () {
        	return '<span style="font-size: 11px">' + this.point.name + '</span><br/>' +
				'<span style="color:' + this.series.color + '">\u25CF</span> ' +
            	msToTimestamp(this.point.low) + ' - ' + msToTimestamp(this.point.high) + '<br/>';
        }
    },
    legend: {
    	enabled: false
    },
    yAxis: {
    	title: {
        	text: ''
        },
        type: 'datetime',
        min: 2*60*60*1000
    },
    data: {
    	csv: document.getElementById('csv').innerHTML,
        parsed: function (res) {
			var i = res[1].length;
            while (i--) {
            	res[1][i] = timestampToMs(res[1][i]);
                res[2][i] = timestampToMs(res[2][i]);
            }
        }
    }
});