[Chartjs]-Chartjs with Backbone

6👍

Complementing to answers and comments of others, I get this solution:

var MyChartView = Backbone.View.extend({
    template: _.template($('#myChart-template').html()),
    render: function(){
        $(this.el).html(this.template());

        var data = ...
        var options = ...

        var ctx = $('#myChart', this,el)[0].getContext("2d");
        var myLineChart = new Chart(ctx).Line(data, options);
}

Hope this help you.

0👍

Since document.getElementById("myChart"), is unable to find the canvas/myChart as it is within the script tag.

Try this,

var ctx = $($('#myChart-template').html()).children('#myChart').get(0).getContext("2d");

Hope this solves the issue. 🙂

Leave a comment