Highcharts.js integration in Elm
Highcharts.js is a JavaScript library to generate graphs inside the browser. It has a pretty simple API that allows you to create a wide range of different graphs. I stumbled upon this library when looking for a solution to create some graphs in an Elm web app that I’m working on.
Highcharts documentation gives an example to create a simple bar chart. In it, you are instructed to create a div
element, load it using jQuery
and call the highcharts
function on the element. So something like this
<div id="container"></div>
<script>
$(function () {
$('#container').highcharts({ ... });
});
</script>
The argument to highcharts
is just a dict that contains all data and configuration options for the chart as described in their API documentation. Easy enough.
But I want this to be integrated in an Elm web app, where I can’t just create a div
element and load it injQuery
. This is because the app uses elm-html
, which uses the virtual-dom
project behind the screens. This means that a virtual DOM will be created by the app, and rendered at runtime. So it’s not obvious when to call the highcharts
function on the div
that will contain the chart, since we don’t know when the HTML element will be present.