Search code examples
canvaschart.jsnode-canvas

How can I tighten letter spacing on labels with ChartJS?


I'm trying to draw a chart using https://www.npmjs.com/package/chartjs-node-canvas but the labels have a strange letter spacing and they look too spaced out.

Below is an image of a chart drawn with chart.js on the browser on the left and a chart drawn with chartjs-node-canvas on the right. You can see the labels are the only thing that's really throwing it off.

I found a similar issue here Chart.js letter spacing very awkward but no answers on that question so here's hoping someone has a solution.

left chart browser drawn, right chart chartjs node canvas drawn

I've tried tweaking canvas.styles.letterSpacing of the canvas directly in the node library, but it hasn't worked so far.

Anyone else with any ideas?


Solution

  • After trying countless of different things, I found out that the issue was in the devicePixelRatio setting I was using to replicate the config from the browser version. This was originally set to 2 in the browser version, I removed it from the node.js version and now the graph looks as expected (or at least close to):

    corrected chart

    Documenting this here in case someone else stumbles upon this question or issue in the future.