【发布时间】:2019-08-20 11:02:11
【问题描述】:
我正在尝试在vuejs 中为am4chart 构建一个组件,我可以在不同的地方重新使用它,我有一些搜索选项可以根据搜索结果更改chartData 值。
当我的搜索页面加载时,它会完美地显示图表,但当我尝试检查过滤器时,它不会根据数据更改或显示。我在mounted 部分中拥有与am4Chart 相关的所有代码,因此我尝试将相同的代码放在我的methods 中,并尝试从那里在mounted 部分中调用method。我还放了一个watch 函数,以便在数据发生变化时可以再次调用该函数。但是当我将它放在methods 中时,整个chart 消失了,事实上,如果我将相同的代码放在mounted 和methods 部分它不会显示。
我通过控制台检查了我的method 在chartData 更改时被调用,但不知道如何使其具有响应性。
VueJS版本2.6.10
am4Chart版本4.4.10
我的代码:
<template>
<div ref="amchart_semi_pie_chart"></div>
</template>
<script>
import * as am4core from "@amcharts/amcharts4/core";
import * as am4charts from "@amcharts/amcharts4/charts";
export default {
name: "amchart-semi-pie-chart",
props: {
chartData: Object,
},
data() {
return {
chart: '',
}
},
methods: {
loadChart() {
console.log('Values are changed')
}
},
mounted() {
//Creating charts
let chart = am4core.create(this.$refs.amchart_semi_pie_chart, am4charts.PieChart);
chart.hiddenState.properties.opacity = 0; // this creates initial fade-in
chart.data = this.chartData.data;
// Add and configure Series
chart.radius = am4core.percent(70);
chart.innerRadius = am4core.percent(40);
chart.startAngle = 180;
chart.endAngle = 360;
let series = chart.series.push(new am4charts.PieSeries());
series.dataFields.value = "value";
series.dataFields.category = "label";
series.slices.template.cornerRadius = 10;
series.slices.template.innerCornerRadius = 7;
series.slices.template.draggable = true;
series.slices.template.inert = true;
series.alignLabels = false;
if(this.chartData.legends)
chart.legend = new am4charts.Legend();
this.chart = chart;
},
watch: {
chartData: {
handler: 'loadChart',
deep: true
}
},
beforeDestroy() {
if (this.chart) {
this.chart.dispose();
}
}
}
</script>
【问题讨论】:
标签: vue.js vuejs2 vue-component amcharts amcharts4