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/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">The following chart demonstrates a chart that animates quickly and is hidden from a screen reader.
        If you pause the data, the Accessibility module is enabled and you are able to explore the data.</p>
</figure>
<button id="toggle">Start animating</button>

CSS

#container {
    max-width: 800px;
    height: 400px;
    margin: 1em auto;
}

caption {
    padding-bottom: 15px;
    font-family: Verdana, sans-serif;
    font-size: 1.2em;
    color: #555;
}

table {
    font-family: Verdana, sans-serif;
    font-size: 12pt;
    border-collapse: collapse;
    border: 1px solid #ebebeb;
    margin: 10px auto;
    text-align: center;
    width: 100%;
}

table tr:nth-child(odd) {
    background-color: #fff;
}

table tr:nth-child(even) {
    background-color: #fcf9f9;
}

th {
    font-weight: 600;
    padding: 10px;
}

JavaScript

const chart = Highcharts.chart('container', {
    title: {
        text: 'Dynamic data'
    },
    subtitle: {
        text: 'Click button to animate or explore chart'
    },
    accessibility: {
        enabled: true
    },
    tooltip: {
        dateTimeLabelFormats: {
            day: '%H:%M',
            hour: '%H:%M'
        }
    },
    xAxis: {
        type: 'datetime',
        dateTimeLabelFormats: {
            day: '%H:%M',
            hour: '%H:%M'
        }
    },
    plotOptions: {
        series: {
            pointStart: 0,
            pointInterval: 1000 * 60 * 60
        }
    },
    series: [{
        name: 'Random data',
        data: [1, 3, 4, 6, 7, 5, 3, 4, 8, 9, 7, 6, 4, 3]
    }]
});

let intervalId;
let isAnimating = false;

const toggleButton = document.getElementById('toggle');
toggleButton.onclick = function () {
    toggleAnimation();
};

function toggleAnimation() {
    if (isAnimating) {
        clearInterval(intervalId);
        chart.update({
            accessibility: {
                enabled: true
            }
        });
        toggleButton.textContent = 'Start animating';
    } else {
        intervalId = setInterval(function () {
            chart.series[0].addPoint(Math.round(Math.random() * 10));
        }, 500);
        chart.update({
            accessibility: {
                enabled: false
            }
        });
        toggleButton.textContent = 'Stop animating';
    }
    isAnimating = !isAnimating;
}

// Start animating by default
toggleAnimation();