如何更新 ApexCharts 系列中的数据 我使用 ApexCharts 创建 了以下 _Vue 组件_。该组件从这些组件所在的父级更新。更新后的值是通过 props 传入的。
<template>
<div>
<apexchart type="line" width="1000px" :options="options" :series="series"></apexchart>
</div>
</template>
<script>
import Vue from 'vue';
import VueApexCharts from 'vue-apexcharts';
Vue.use(VueApexCharts);
Vue.component('apexchart', VueApexCharts);
export default {
name: 'EnergyConsumption',
props: {
channel1: Number,
channel2: Number,
},
data() {
return {
options: {
chart: {
id: 'vuechart-example',
},
xaxis: {
categories: ['Channel 1', 'Channel 2'],
},
},
series: [
{
name: 'series-1',
data: [this.channel1, this.channel2],
},
],
};
},
methods: {
updateSeries() {
this.series[0].data = [this.channel1, this.channel2];
},
},
watch: {
channel1: function (_channel1) {
this.updateSeries();
// this.series[0].data[0] = _channel1;
},
},
};
</script>
使用 Vue DevTools ,我可以看到 ApexCharts 组件内部的数据正在发生变化,但视图没有更新。
如果我查看 ApexCharts 文档,我会看到有一种方法可以更新系列
updateSeries (newSeries, animate)
但我需要一个实例来调用 updateSeries
来自。如果我使用模板机制,我就不能引用那个模块。
解决此问题的最佳方法是什么?
原文由 Bik Lander 发布,翻译遵循 CC BY-SA 4.0 许可协议
您需要使用
ref
更新系列。