我有以下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