【发布时间】:2016-11-27 22:27:42
【问题描述】:
我有一个使用 Bootstrap、vue.js 和 chart.js 的简单应用。 HTML 模板非常简单。在搜索我的错误时,我注意到这个问题通常是在尝试在选项卡或模式中呈现图表时引起的,但我的图表没有在模式或选项卡中。
只要我调整浏览器窗口的大小,图表就会正确呈现并缩放以适应 div 和所有内容。
无论出于何种原因,当页面首次加载时,canvas HTML 标签呈现如下:
<canvas id="graph" width="0" height="0" style="margin: auto; width: 0px; display: block; height: 0px;"></canvas>
但是当我调整窗口大小时,一切都改变了,我可以正确地看到图表。
<canvas id="graph" width="493" height="328" style="margin: auto; width: 493px; display: block; height: 328px;"></canvas>
为什么页面加载时高度和宽度不正确?
HTML 模板
<div class="panel panel-default">
<div class="panel-heading">Chart List</div>
<div class="panel-body">
<div class="row">
<div class="col-sm-6">
<defaultchart>
</defaultchart>
</div>
<div class="col-sm-6">
<chartlistview
:data="{{ $private }}"
:columns="{{ json_encode($columns) }}"
>
</chartlistview>
</div>
</div>
</div>
</div>
DefaultChart.vue
<template>
<div>
<canvas
id="graph"
v-el:canvas
width="600"
height="400"
style="margin:auto; width: 600px;"
></canvas>
</div>
</template>
<script>
export default {
data() {
return {
ourChart: '',
chartData: {
labels: ["Red", "Blue", "Yellow", "Green", "Purple", "Orange"],
datasets: [{
label: '# of Votes',
data: [12, 19, 3, 5, 2, 3]
}]
}
};
},
ready() {
this.drawChart();
},
methods: {
drawChart: function() {
if (this.ourChart) {
this.ourChart.destroy();
}
var context = document.querySelector('#graph').getContext('2d');
this.ourChart = new Chart(context, {
type: 'line',
data: this.chartData
// wouldn't render at all with this regardless of resizing
/*options: {
response: true,
maintainAspectRatio: false
}*/
});
// added these lines to see if it would fix the issue; it didn't
this.ourChart.render();
this.ourChart.update();
}
}
};
</script>
【问题讨论】:
标签: twitter-bootstrap-3 chart.js vue.js