我想使用带有Backbone View的Chart.js v2.1.4的升级版本(与v1.0.2一起正常运行),因为它有额外的功能。但它不能与升级版本v 2.1.4一起使用。
代码段是:
define([
'underscore',
'jquery',
'backbone',
'Chart',
'text!templates/dashboard/graph-view-template.html'
], function(_, $, Backbone, Chart, GraphViewTemplate) {
var GraphView = Backbone.View.extend({
template: GraphViewTemplate,
events: {
},
elements: {
},
initialize: function(options){
this.options = options;
},
render: function(){
this.$el.html(this.template);
this.renderTempGraph();
return this;
},
renderTempGraph: function(){
var ctx = this.$el.find("#myChart").get(0).getContext("2d");
var myChart = new Chart(ctx, {
type: 'bar',
data: {
labels: ["Red", "Blue", "Yellow"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3],
backgroundColor: [
'rgba(255, 99, 132, 0.2)',
'rgba(54, 162, 235, 0.2)',
'rgba(255, 159, 64, 0.2)'
],
borderColor: [
'rgba(255,99,132,1)',
'rgba(54, 162, 235, 1)',
'rgba(255, 159, 64, 1)'
],
borderWidth: 1
}]
},
options: {
scales: {
yAxes: [{
ticks: {
beginAtZero:true
}
}]
}
}
});
}
return GraphView;
});

<canvas id="myChart" width="400" height="400"></canvas>
&#13;
渲染后,canvas元素属性看起来像
<iframe class="chartjs-hidden-iframe" style="width: 100%; display: block; border: 0px; height: 0px; margin: 0px; position: absolute; left: 0px; right: 0px; top: 0px; bottom: 0px;">
</iframe>
<canvas id="myChart" width="0" height="0" style="width: 0px; height: 0px;"></canvas>
&#13;