Chartjs-Using and reading a variable from Knockout.js in Chart.js

0👍

What you should do is create a custom binding handler for ChartJS:

ko.bindingHandlers.chartJS = {
    update: function (element, valueAccessor) {
        // do the chartJS stuff here
        // access the chart data by calling valueAccessor()
    }
};

Then you would call it in your template like so:

<canvas data-bind="chartJS: myChartData"></canvas>

Where myChartData would be an (observable) property on your viewmodel. Should the data change (through an AJAX call), your chart will update automatically.

Leave a comment