@ViewChild('chart')
public chart: ChartComponent;
UpdateChart(){this.chart.series[0].dataSource = test;
this.chart.refresh();}Suppose if there are 10 charts in the same page, the chart ids are generated dynamically, how do we refresh the chart with new data.
| Query | Response | |
| You can use *ngFor for adding more charts dynamically. Please find the code snippet below to achieve this requirement.
In this we have include 10 chart based on the indexes in the data. | |
| Since we are adding the chart dynamically, we don’t have information about the number of chart and we can’t get the chart using viewChild options. Instead we can get the parent element of chart and we can refresh the chart. For example if we add our charts inside a div, we can get the child elements and if its id matches with our scenario, we can get chart instance from ej2_instances variable and we can refresh the chart with new dataSource.
|
| Html: <div *ngFor="let data of [1,2,3, 4, 5, 6, 7, 8, 9, 10]; index as i" [attr.data-index]="i"> <ejs-chart #chart1 id=chart{{i}} height = '250' (axisLabelRender)='axisLabelRender($event)'> </ejs-chart> </div> Component.ts: add() { var elements = this.elements.nativeElement.children for (let i =0; i < elements.length-1; i++) { if (elements[i].children.length > 0 && elements[i].children[0].id == 'chart' + this.input.nativeElement.value) { this.chartName=elements[i].children[0].id; // Store the selected particular index elements[i].children[0].ej2_instances[0].refresh(); break; } } } // public axisLabelRender(args: IAxisLabelRenderEventArgs ): void { // checking chart element id with selected particular index if (args.axis.baseModule.chart.element.id==this.chartName && args.axis.orientation === 'Vertical') { args.text = (parseFloat(args.text)).toExponential(2); } |
| let chart: ChartComponent = elements[i].children[0].ej2_instances[0]; chart.axisLabelRender.subscribe((args: IAxisLabelRenderEventArgs) => { if (args.axis.orientation === 'Vertical') { args.text = (parseFloat(args.text)).toExponential(1); } }); chart.refresh(); |