0

How to grab angular component on onClick function of a ChartJs function? is there a way? as this is the Chart, self the window...

enter image description here

My chart is added like that into the some.component.ts

 @ViewChild(Chart) mainChart: Chart // declaration on public variables on component.ts

  // construction of chart on API call result
  this.mainChart = new Chart(MeasureUnitMonthlyComponent.CHART_ID, {
    type: 'bar',
    data: {
      labels: aChartLabelsArray,
      datasets: aDatasetsArray
    },
    options: {
      onClick: this.onChartClicked,
      legend: {
        display: true
      },
    ....
Pipo
  • 4,653
  • 38
  • 47

1 Answers1

0

If you write, onClick: () => {}, this will be the component/class when writing an arrow function.

If you write, onClick: function() {}, this will be the chart object.

If you want access to both the chart object and component/class, I would use the utility of thisAsThat linked below and explained as well.

TypeScript: How to use both fat arrow and this?

For onClick: you can use the utility of thisAsThat that's present in the answer where this will be the class/component and that will be the chart object.

I would also consider using ng2-charts which is a wrapper around chart.js where it has a (chartClicked) event.

Chart.js & Angular 2 - ng2-charts Custom on Click Event

AliF50
  • 16,947
  • 1
  • 21
  • 37