1

I am trying to update the same chart with different data on tab change. So my parent component has the child component app-chart-metric-view in this form. I have included tab event detection for the mat tab group.

AdminConsolecomponent.html

<mat-tab-group mat-stretch-tabs class="mat-elevation-z4" (selectedTabChange)="tabChanged($event)">
  <mat-tab label="Enable Plugin">
    <div class="button-row">
      <button mat-raised-button class="activation-buttons" (click)="openAddPluginDialog()">Add
        plugin
      </button>
    </div>
 </mat-tab>

  <mat-tab label="Chart1">
    <app-chart-metric-view></app-chart-metric-view>
  </mat-tab>

  <mat-tab label="Chart2">
    <app-chart-metric-view></app-chart-metric-view>
  </mat-tab>


</mat-tab-group>

I detect the tab tab change & once this is triggered I call the function in child component (with the help of @viewChild) which makes request to backend, gets the data sets it and displays it.

Also, I am sending the label of the tab to backend and based on it the data I receive changes. The problem is chart for the first tab is being loaded but not for subsequent tabs.

AdminConsolecomponent.ts

import { ChartMetricViewComponent } from '../chart-metric-view/chart-metric-view.component';

    export class AdminConsoleComponent {

      @ViewChild( ChartMetricViewComponent ) child: ChartMetricViewComponent ; 

      constructor(//some components) { }
      ngOnInit() {
      //some functions
      }

      tabChanged = (tabChangeEvent: MatTabChangeEvent): void => {
        if(tabChangeEvent.index !=0) {
          this.child.shfk(tabChangeEvent.tab.textLabel);

      }

}

chart-metric-view-component.ts

export class ChartMetricViewComponent implements OnInit {

  metricItems: MetricItem[] = [];
  constructor(public utility: ControltowerUtilityService, public metricService: MetricDataService, private chartService: ChartDataService) { }

  @ViewChild('chartTarget') chartTarget: ElementRef;
  @ViewChild('chartTarget2') chartTarget2: ElementRef;

  chart: Highcharts.ChartObject;
  chart2: Highcharts.ChartObject

  public shfk(textLabel) {

    this.chartService.getPluginMetrics(textLabel).subscribe(data => {

      this.metricItems = data;


      const options: Highcharts.Options = {
        chart: {
          type: 'column'
        },
        title: {
          text: 'Stacked Consumption chart'
        },
        xAxis: {
          categories: ['Jan', 'Feb', 'March', 'April', 'May', 'June', 'July', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
        },
        yAxis: {
          min: 0,
          title: {
            text: 'Bitbucket Consumption in TBs'
          },
          stackLabels: {
            enabled: true,
            style: {
              fontWeight: 'bold',
            }
          }
        },
        legend: {
          align: 'right',
          x: -30,
          verticalAlign: 'top',
          y: 25,
          floating: true,
          borderColor: '#CCC',
          borderWidth: 1,
          shadow: false
        },
        tooltip: {
          headerFormat: '<b>{point.x}</b><br/>',
          pointFormat: '{series.name}: {point.y}<br/>Total: {point.stackTotal}'
        },
        plotOptions: {
          column: {
            stacking: 'normal',
            dataLabels: {
              enabled: true,
            }
          }
        },
        series: [{
          name: this.metricItems[0].name,
          data: JSON.parse(" [ "+ this.metricItems[0].value+"]")//this.A //JSON.parse("["+this.metricItems[0].value+"]")//[5, 3, 4, 7, 2, 6] {this.A}
        }, {
          name: this.metricItems[1].name,
          data: JSON.parse("["+this.metricItems[1].value+"]")
        }, {
          name: this.metricItems[2].name,
          data: JSON.parse("["+this.metricItems[2].value+"]")
        }, {
          name: this.metricItems[3].name,
          data: JSON.parse("["+this.metricItems[3].value+"]")
        }, {
          name: this.metricItems[4].name,
          data: JSON.parse("["+this.metricItems[4].value+"]")
        }]

      };

      this.chart = chart(this.chartTarget.nativeElement, options);

    });
}

On change of tab I have verified that request is being made and I am receiving the correct response but cant update my chart. Can someone please help ? Thanks

Have found similar questions but still cant figure out

highcharts not loading in tabs

HIghcharts & Bootstrap: Chart on Tab 2 is not appearing. Though First chart is working fine

Shaurya
  • 313
  • 1
  • 12
  • Could you please prepare a simplified online example of your app with sample data presenting this issue (codesandbox for example)? Are you using highcharts official wrapper for Angular: https://github.com/highcharts/highcharts-angular. – Wojciech Chmiel Nov 19 '18 at 09:21
  • Hi..did you manage to solve this issue? I have a similar issue which I am unable to fix. – FE_Addict Mar 13 '19 at 04:02
  • @FE_Addict nope.... – Shaurya Mar 14 '19 at 09:58

1 Answers1

1

The solution of this problem is by using Lazy loading concept, which is supported on Angular Material Tab.

Just declare your component highchart in a ng-template with the matTabContentattribute like this :

<mat-tab label="Chart1">
 <ng-template matTabContent>
   <app-chart-metric-view></app-chart-metric-view>
 </ng-template>
</mat-tab>

You can see the documentation of MatTab Lazy loading here

I don't know if you already make it or not, but it might help other people who had similiar porblem anyway

alramdein
  • 810
  • 2
  • 12
  • 26