简体   繁体   English

使用来自 Firestore 实时侦听器的数据更新图表

[英]Updating chart with data from Firestore realtime listener

In my Vue app I am using Apexcharts to display some data.在我的 Vue 应用程序中,我使用 Apexcharts 来显示一些数据。 I am grabbing the data from my Firestore database by using a realtime listener.我正在使用实时侦听器从我的 Firestore 数据库中获取数据。

The chart is working as it should and I am also getting the data in realtime.该图表正在正常工作,我也在实时获取数据。 The problem is that my chart is not updating itself with the new data, and I am not sure on how to approach it.问题是我的图表没有用新数据更新自己,我不确定如何处理它。

I am fetching the data my parent component through this script:我正在通过此脚本获取我的父组件的数据:

onMounted(async () => {
      const unsub = onSnapshot(doc(db, "testUsers", "rtBp8UHReBE2rACDBHij"), (doc) => {
        getWeight.value = doc.data();
      });
      watchEffect((onInvalidate) => {
        onInvalidate(() => unsub());
      });
    });

I am sending the data to my child component through props like this:我通过这样的道具将数据发送到我的子组件:

watch(
      () => props.getWeight,
      (getWeights) => {
        weight.value = [...getWeights.weightData.weight];

        let numeric = { day: "numeric", month: "numeric" };

        getWeights.weightData.date.forEach((dates) => {
          date.value.push([dates.toDate().toLocaleDateString("se-SW", numeric)]);
        }),
      }
    );

My chart in the child component looks something like this:我在子组件中的图表如下所示:

  <apexchart class="apexchart" type="line" :options="options" :series="series">
  </apexchart>

<script>
export default {
  props: ["weight", "date"],

  setup(props) {
    return {
      options: {

        xaxis: {
          type: "category",
          categories: props.date,

          axisBorder: {
            show: false,
          },
        },
      },
      series: [
        {
          name: "Værdi",
          data: props.weight,
        },
      ],
    };
  },
};
</script>

How can I make my chart update with the new data from the realtime listener?如何使用来自实时侦听器的新数据更新图表?

If in chart options you add id you would be able to call exec and update your chart如果在图表options中添加id ,您将能够调用exec并更新您的图表

Example:例子:

import ApexCharts from "apexcharts";

ApexCharts.exec('chartId', 'updateOptions', {
  series: [
    {
      name: 'Værdi',
      data: newWeights,
    },
  ],
  xaxis: {
    categories: newDates,
  },
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM