我有以下angularJS指令为什么ChartJS图表会以0和0的宽度结束?
app.directive('answersPlot', function() {
return {
scope: {
chartData: "=chartData",
chartType: "=chartType"
},
template: '<canvas width="100px" height="100px" style="width:100px; !important height:100px; !important"></canvas>',
link: function(scope, element, attrs) {
var canvas = element[0].childNodes[0];
var context = canvas.getContext('2d');
var chart = new Chart(context);
if (scope.chartType == 0) {
chart.Bar(scope.chartData, {});
} else {
chart.Pie(scope.chartData, {});
}
}
}
});
我实例化它的NG-重复DOM块内像这样
<div ng-repeat="question in talk.questions track by $index">
<answers-plot chart-data="question.chartData" chart-type="question.chartType"></answers-plot>
</div>
然而,当我检查DOM我觉得这
<answers-plot chart-data="question.chartData"
chart-type="question.chartType"
class="ng-isolate-scope">
<canvas width="0" height="0" style="width: 0px; height: 0px;"></canvas>
</answers-plot>
没有错误登录到控制台。 chartData
具有这种格式:"{"labels":["A","C","D","B"],"datasets":[{"data":[0,2,0,1]}]}"
。
手动更改宽度/高度值(在CSS中作为属性)只会生成更大的空白画布。
我已经尝试了两个经历同样问题的angularjs chartjs库,使用编写自己的指令来验证数据实际上是到达chartjs库。仍然没有坚果。我究竟做错了什么?
还没有骰子,同样的问题。 ChartJS会将宽度和高度(在CSS和属性中)都替换为0. – Machinarius