javascript - Vue-chart.js :获取部分数据;图表显示不好
问题描述
我想在我的Dashboard.vue
. x-axis
和上的数据Y-axis
来自我的 vuex 商店。当我检查浏览器上的 Vue 插件时,我可以看到来自商店的数据正在被访问。这是我的LineChart.js
文件版本(图表的逻辑所在的位置)。
import { Line } from 'vue-chartjs'
export default ({
extends: Line,
data() {
return {
xAxis: [],
yAxis: [],
title: "My activities"
}
},beforeMount(){
console.log('beforeMount')
},
computed: {
averageNumbers: function() {
console.log('computed')
return this.$store.getters.userData
},
daysItems: function(){
return this.$store.getters.userDataXAxis
}
},
watch: { // watch changes here
averageNumbers: function(newValue) {
console.log('watch')
for (var i=0; i < newValue.length; i++){
this.yAxis.push(newValue[i].val)
}
},
daysItems: function(newVal){
for (var i=0; i < newVal.length; i++){
this.xAxis.push(newVal[i])
}
}
},
methods:{
renderLineChart: function() {
this.renderChart({
labels: this.xAxis,
datasets: [
{ label: this.title, backgroundColor: '#dd4b39', data: this.yAxis }
]
},
{ responsive: true, maintainAspectRatio: false })
},
},
mounted() {
console.log('mounted')
this.renderLineChart();
}
})
正如你在图片上看到的,我有两个红点;这些是针对前两组数据的:[18 January and 63.5]
&[21 january and 23.9]
所以我只得到了五分之二(目前数据对的总数)。谢谢您的帮助!
解决方案
将观察者添加到 vuex 计算属性并触发更新。
this.$data._chart.update()
也许您的图表在您的所有数据都存在之前就被渲染了。
推荐阅读
- python - 记录枚举类的困难?
- python-3.x - 我的对称差分函数没有返回,我不明白为什么
- android - 如何在 linux 上运行 android x86 so 文件?
- python - Python:验证信用卡数字
- c# - 绑定到 ListView 上的 ViewModel 什么都没有显示,但在其他控件上工作正常
- java - Java 不能在自己创建的私有 void 中工作,但是在执行按钮操作时它可以
- math - 3D刚体变换分解重新排序
- java - 就餐中的 EXCEPTION_ACCESS_VIOLATION
- laravel - 使用 Vue.JS “操作” Laravel Blade 表单
- docker - {} 在 docker-compose.yml 的服务部分是什么意思