3
我有一個Twitter Bootstrap 3 Modal窗口,我想畫一個Chart.js線圖。但每次我打開模式時,畫布元素的高度和寬度均爲0.如果我更改這些值,則圖表爲空。它似乎是圖表從未繪製,但我沒有得到任何控制檯輸出或錯誤。Bootstrap Modal with Chart.js linechart
模態:
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="exampleModalLabel">New message</h4>
</div>
<div class="modal-body">
<p>Text</p>
<canvas id="canvas" width="400" height="400"></canvas>
<p>Text</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
的Javascript:
$('#exampleModal').on('show.bs.modal', function (event) {
var button = $(event.relatedTarget);
var modal = $(this);
var canvas = modal.find('.modal-body canvas');
// Chart initialisieren
var ctx = canvas[0].getContext("2d");
var chart = new Chart(ctx).Line({
labels: ["January", "February", "March", "April", "May", "June", "July"],
datasets: [
{
fillColor: "rgba(190,144,212,0.2)",
strokeColor: "rgba(190,144,212,1)",
pointColor: "rgba(190,144,212,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(220,220,220,1)",
data: [65, 59, 80, 81, 56, 55, 40]
},
{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
pointHighlightFill: "#fff",
pointHighlightStroke: "rgba(151,187,205,1)",
data: [65, 59, 80, 81, 56, 55, 40]
}
]
}, {});
});
按鈕:
<button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal" data-id="<?php echo $var["id"]; ?>">Open</button>
缺少什麼?爲什麼圖表永遠不會被繪製?
謝謝