我正在使用ng2-charts。我有一个数据数组。我在x轴上打印日期值,在y轴上打印标记。问题是,如果我仅具有星期二和星期三的数据,则仅显示两个,但我希望像静态x轴(周日至周六)。如下图所示
我目前拥有的:
我想要得到的:
HTML
<div style="display: block" *ngIf="barChartData">
<canvas baseChart width="400" height="180" style="margin-left:5%;margin-
top: 5%;" [datasets]="barChartData"
[labels]="barChartLabels" [options]="barChartOptions"
[legend]="barChartLegend" [chartType]="barChartType"
(chartHover)="chartHovered($event)" [colors]="chartColors"
(chartClick)="chartClicked($event)"></canvas>
</div>
TS
this.barChartLabels = this.days; //Have week days like[tue,wed]
setTimeout(() => {
this.barChartData = [
{ data: this.marks },
]
})
要获得解决方案,请按如下所示更新您的代码。
HTML
<div>
<div style="display: block">
<canvas baseChart
[datasets]="barChartData"
[labels]="barChartLabels"
[options]="barChartOptions"
[legend]="barChartLegend"
[chartType]="barChartType"
(chartHover)="chartHovered($event)"
(chartClick)="chartClicked($event)"></canvas>
</div>
<button (click)="randomize()">Update</button>
</div>
TS
export class AppComponent {
public barChartOptions:any = {
scaleShowVerticalLines: false,
responsive: true
};
public barChartLabels:string[] = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
public barChartType:string = 'bar';
public barChartLegend:boolean = true;
public barChartData:any[] = [
{data: [0, 0, 80, 81, 0, 0, 0], label: 'Graph 1'},
]
}