Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call an angular component method when we click on highchart series

I use angular2-highcharts and I want to call a component method in a local function but I don't know how it could be possible.

Could you help me ? A plunker example : http://plnkr.co/edit/gOLGytp9PZXiXvv2wv1t?p=preview

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic';
import { NgModule, Component }    from '@angular/core';
import { BrowserModule }          from '@angular/platform-browser';
import { ChartModule }            from 'angular2-highcharts';

@Component({
    selector: 'my-app',
    styles: [`
      chart {
        display: block;
      }
    `],
    template: `<chart [options]="options"></chart>`
})
class AppComponent {
    constructor() {
        this.options = {
            chart: {
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false,
                type: 'pie'
            },
            title : { text : 'simple chart' },
            plotOptions: {
                series: {
                    turboThreshold:3000,
                    cursor: 'pointer',
                    point: {
                        events: {
                            click: function() {
                                console.log(this);
                                // I want to call a component method here
                            }
                        }
                    }
                }
            },
            series: [{
                name: 'Brands',
                colorByPoint: true,
                data: [{
                    name: 'Microsoft Internet Explorer',
                    y: 56.33
                }, {
                    name: 'Chrome',
                    y: 24.03,
                    sliced: true,
                    selected: true
                }, {
                    name: 'Firefox',
                    y: 10.38
                }, {
                    name: 'Safari',
                    y: 4.77
                }, {
                    name: 'Opera',
                    y: 0.91
                }, {
                    name: 'Proprietary or Undetectable',
                    y: 0.2
                }]
            }]
        };
    }
    options: Object;

    methodToCall(){
        console.log("Method called");
    }
}

@NgModule({
    imports:      [BrowserModule, ChartModule.forRoot(require('highcharts'))],
    declarations: [AppComponent],
    bootstrap:    [AppComponent]
})
class AppModule { }


platformBrowserDynamic().bootstrapModule(AppModule);
like image 669
al37350 Avatar asked May 18 '17 10:05

al37350


2 Answers

To have access to the informations that the click event returns and also have access to your component methods you can do something like this:

plotOptions: {
            series: {
                turboThreshold:3000,
                cursor: 'pointer',
                point: {
                    events: {
                        click: function(e){
                           const p = e.point
                           this.myComponentMethod(p.category,p.series.name);
                        }.bind(this)
                    }
                }
            }
        },

I hope this will help.

like image 169
Marcel Jr. Samson Morasse Avatar answered Oct 21 '22 03:10

Marcel Jr. Samson Morasse


You just need to replace the anonymous function by an arrow function or bind it to the component:

point: {
    events: {
        click: () => {
            console.log(this);
            // I want to call a component method here
        }
    }
}

or

point: {
    events: {
        click: (function(){
            console.log(this);
            // I want to call a component method here
        }).bind(this)
    }
}

or even :

point: {
    events: {
        click: this.myComponentMethod.bind(this)
    }
}
like image 35
n00dl3 Avatar answered Oct 21 '22 03:10

n00dl3