Chartjs-How to access class variables inside QuickChart configuration?

1👍

Because QuickChart renders the chart on the server side, it doesn’t have access to your local variable. You can think of everything in setConfig as though it’s passed as a string rather than a function.

The most straightforward thing to do here is just pass the config as a string, and interpolate the language value:

        myChart.setConfig(`{
            type: 'bar',
            data: { // etc... },
            options: {
                scales: {
                    yAxes: [{
                        id: 'Left',
                        ticks: {
                            fontSize: 10,
                            callback: (value) => value.toLocaleString('${language}')
                        },
                    }]
                }
            }
        }`)

There are some alternative approaches in the QuickChart docs.

Leave a comment