如何使用Vue-Chartjs发出活动?

发布于 2025-01-31 19:08:05 字数 904 浏览 0 评论 0原文

我使用VUE JS,并显示带有Chartjs的图形。当我单击图表时,我希望发出一个事件以获取父组件中的数据。我的onclick功能有效,但事件不起作用。 你对我的问题有想法吗? 组件线2.

<script>
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
  extends: Line, 
  mixins: [reactiveProp],
  props: ["options"],
  mounted() {
    const self = this;
    console.log(self);
    this.options.onClick = function (e, tooltipItems) {
      console.log(tooltipItems[0].__ob__); //.logged
      self.$emit("sendIndex", tooltipItems[0]._index);
    };
    this.renderChart(this.chartData, this.options);
  },
};
</script>
<style scoped></style>

我的主要组件

...
    <h1 v-on:sendIndex="getIndexCoord($event)">{{ indexCoord }}</h1>
...
methods: {
    getIndexCoord: function (e) {
      console.log("rrr", e); //Not logged
      this.indexCoord = e;
    },
}

求职

I use vue js and I display a graph with chartjs. When I click on the graph I want emit an event for get data in parent component. My onClick function works but not the event.
Do you have an idea of my problem ?
Component Line2.vue

<script>
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
  extends: Line, 
  mixins: [reactiveProp],
  props: ["options"],
  mounted() {
    const self = this;
    console.log(self);
    this.options.onClick = function (e, tooltipItems) {
      console.log(tooltipItems[0].__ob__); //.logged
      self.$emit("sendIndex", tooltipItems[0]._index);
    };
    this.renderChart(this.chartData, this.options);
  },
};
</script>
<style scoped></style>

My main component

...
    <h1 v-on:sendIndex="getIndexCoord($event)">{{ indexCoord }}</h1>
...
methods: {
    getIndexCoord: function (e) {
      console.log("rrr", e); //Not logged
      this.indexCoord = e;
    },
}

Regards

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

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

发布评论

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

评论(2

美羊羊 2025-02-07 19:08:06

vue-chartjs document 不描述处理图表事件,但是那里是示例

我们需要在图表上创建Ref,然后我们可以使用提供的getElementsateTateVent函数来访问有关单击元素的信息。检查链接的示例以获取更多实用程序功能 - 在这里,我刚刚提供了简化的设置。

<template>
  <Bar
    :data="chartData"
    :options="options"
    ref="chartInstance"
    @click="onClick"
  />
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { ChartComponentRef } from "vue-chartjs";
import { Bar, getElementsAtEvent } from "vue-chartjs";
import type { InteractionItem } from "chart.js";

const chartInstance = ref<ChartComponentRef | null>(null);

const onClick = (event: MouseEvent) => {
  const chart = chartInstance.value!.chart;

  if (!chart) {
    return;
  }

  const elements: InteractionItem[] = getElementsAtEvent(chart, event);
  if (!elements.length) return;

  console.log("elements", elements);
};
</script>

PS:当我第一次尝试使用[emagy&nbsp; emagy&nbsp; /code>我看到了错误打字稿错误ts2590:表达式产生的联合类型太复杂而无法表示。 - 我升级到

vue-chartjs documentation doesn't describe handling chart events, but there is an example.

We need to create a ref to our chart then we can use the provided getElementsAtEvent function to access information about the clicked elements. Check the linked example for more utility functions - here I have just provided a simplified setup.

<template>
  <Bar
    :data="chartData"
    :options="options"
    ref="chartInstance"
    @click="onClick"
  />
</template>

<script setup lang="ts">
import { ref } from "vue";
import type { ChartComponentRef } from "vue-chartjs";
import { Bar, getElementsAtEvent } from "vue-chartjs";
import type { InteractionItem } from "chart.js";

const chartInstance = ref<ChartComponentRef | null>(null);

const onClick = (event: MouseEvent) => {
  const chart = chartInstance.value!.chart;

  if (!chart) {
    return;
  }

  const elements: InteractionItem[] = getElementsAtEvent(chart, event);
  if (!elements.length) return;

  console.log("elements", elements);
};
</script>

PS: When I first tried this with [email protected] I was seeing the error TypeScript Error TS2590: Expression produces a union type that is too complex to represent. - I upgraded to [email protected] and vue-tsc@^2 and it fixed my type problem.

计㈡愣 2025-02-07 19:08:05

1.首先创建eventbus.js文件
从“ vue”导入vue;

export const EventBus = new Vue();

2.在您的char.js文件代码下方,如下

import { EventBus } from "../EventBus.js";
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
  extends: Line, 
  mixins: [reactiveProp],
  props: ["options"],
  mounted() {
    const self = this;
    console.log(self);
    this.options.onClick = function (e, tooltipItems) {
      console.log(tooltipItems[0].__ob__); //.logged
      EventBus.$emit("sendIndex", tooltipItems[0]._index);
    };
    this.renderChart(this.chartData, this.options);
  },
};
  1. 您要在该文件中访问该数据的位置,例如

     来自“ ..//eventbus.js”的导入{eventBus};
    安装(){
    
       eventbus。$ on('sendIndex',data =&gt; {
          console.log(数据)
         });
    
    },,
     

1.first you create EventBus.js file
import Vue from 'vue';

export const EventBus = new Vue();

2.In your char.js file code like below

import { EventBus } from "../EventBus.js";
import { mixins, Line } from "vue-chartjs";
const { reactiveProp } = mixins;
export default {
  extends: Line, 
  mixins: [reactiveProp],
  props: ["options"],
  mounted() {
    const self = this;
    console.log(self);
    this.options.onClick = function (e, tooltipItems) {
      console.log(tooltipItems[0].__ob__); //.logged
      EventBus.$emit("sendIndex", tooltipItems[0]._index);
    };
    this.renderChart(this.chartData, this.options);
  },
};
  1. where you want to access your data in that file like below

    import { EventBus } from "../EventBus.js";
    mounted() {
    
       EventBus.$on('sendIndex', data => {
          console.log(data)
         });
    
    },
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文