MOBILE-3320 chart: Fix rendering null legend items

main
Noel De Martin 2021-06-09 12:16:06 +02:00
parent 69d59cd2ab
commit 00dcac0b9c
2 changed files with 13 additions and 1 deletions

View File

@ -57,6 +57,7 @@ export class CoreChartComponent implements OnDestroy, OnInit, OnChanges {
@ViewChild('canvas') canvas?: ElementRef<HTMLCanvasElement>; @ViewChild('canvas') canvas?: ElementRef<HTMLCanvasElement>;
chart?: ChartWithLegend; chart?: ChartWithLegend;
legendItems: ChartLegendLabelItem[] = [];
/** /**
* @inheritdoc * @inheritdoc
@ -105,6 +106,8 @@ export class CoreChartComponent implements OnDestroy, OnInit, OnChanges {
}, },
options: { legend }, options: { legend },
}); });
this.updateLegendItems();
} }
/** /**
@ -126,6 +129,8 @@ export class CoreChartComponent implements OnDestroy, OnInit, OnChanges {
}; };
this.chart.data.labels = this.labels; this.chart.data.labels = this.labels;
this.chart.update(); this.chart.update();
this.updateLegendItems();
} }
/** /**
@ -179,6 +184,13 @@ export class CoreChartComponent implements OnDestroy, OnInit, OnChanges {
} }
} }
/**
* Recompute legendItems property.
*/
protected updateLegendItems(): void {
this.legendItems = (this.chart?.legend?.legendItems ?? []).filter(item => !!item);
}
} }
// For some reason the legend property isn't defined in TS, define it ourselves. // For some reason the legend property isn't defined in TS, define it ourselves.

View File

@ -1,7 +1,7 @@
<canvas #canvas [attr.height]="height"></canvas> <canvas #canvas [attr.height]="height"></canvas>
<ion-list *ngIf="chart"> <ion-list *ngIf="chart">
<ion-item *ngFor="let data of chart.legend!.legendItems"> <ion-item *ngFor="let data of legendItems">
<ion-icon name="fas-square" slot="start" [style.color]="data.fillStyle" aria-hidden="true"></ion-icon> <ion-icon name="fas-square" slot="start" [style.color]="data.fillStyle" aria-hidden="true"></ion-icon>
<ion-label>{{data.text}}</ion-label> <ion-label>{{data.text}}</ion-label>
</ion-item> </ion-item>