JSFiddle - React, Tailwind, and code Playground

by maritavindedal

HTML

<script src="https://github.highcharts.com/bff0168f8c1080b17e062499acb1b5d12dcf9d7b/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>
<div id="container"></div>
<button id="stop">Stop updating live data</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

let updateIntervalId;

// Custom announce formatter - only report if there is a new point and it is
// above 10. Returning empty string stops announcement, returning false uses
// default announcement.
function onAnnounce(updatedSeries, newSeries, newPoint) {
    return newPoint && newPoint.y > 7 ? 'Alert: ' + newPoint.y : '';
}

// Create the chart
Highcharts.chart('container', {
    title: {
        text: 'Live updating data'
    },
    subtitle: {
        text: 'Points above 10 trigger alert by screen reader'
    },
    caption: {
        text: 'A test case for dynamic data in charts.'
    },
    accessibility: {
        announceNewData: {
            enabled: true,
            interruptUser: true,
            minAnnounceInterval: 0,
            announcementFormatter: onAnnounce
        }
    },
    chart: {
        type: 'spline',
        events: {
            load: function () {
                // Set up the updating of the chart each second
                const series = this.series[0];
                updateIntervalId = setInterval(function () {
                    series.addPoint(
                        Math.round(Math.random() * 110) / 10,
                        true,
                        series.points.length > 20
                    );
                }, 2000);
            }
        }
    },
    yAxis: {
        min: 0,
        max: 12,
        plotLines: [{
            value: 7,
            width: 2,
            color: '#e33'
        }]
    },
    series: [{
        name: 'Random data',
        dataLabels: {
            enabled: true
        },
        data: [1.1]
    }]
});

const stopButton = document.getElementById('stop');
stopButton.addEventListener('click', function () {
    clearInterval(updateIntervalId);
});