I'm trying to create a pie chart on an element using the directive in Angular 5. I created a directive like:
@Directive({
selector: '[pieChart]'
})
export class PieChartDirective implements AfterViewInit {
@Input() pieChart: any;
@Input() pieChartElementId: string;
@Input() pieChartData: any;
constructor(elem: ElementRef, renderer: Renderer2) {}
ngAfterViewInit() {
this.generatePieChart(this.pieChartElementId);
}
generatePieChart(elemId) {
var values = [1, 2, 3];
$('#' + elemId).sparkline(values, {
type: "pie",
tooltipFormat: '{{offset:offset}} ({{percent.1}}%)',
tooltipValueLookups: {
'offset': {
0: 'First',
1: 'Second',
2: 'Third'
}
},
});
}
}
In the template I'm calling directive like:
<div class="pieChartDiv" [attr.id]="'classroom.field1' + '_'+ i" [pieChart]="'classroom.field1' + '_'+ i" [pieChartElementId]="'classroom.field1' + '_'+ i"></div>
I'm creating id dynamically and want to draw the pie chart on each id. But unfortunately, I'm not getting the length of element in directive when using console.log($("#"+this.pieChartElementId).length) in the directive file. Due to that charts are not working because they are not getting the element. Could anyone please tell me why this happening.
Thanks in advance.
$('#' + elemId)try$(this.elem.nativeElement). For this to work you will also have to add an access modifier to theelemparameter for your constructor to automatically make it a property of your class e.g.constructor(private elem:ElementRef, renderer:Renderer2) {}.