【发布时间】:2016-01-13 10:46:28
【问题描述】:
好的,所以我正在尝试通过 VueJS 创建一个 ChartJS 实例,以便我可以通过组件内的 AJAX 请求轻松更新其数据。
基本上它正在工作,ChartJS 实例被创建但它是空的并且高度和宽度为 0,当我通过控制台调整它的大小时它仍然是空的。
那么使用 VueJS 创建“ChartJS 组件”的最佳方法是什么,或者我下面的代码中的错误在哪里?
我想加载图表的最基本方式是这样的。
<chart :resource="'https://httpbin.org/get'"></chart>
这是基本组件
var Vue = require('vue');
Vue.component('chart', {
template: require('./template.html'),
props: ['resource'],
data: function() {
return {
startingData: {},
latestLabel: {},
}
},
ready: function() {
// here I would load the js data and set the startingData
},
});
这是我用来从组件传递数据的指令。我这样做是为了得到this.el,这样我就可以得到它的上下文
Vue.directive('loadData', function(value) {
var startingData = {
labels: [1, 2, 3, 4, 5, 6, 7],
datasets: [{
fillColor: "rgba(151,187,205,0.2)",
strokeColor: "rgba(151,187,205,1)",
pointColor: "rgba(151,187,205,1)",
pointStrokeColor: "#fff",
data: [28, 48, 40, 19, 86, 27, 90]
}]
},
latestLabel = startingData.labels[6];
var liveChart = new Chart(this.el.getContext('2d')).Bar(startingData);
setInterval(function() {
liveChart.addData([Math.random() * 100], ++latestLabel);
liveChart.removeData();
}, 1000);
});
这是组件的模板
<canvas width="960" height="300" v-load-data="startingData"></canvas>
【问题讨论】: