JSFiddle - React, Tailwind, and code Playground
by maritavindedal
January 14, 2025
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/export-data.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<figure class="highcharts-figure">
<div id="container"></div>
<p class="highcharts-description">
This bar chart compares the estimated production of corn and wheat for 2023 across six countries:
the USA, China, Brazil, the EU, Argentina, and India. Corn production significantly exceeds wheat production
in countries like the USA and Brazil, while wheat production surpasses corn in the EU. The chart uses metric tons
(MT) as the unit and highlights data sourced from IndexMundi.
</p>
</figure>
CSS
#container {
max-width: 800px;
margin: 1em auto;
}
#pref-menu-dialog {
z-index: 9999;
background-color: white;
padding: 20px;
border: 1px solid black;
border-radius: 5px;
min-width: 320px;
}
.pref {
display: flex;
align-items: center;
gap: 10px;
}
h3 {
margin-bottom: 10px;
}
/* To avoid screen reader region disappearing after chart.update() */
.hide-section {
position: static !important;
width: auto !important;
height: auto !important;
overflow: visible !important;
white-space: normal !important;
clip: auto !important;
margin-top: 0 !important;
opacity: 1 !important;
}
.highcharts-button-normal:hover,
.highcharts-button-normal-hover {
cursor: pointer;
}
#small-font {
font-size: 10px !important;
}
#default-font {
font-size: 16px !important;
}
#large-font {
font-size: 22px !important;
}
.alt-text-div {
position: absolute;
background: white;
border: 1px solid #ccc;
padding: 4px;
font-size: 12px;
transform: translate(-50%, -100%);
pointer-events: none;
}
#hc-pref-button text {
font-size: 1.2em !important;
}
#hc-pref-button rect {
fill: rgb(255, 255, 255) !important;
stroke: rgb(255, 255, 255) !important;
}
#hc-pref-button:hover rect {
fill: rgb(211, 211, 211) !important;
stroke: rgb(211, 211, 211) !important;
}
JavaScript
/**
* TODO: Make preferences only an icon
* TODO: Make points work with verbosity settings
* TODO: Add white border to the columns of the chart?
* TODO: Add local storage
* TODO: Calculate contrast with function instead of hardcoded
*/
// Storing preference states globally
let selectedVerbosity = 'full',
selectedTextSize = 'default',
isContrastChecked = false,
isBorderChecked = false,
isAltPointChecked = false,
isInfoChecked = false,
defaultDesc = '';
function initializeChart() {
const chart = Highcharts.chart('container', getChartConfig());
chart.prefMenu = {};
chart.altTextDivs = [];
addPrefButton(chart);
addCustomA11yComponent(chart);
addPrefButtonScreenReader(chart);
return chart;
}
function getChartConfig() {
return {
chart: {
type: 'column'
},
accessibility: {
screenReaderSection: {
beforeChartFormat: '<h1>{chartTitle}</h1>' +
'<div>{typeDescription}</div>' +
'<div>{chartSubtitle}</div>' +
'<div>{chartLongdesc}</div>' +
'<div>{playAsSoundButton}</div>' +
'<div>{viewTableButton}</div>' +
'<div>{xAxisDescription}</div>' +
'<div>{yAxisDescription}</div>'
}
},
title: {
text: 'Corn vs wheat estimated production for 2023'
},
subtitle: {
text:
'Source: <a target="_blank" ' +
'href="https://www.indexmundi.com/agriculture/?commodity=corn">indexmundi</a>'
},
xAxis: {
categories: ['USA', 'China', 'Brazil', 'EU', 'Argentina', 'India'],
crosshair: true,
accessibility: {
description: 'Countries'
}
},
yAxis: {
min: 0,
title: {
text: '1000 metric tons (MT)'
}
},
tooltip: {
...