【发布时间】:2017-07-21 14:57:28
【问题描述】:
我正在尝试使用随机数组值填充画布图。我正在使用 Veujs2 库来实现模板来显示图形数据。请找到我粘贴代码的以下文件。
import {Line} from 'vue-chartjs'
export default Line.extend({
created () {
this.generateRandomArray()
setInterval(function () {
this.generateRandomArray()
}.bind(this), 1000)
},
methods: {
generateRandomArray: function () {
this.dataForServerOne = Array.from({ length: 7 }, () => Math.floor(Math.random() * 40))
this.dataForServerTwo = Array.from({ length: 7 }, () => Math.floor(Math.random() * 40))
console.log(this.dataForServerOne + ' = ' + this.dataForServerTwo)
}
},
data: () => {
return {
dataForServerOne: [],
dataForServerTwo: []
}
},
mounted () {
this.renderChart({
labels: ['January', 'February', 'March', 'April', 'May', 'June', 'July'],
datasets: [
{
label: 'Server One',
backgroundColor: '#FC2525',
// data: [40, 39, 10, 40, 39, 80, 40]
data: this.dataForServerOne
},
{
label: 'Server Two',
backgroundColor: '#05CBE1',
data: this.dataForServerTwo
}
]
}, {responsive: true, maintainAspectRatio: false})
}
})
在 generateRandomArray() 中我有 dataForX 变量值,我无法在 renderchart 函数中使用这个变量。
【问题讨论】:
-
你确定你的库支持reactive data吗?
-
@MatJ - 我使用 chartjs 作为 vue(Vue-chartjs) 的包,我希望它应该支持响应式数据。
-
您检查了链接吗?是同一个图书馆吗?那么,我有消息要告诉你。
-
是的,我正在使用同一个库@Mat J