首页 > 解决方案 > 如何使用角度组件调用 Apexcharts 事件中的函数?

问题描述

我正在使用 apexcharts 角度库来图形表示我的数据。当我单击该栏时,我想触发另一个功能。这是我对条形图的配置。

this.chartOptions = {
      series: [
        {
          name: "basic",
          data:[25,30,35,40]
        }
      ],
      chart: {
        type: "bar",
        height: 350,
        
        events: {
          click(event, chartContext, config) {
            console.log(config.seriesIndex);
            console.log(config.dataPointIndex);
            //this.getStates(config.dataPointIndex);
            var test: number = config.dataPointIndex;
            console.log(config.globals.labels[test]);
            this.getStates(test);
          }
        }
      },
      plotOptions: {
        bar: {
          horizontal: true
        }
      },
      dataLabels: {
        enabled: false
      },
      xaxis: {
        categories: ["India","Pakistan","USA","England"]
      }
    }

在这里,getStates 是我想要在单击图表中的任何条时触发的函数。

有人可以回复如何调用该功能吗?

标签: angularapexcharts

解决方案


您可以传入箭头函数作为回调来保留this关键字的含义。

chart: {
  type: "bar",
  height: 350,
  events: {
    click: (event: any, chartContext: any, config: any) => {
      this.getStates(config.dataPointIndex);

      console.log(config.seriesIndex);
      console.log(config.dataPointIndex);
      var test: number = config.dataPointIndex;
      console.log(config.globals.labels[test]);
      this.getStates(test);
    }
  }
}

您可以在此处this找到有关回调中关键字含义的更多信息。


推荐阅读