如何为ng2-chart条形图设置静态标签?

问题描述 投票:2回答:1

我正在使用ng2-charts。我有一个数据数组。我在x轴上打印日期值,在y轴上打印标记。问题是,如果我仅具有星期二和星期三的数据,则仅显示两个,但我希望像静态x轴(周日至周六)。如下图所示

我目前拥有的:

Screenshot of 2 bars (a blue on Monday and a yellow on Tuesday) on a bar chart

我想要得到的:

Screenshot of 2 bars (a blue on Tuesday and a yellow on Wednesday) on a bar chart

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 },
 ]
})
angular typescript chart.js ng2-charts
1个回答
1
投票

要获得解决方案,请按如下所示更新您的代码。

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'},
  ]
}

这里Working example

© www.soinside.com 2019 - 2024. All rights reserved.