JSFiddle - React, Tailwind, and code Playground
by maritavindedal
HTML
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/modules/accessibility.js"></script>
<script src="https://code.highcharts.com/modules/sonification.js"></script>
<div id="outer">
<div id="overlay">
<button class="btn btn-play" id="sonify">Click to play chart</button>
</div>
<figure class="highcharts-figure">
<div id="container" ></div>
</figure>
<div class="text-center">
<button class="btn btn-stop" id="stop">Stop playing</button>
</div>
</div>
CSS
.btn.btn-stop {
background-color: #fff;
border: 1px solid #25386f;
color: #25386f;
width: 20%;
font-size: 0.9rem;
min-height: 30px;
font-weight: 500;
}
.btn {
background-color: #25386f;
border: 1 px solid #25386f;
color: #fff;
min-height: 40px;
width: 30%;
border-radius: 4px;
font-size: 1.1rem;
font-weight: 700;
text-align: center;
padding: .375rem .75rem;
margin-bottom: .25rem;
margin-top: .25rem;
}
.text-center{
text-align: center;
}
.highcharts-figure {
width: 100%;
height: 100%;
margin: 0;
}
#outer {
min-width: 360px;
max-width: 800px;
margin: 0 auto;
position: relative;
}
#overlay {
display: flex;
align-items: center;
justify-content: center;
position: absolute;
top: 0;
bottom: 0;
right: 0;
left: 0;
z-index: 999;
background-color: rgba(255, 255, 255, 0.75);
}
#stop {
visibility: hidden;
}
@media (max-width: 575px){
.btn.btn-play {
width: 50%;
}
.btn.btn-stop {
width: 30%;
}
}
JavaScript
var chart = Highcharts.chart('container', {
chart: {
type: 'spline'
},
title: {
text: 'Sonify series in order'
},
subtitle: {
text: 'Earcon on highest point in series'
},
sonification: {
duration: 7000,
defaultInstrumentOptions: {
minFrequency: 220,
maxFrequency: 2200,
mapping: {
duration: 200
}
},
earcons: [{
// Define the earcon we want to play
earcon: new Highcharts.sonification.Earcon({
instruments: [{
instrument: 'triangleMajor',
playOptions: {
// Play a quick rising frequency
frequency: function (time) {
return time * 1760 + 440;
},
volume: 0.3,
duration: 200
}
}]
}),
// Play this earcon if this point is the highest in the series.
condition: function (point) {
var series = point.series;
return point.y === Math.max.apply(Math, series.points.map(
function (p) {
return p.y;
}
));
}
}],
events: {
onPointStart: function (e, point) {
point.onMouseOver();
},
onEnd: function () {
document.getElementById('sonify').style.visibility = 'visible';
document.getElementById('overlay').style.visibility = 'visible';
document.getElementById('stop').style.visibility= 'hidden';
}
}
},
series: [{
data: [1, 2, 4, 5, 7, 9, 11, 13]
}, {
data: [4, 5, 9, 5, 2, 1, 4, 6]
}, {
data: [2, 2, 2, 7, 9, 11, 13, 12]
...