如何使用 angular 组件调用 Apexcharts Events 中的函数?

How to call a function inside the Apexcharts Events with angular component?

我正在使用 apexcharts angular 库来以图形方式表示我的数据。我想在点击栏时触发另一个功能。这是我的条形图配置。

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 是我想在点击图表中的任意柱时触发的函数。

有人可以回复一下如何调用那个函数吗?

您可以传入一个箭头函数作为回调以保留 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);
    }
  }
}

您可以在回调 here.

中找到有关 this 关键字含义的更多信息