This package provide a (WIP) integration between elm and Apex charts via either custom-element or ports.
The main thing this package does is provide a simple and declarative way to describe a chart and encode it so that it can directly be picked up by Apex Charts.
Note, this package comes with an "already made" custom component which you can install and use via the node package version of elm-apex-charts-link (see the README).
Here is how you would describe a simple chart with some options (checkout the example project for more use-cases):
import Apex
myChart : Html Msg
myChart =
Apex.apexChart
(Apex.chart
|> Apex.addLineSeries "Connections by week" (connectionsByWeek logins)
|> Apex.addColumnSeries "Connections within office hour for that week" (dayTimeConnectionByWeek logins)
|> Apex.addColumnSeries "Connections outside office hour for that week" (outsideOfficeHourConnectionByWeek logins)
|> Apex.withXAxisType Apex.DateTime
)
[]
[]For that please refer to the README page
A simple record type to make things a bit clearer when writing series
as the name suggest, this add a line to your chart by creating a series with the given name and by linking the given points together.
as the name suggest, this add a new column series to your chart using the given name and by adding a bar for each of the given points.
Allow to turn on or off the legend for the graph
change the type of x-axis used in you graph
Describe how the x-axis of your graph should be labelled.
It can be either a Category, a DateTime or a Numeric value
NOTE: for the DateTime to properly work I suggest that the x-values in your series should be turned into miliseconds via Time.posixToMillis. I hope to find something better in due time but that's the best option until then.
NOTE: A lot more support for options and type of series needs to be added
To render your chart you have 2 options:
update : Msg -> Model -> ( Model, Cmd Msg )
update msg _ =
case msg of
LoadFakeLogins now ->
let
logins =
fictiveLogins Time.utc now
in
( logins
, updateChart <|
Apex.encodeChart <|
(Apex.chart
|> Apex.addLineSeries "Connections by week" (connectionsByWeek logins)
|> Apex.addColumnSeries "Connections within office hour for that week" (dayTimeConnectionByWeek logins)
|> Apex.addColumnSeries "Connections outside office hour for that week" (outsideOfficeHourConnectionByWeek logins)
|> Apex.withXAxisType Apex.DateTime
)
)app.ports.updateChart.subscribe((chartDescription) => {
const chart = new ApexCharts(
document.querySelector('#chart1'),
chartDescription
)
chart.render()
})this function takes a chart and turns it into JSON data that Apex Charts can understand
NOTE: if you are using the custom-element version you should not need to use this function
use npm install elm-apex-charts-link to gain access to the custom element and then use:
import Apex
myChart : Html Msg
myChart =
Apex.apexChart
(Apex.chart
|> Apex.addLineSeries "Connections by week" (connectionsByWeek logins)
|> Apex.addColumnSeries "Connections within office hour for that week" (dayTimeConnectionByWeek logins)
|> Apex.addColumnSeries "Connections outside office hour for that week" (outsideOfficeHourConnectionByWeek logins)
|> Apex.withXAxisType Apex.DateTime
)
[]
[]this is the custom element wrapper.
Make sure that you have installed the javascript companion package (npm i elm-apex-charts-link) before using this function!
this is the entry point of any chart: it gives you an empty chart with some default options