[Chartjs]-How do I access chart.js options dynamically?

3👍

To update the options, you either update individual properties or update the entire object. Then you must force a redraw on the chart for the new chart options to take effect (see chartjs docs).

For example:

const ctx = document.getElementById("myChart").getContext('2d');
const chart = new ChartJS(ctx, { title: { text: 'old title' } });
chart.options.title.text = 'new title';
chart.update();

1👍

//get object
const chartJS: Chart = $("#myChart").data('chart');

//change something
chartJS.config.options.scales.yAxes[0].ticks.min = minVal <= 0 ? minVal : minVal - 

//update
chartJS.update();

make sure you add the object as data after creation

....
var ctx = canvas.getContext("2d");
var myChart = new Chart(ctx, {
                type: 'line',
                data: chartData,
                options
            });

//
var canvasItem = $("#myChart");
canvasItem.data('chart', myChart);

Leave a comment