JSFiddle - React, Tailwind, and code Playground
by Umair Rafiq
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<div id="container"></div>
CSS
body{
background: #222
}
.highcharts-figure,
.highcharts-data-table table {
min-width: 360px;
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
const stops = [{
value: 8,
color: "red"
}, {
value: 16,
color: 'gray'
}, {
value: 20,
color: 'green'
}]
const value = 17;
const seriesData = [];
let previousStopValue = 0;
stops.forEach(stop => {
if (previousStopValue !== -1) {
if (value >= stop.value) {
seriesData.push({
data: [stop.value - previousStopValue],
color: stop.color
})
previousStopValue = stop.value
} else {
seriesData.push({
data: [value - previousStopValue],
color: stop.color
})
previousStopValue = -1
}
}
})
Highcharts.chart('container', {
chart: {
type: 'bar',
backgroundColor: '#555',
margin: 0,
padding: 0,
height: 15
},
title: null,
xAxis: {
enabled: false,
// categories: [],
title: {
text: null
},
// useHTML: true,
labels: {
enabled: false
},
},
yAxis: {
min: 0,
max: 20,
gridLineWidth: 3,
// tickPosition: 'inside',
gridLineColor: '#222',
// minorGridLineWidth: 0,
gridZIndex: 4,
labels: {
enabled: false
},
title: {
text: null
},
},
tooltip: {
enabled: false
},
plotOptions: {
bar: {
borderWidth: 0,
pointPadding: 0,
groupPadding: 0,
stacking: 'normal',
}
},
legend: {
enabled: false
},
credits: {
enabled: false
},
series: seriesData.reverse()
});