ng2-charts 的自定义空屏幕
Custom empty screen for ng2-charts
我正在研究 Line Chart in ng2-charts。如果有数据,我会尝试显示图表,如果没有,我想显示 No Data Available
而不是空图表。
HTML
<div style="display: block;" *ngIf="lineChartData">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
TS
public lineChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
];
public lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
您可以使用 *ngIf
's else
block:
<div style="display: block;" *ngIf="lineChartData; else elseBlock">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
<ng-template #elseBlock>No Data Available.</ng-template>
如果 lineChartData
可以只是空的 ([]
) 并且您希望在这种情况下也隐藏图表,您可以使用:
<div style="display: block;" *ngIf="lineChartData && lineChartData.length; else elseBlock">
And/or 如果你也想保护 data
变量,请执行:
<div style="display: block;" *ngIf="lineChartData && lineChartData.length && lineChartData[0].data.length; else elseBlock">
你必须像
这样检查数据变量长度
html
<div style="display: block;" *ngIf="lineChartData[0]['data'].length">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
<div *ngIf="lineChartData[0]['data'].length === 0 ">
No Data Available
</div>
ts
export class AppComponent {
public lineChartData: ChartDataSets[] = [
{ data: [], label: 'Series A' },
];
}
我正在研究 Line Chart in ng2-charts。如果有数据,我会尝试显示图表,如果没有,我想显示 No Data Available
而不是空图表。
HTML
<div style="display: block;" *ngIf="lineChartData">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
TS
public lineChartData: ChartDataSets[] = [
{ data: [65, 59, 80, 81, 56, 55, 40], label: 'Series A' },
];
public lineChartLabels: Label[] = ['January', 'February', 'March', 'April', 'May', 'June', 'July'];
您可以使用 *ngIf
's else
block:
<div style="display: block;" *ngIf="lineChartData; else elseBlock">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
<ng-template #elseBlock>No Data Available.</ng-template>
如果 lineChartData
可以只是空的 ([]
) 并且您希望在这种情况下也隐藏图表,您可以使用:
<div style="display: block;" *ngIf="lineChartData && lineChartData.length; else elseBlock">
And/or 如果你也想保护 data
变量,请执行:
<div style="display: block;" *ngIf="lineChartData && lineChartData.length && lineChartData[0].data.length; else elseBlock">
你必须像
这样检查数据变量长度html
<div style="display: block;" *ngIf="lineChartData[0]['data'].length">
<canvas baseChart width="400" height="400"
[datasets]="lineChartData"
[labels]="lineChartLabels"
[options]="lineChartOptions"
[colors]="lineChartColors"
[legend]="lineChartLegend"
[chartType]="lineChartType"
[plugins]="lineChartPlugins">
</canvas>
</div>
<div *ngIf="lineChartData[0]['data'].length === 0 ">
No Data Available
</div>
ts
export class AppComponent {
public lineChartData: ChartDataSets[] = [
{ data: [], label: 'Series A' },
];
}