Chartjs-Chart.js misreading arrays for labels and axes

1👍

This is tricky!

I see these lines in the ejs file

const entryDate = "<%= entryDate %>"

When this is actually rendered, it will run toString() against the value

['Week 1','Week 2','Week 3'].toString()
'Week 1,Week 2,Week 3'

And in the template its surrounding that toString() response in quotes:

const entryDate = "Week 1,Week 2,Week 3"

Since a string is just an array of characters it renders in that strange way.

Try doing this for the arrays in ejs instead:

const entryDate = <%- JSON.stringify(entryDate) %>
// also notice we removed the quotes! :)

This will run JSON.stringify against it, producting the string:

> JSON.stringify(['Week 1','Week 2','Week 3'])
'["Week 1","Week 2","Week 3"]'

So it will render like this:

const entryDate = ["Week 1","Week 2","Week 3"]

Do this for all your arrays and it should work!

Leave a comment