0
votes

I wrote a method to sync the chartCursors of multiple charts in amCharts. For my approach to work, nested forEach loops are necessary. I would like to resolve this, but I just can't do it..

Here is the simplified version of my method:

  syncCursor(charts: any[]) {
    const allCharts: any[] = [];
    let category: any = '';

    charts.forEach((c, index) => {
      allCharts.push(c);
    });

    allCharts.forEach((c, index) => {
      c.addListener('changed', (event) => {
        if (event.chart) {
          category = event?.chart?.categoryField];
          allCharts.forEach((chart) => {
            chart.chartCursor.showCursorAt(category);
          });
        }
      });
    });
  }

How can I write this method cleaner or outsource parts of it without losing functionality?

You can "outsource" the event handler, if you mean moving it to a higher scope - Bergi
Btw, const allCharts = charts.slice() or even just const allCharts = charts? - Bergi