无法在VUE中动态更新ApexChart类别(X轴标签)
我正在使用Apexchart的条形图,并注意到我无法更改X轴的标签,即类别
。以下是组件:
<template>
<div>
{{magnitudeByFreq}}
{{chartOptions}}
<apex-chart width="500" type="bar" :options="chartOptions" :series="series"></apex-chart>
</div>
</template>
<script>
export default {
props: {
processedMouseData: null,
gradientCountByType: null,
magnitudeByFreq: null,
},
data: function() {
return {
chartOptions: {
chart: {
id: 'vuechart-example'
},
xaxis: {
categories: []//['Positive', 'Neutral', 'Negative']
}
},
series: [{
name: 'series-1',
data: []
}]
}
},
mounted() {
},
watch: {
gradientCountByType: function() {
console.log(this.series.data)
this.gradientCountByType ? this.series[0].data = this.gradientCountByType : console.log("Not working")
this.gradientCountByType ? this.chartOptions.xaxis.categories = ['Positive', 'Neutral', 'Negative'] : console.log("No xaxis")
},
magnitudeByFreq: function() {
this.magnitudeByFreq ? this.series[0].data = Object.values(this.magnitudeByFreq) : console.log("ABX")
this.magnitudeByFreq ? this.chartOptions.xaxis.categories = Object.keys(this.magnitudeByFreq) : console.log("ABA")
}
}
};
</script>
当前类别
设置为[]
。这是因为我希望它会被不同的数据填充,具体取决于Prop的使用。即gradientCountByType
或agemugerbyfreq
。
下面应该设置类别
的两行:
this.gradientCountByType ? this.chartOptions.xaxis.categories = ['Positive', 'Neutral', 'Negative'] : console.log("No xaxis")
this.magnitudeByFreq ? this.chartOptions.xaxis.categories = Object.keys(this.magnitudeByFreq) : console.log("ABA")
它们似乎根本不更新类别。但是,我应该提到,模板中显示的内容{{MATEM级byfreq}}}
and {{ChartOptions}}}
,确实反映了类别中的更改< /code>变量:
{{ChartOptions}}}显示:
{ "chart": { "id": "vuechart-example" }, "xaxis": { "categories": [ "Positive", "Neutral", "Negative" ], "convertedCatToNumeric": false } }
为什么
{ "chart": { "id": "vuechart-example" }, "xaxis": { "categories": [ "+0", "+100", "+1000", "+2000" ], "convertedCatToNumeric": false } }
categories
属性无法正确显示?无论出于何种原因,类别
正在显示数字。
I am using Apexchart's bar chart and noticed that I am not able to change the x-axis's labels, ie the categories
. Below is the component:
<template>
<div>
{{magnitudeByFreq}}
{{chartOptions}}
<apex-chart width="500" type="bar" :options="chartOptions" :series="series"></apex-chart>
</div>
</template>
<script>
export default {
props: {
processedMouseData: null,
gradientCountByType: null,
magnitudeByFreq: null,
},
data: function() {
return {
chartOptions: {
chart: {
id: 'vuechart-example'
},
xaxis: {
categories: []//['Positive', 'Neutral', 'Negative']
}
},
series: [{
name: 'series-1',
data: []
}]
}
},
mounted() {
},
watch: {
gradientCountByType: function() {
console.log(this.series.data)
this.gradientCountByType ? this.series[0].data = this.gradientCountByType : console.log("Not working")
this.gradientCountByType ? this.chartOptions.xaxis.categories = ['Positive', 'Neutral', 'Negative'] : console.log("No xaxis")
},
magnitudeByFreq: function() {
this.magnitudeByFreq ? this.series[0].data = Object.values(this.magnitudeByFreq) : console.log("ABX")
this.magnitudeByFreq ? this.chartOptions.xaxis.categories = Object.keys(this.magnitudeByFreq) : console.log("ABA")
}
}
};
</script>
Currently the categories
is set to []
. This is because I want it to be filled by different data depending on which prop is using it. ie gradientCountByType
or magnitudeByFreq
.
The two lines below which are supposed to set the category
:
this.gradientCountByType ? this.chartOptions.xaxis.categories = ['Positive', 'Neutral', 'Negative'] : console.log("No xaxis")
this.magnitudeByFreq ? this.chartOptions.xaxis.categories = Object.keys(this.magnitudeByFreq) : console.log("ABA")
They don't seem to update the category at all. I should however mention that what gets displayed in the template {{magnitudeByFreq}}
and {{chartOptions}}
, do reflect there is a change in the category
variable:
{{chartOptions}} shows:
{ "chart": { "id": "vuechart-example" }, "xaxis": { "categories": [ "Positive", "Neutral", "Negative" ], "convertedCatToNumeric": false } }
and
{ "chart": { "id": "vuechart-example" }, "xaxis": { "categories": [ "+0", "+100", "+1000", "+2000" ], "convertedCatToNumeric": false } }
Why is the categories
attribute not displaying correctly? For whatever reason, the categories
are showing numbers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我的猜测是通过更改数据属性实际上并未更新图表。
相反,我们应该创建对图表的引用:
然后,我们可以使用
updateseries
方法更新数据,并使用update> updateOptions
方法更新图表:方法:My guess is by changing the data attribute doesn't actually update the chart.
Instead we should create a reference to the chart:
Then we can update the data with
updateSeries
method and update the chartOptions withupdateOptions
method:图表组件具有刷新功能。因此我们可以重新渲染图表。
Chart component has refresh function. So we can re-render the chart.
在VUE 3中,您应该将ChartOptions放入反应性类型中,以便随时可以将其更改。
然后在每一个甲壳部或生命周期中
In vue 3 you should put chartOptions in reactive type so you can change it in whenever you want.
then in every methode or lifecycle
我最近遇到了这个问题。要动态更新图表的X轴类别,您需要迭代数组(新类别)(使用或foreach),并使用push()方法将数组的每个元素添加到X轴类别中。
I recently faced this problem. To dynamically update the chart's x-axis categories, you need to iterate over the array (the new categories) (with for or forEach) and add each element of the array to the x-axis categories using the push() method.