vue-chartjs 反应数据错误

2023-11-02前端开发问题
8

本文介绍了vue-chartjs 反应数据错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我正在尝试为 vue-chartjs 使用响应式数据混合

I am trying to use reactive data mixin for vue-chartjs

设置初始数据的挂载函数正在运行,我可以使用 API 响应正确查看图表:

The mounted function to set the initial data is working and I can see the chart correctly using the API response:

fetchSessionTrends() {
    axios.get(endpoint)
    .then(({data}) => {
        this.sessions = data.map(session => session.sessions);
        this.sessionLabels = data.map(label => label.date);
        this.loaded = true;
    });
},

数据:

data() {
    return {
       endpoint: 'public/api/sessions',
       sessions: [],
       sessionLabels: [],
       loaded: false,
       daysFilter: 14
    };
},

我正在显示带有文本字段的图表以提供反应部分 - 期望它再次调用端点并接收新响应

I am display the chart with a text field to provide the reactive portion - expectation is that it calls the endpoint again and receives new response

<div class="col-md-2 session-filter">
    <div class="input-group">
    <input type="text" class="form-control" placeholder="days..." v-model="daysFilter">
    <span class="input-group-btn">
      <button class="btn btn-secondary" type="button" @click="refreshSessions">Go</button>
    </span>
    </div>
</div>
<line-chart v-if="loaded" :chart-data="sessions" :chart-labels="sessionLabels"></line-chart>

然而,为了测试反应部分,现在我只是直接更改数据数组以查看它是如何工作的:

To test the reactive part however, for now I am simply changing the data arrays directly to see how it works:

refreshSessions() {          
   this.sessions = [1, 2, 3];
   this.sessionlabels = ["june", "july", "august"];  
},

是的,所以这给了我错误

Right, so this is giving me the errors

[Vue warn]: Error in callback for watcher "chartData": "TypeError: Cannot read property 'map' of undefined" found in ....

 TypeError: Cannot read property 'map' of undefined

LineChart.js 如文档中所述,此处缩写为空格

LineChart.js is as described in the docs, abbreviated here for space

import { Line, mixins } from 'vue-chartjs';
const { reactiveProp } = mixins

extends: Line,
mixins: [reactiveProp],
props: {
 chartData: {
   type: Array,
   required: true
 },
 chartLabels: {
   type: Array,
   required: true
 }
},

mounted() {
  this.renderChart({
    labels: this.chartLabels,
    datasets: [
      {
        label: 'sessions',
        data: this.chartData
      }
    ]
  }, this.options)
}

所以,图表最初工作正常,但我似乎无法让反应部分工作.

So, chart is initially working fine but I can't seem to get the reactive part working.

推荐答案

这行不通.因为 reactiveMixins 假设 chartData 是整个 chartjs 数据集对象.使用数据集数组,使用标签等.

This will not work. Because the reactiveMixins assume that chartData is the whole chartjs dataset object. With the dataset array, with the labels etc.

但是你把它分开了,这样反应混合器就不能工作了.因为你的chartData只是一个数据集的纯数据.

But you are splitting it up, this way the reactiveMixins can't work. Because your chartData is only the pure data of one dataset.

要解决它,你可以做两件事:

To solve it, you can do two things:

  1. 传入整个数据集对象
  2. 添加自己的观察者.

我想最简单的方法是添加两个观察者来观察 chartDatachartLabel 并在更改调用 this.$data._chart.update()

I guess the most simple method would be to add two watchers to watch chartData and chartLabel and on a change call this.$data._chart.update()

这篇关于vue-chartjs 反应数据错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6