7

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);
n00dl3
  • 21,213
  • 7
  • 66
  • 76
al37350
  • 139
  • 2
  • 8

4 Answers4

13

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.

9

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)
    }
}
n00dl3
  • 21,213
  • 7
  • 66
  • 76
  • Thanks, i'm so stupid ;) And for you what is the best practice ? – al37350 May 18 '17 at 10:35
  • However, with this solution I lost informations than click returns wich in need in my component param method. – al37350 May 18 '17 at 12:00
  • Why not using highchairs components instead of creating it manually ? – n00dl3 May 18 '17 at 12:08
  • I have the same problem: I want to use information that Highcharts return on click.. but then I need to use angular services with the returned data! Still no solution to this? – Fisnik Tahiri Jun 27 '17 at 16:46
0

I know i am late here but his might help someone. The angular2-highchart package does provide the access to series event.

<chart [options]="options">
  <series (click)="methodToCall($event)"
  </series>
</chart>
 <p>Series-Clicked={{data}}</p>

JS code.

methodToCall(e){
    console.log("Method called");
   this.data = e.originalEvent.point.name
}

Here is the updated Plunk.

Shah
  • 86
  • 1
  • 4
0
**/*----- component method to generate Pie-chart from high chart Library  -----*/**
drawPieChart() {
    HighCharts.chart("pie_chart", {
      accessibility: { enabled: false },
      chart: {
        type: 'pie',
        plotBackgroundColor: '#FFF',
        plotShadow: true
      },
      title: {
        text: 'My Title'
      },
      tooltip: {
        // pointFormat: '{point.y}<br/><b>{point.percentage:.1f}%</b>'
      },
      plotOptions: {
        pie: {
          allowPointSelect: true,
          cursor: 'pointer',
          dataLabels: {
            enabled: true,
            //format: '<b>{point.name}</b>: {point.y} '*//change .name & .y according to your data-set*
          },
          showInLegend: true,
          events: {
            click: 
              this.myCustomMethod.bind(this)
          },
        },//pie
      },
      series: [{
        name: 'TA',
        colorByPoint: true,
        type: 'pie',
        data: this.data,*// <<< here you need to put your JSON variable or Json object*
      }]
    });
    

  }

**/* ---- on click event of pie myCustomMethos() 'll get call ---- */**
myCustomMethod(t:any) {
    console.log(t.point.name)
}