X-range series
author(s): Torstein Hønsi
by Sascha
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/xrange.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
#container {
height: 300px;
}
.highcharts-figure, .highcharts-data-table table {
min-width: 320px;
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
var data = [{
"id": 19,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:01:02.137"
},
{
"id": 20,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:01:02.137"
},
{
"id": 21,
"name": "x",
"type": "job",
"state": "Stop",
"datetime": "2020-07-14 03:02:02.137"
},
{
"id": 22,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:03:02.137"
},
{
"id": 30,
"name": "x",
"type": "job",
"state": "Stop",
"datetime": "2020-07-14 03:04:02.137"
},
{
"id": 31,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:05:02.137"
},
{
"id": 32,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:06:02.137"
},
{
"id": 33,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:07:02.137"
},
{
"id": 34,
"name": "x",
"type": "job",
"state": "running",
"datetime": "2020-07-14 03:08:02.137"
},
{
"id": 35,
"name": "x",
"type": "job",
"state": "stop",
"datetime": "2020-07-14 03:09:02.137"
}
];
let latestMode = false;
let startTime = false;
let seriesData = [];
for (let entry of data) {
console.log(entry);
if (!latestMode) {
latestMode = entry.state;
startTime = new Date(entry.datetime);
} else if (latestMode !== entry.state) {
let currentTime = new Date(entry.datetime);
seriesData.push({
x: startTime.getTime(),
x2: currentTime.getTime(),
y: latestMode === 'running' ? 0 : 1
});
startTime = currentTime;
latestMode = entry.state;
}
}
Highcharts.chart('container', {
chart: {
type: 'xrange'
},
title: {
text: 'Job state'
},
accessibility: {
point: {
descriptionFormatter: function(point) {
var ix = point.index + 1,
category =...