无法在VUE中动态更新ApexChart类别(X轴标签)

发布于 2025-01-31 13:16:31 字数 2762 浏览 0 评论 0原文

我正在使用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的使用。即gradientCountByTypeagemugerbyfreq

下面应该设置类别的两行:

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.

enter image description here

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

并安 2025-02-07 13:16:31

我的猜测是通过更改数据属性实际上并未更新图表。

相反,我们应该创建对图表的引用:

<apex-chart ref="radar" type="radar" height="350" :options="chartOptions" :series="series"></apex-chart>

然后,我们可以使用updateseries方法更新数据,并使用update> updateOptions方法更新图表:方法:

this.$refs.radar.updateSeries([{
      name: 'Series 1',
      data: [your_new_data_here] //ie [1,2,3,4]
}])

this.$refs.radar.updateOptions({
      xaxis: {
        categories: [your_new_categories_here] //ie ["a","b","c","d"]
      }
})

My guess is by changing the data attribute doesn't actually update the chart.

Instead we should create a reference to the chart:

<apex-chart ref="radar" type="radar" height="350" :options="chartOptions" :series="series"></apex-chart>

Then we can update the data with updateSeries method and update the chartOptions with updateOptions method:

this.$refs.radar.updateSeries([{
      name: 'Series 1',
      data: [your_new_data_here] //ie [1,2,3,4]
}])

this.$refs.radar.updateOptions({
      xaxis: {
        categories: [your_new_categories_here] //ie ["a","b","c","d"]
      }
})
明媚殇 2025-02-07 13:16:31
<apex-chart ref="chart" :options="options" :series="series"/>

图表组件具有刷新功能。因此我们可以重新渲染图表。

this.series[0].data = [yourData];
this.options.xaxis.categories = [yourCategories]
this.$refs.chart.refresh();
<apex-chart ref="chart" :options="options" :series="series"/>

Chart component has refresh function. So we can re-render the chart.

this.series[0].data = [yourData];
this.options.xaxis.categories = [yourCategories]
this.$refs.chart.refresh();
┈┾☆殇 2025-02-07 13:16:31

在VUE 3中,您应该将ChartOptions放入反应性类型中,以便随时可以将其更改。

const chartOptions =ref({// your codes});

然后在每一个甲壳部或生命周期中

onMounted( () => {   chartOptions.value.xaxis.categories=myArrayValues;    }); }

In vue 3 you should put chartOptions in reactive type so you can change it in whenever you want.

const chartOptions =ref({// your codes});

then in every methode or lifecycle

onMounted( () => {   chartOptions.value.xaxis.categories=myArrayValues;    }); }
春风十里 2025-02-07 13:16:31

我最近遇到了这个问题。要动态更新图表的X轴类别,您需要迭代数组(新类别)(使用或foreach),并使用push()方法将数组的每个元素添加到X轴类别中。

let newCategories = ['Positive', 'Neutral', 'Negative']
newCategories.forEach(category => {
    this.chartOptions.xaxis.categories.push(category)   
})

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.

let newCategories = ['Positive', 'Neutral', 'Negative']
newCategories.forEach(category => {
    this.chartOptions.xaxis.categories.push(category)   
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文