gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:46:12 25 4
gpt4 key购买 nike

我正在使用 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 },
]
})

最佳答案

要实现解决方案,请像下面这样更新您的代码。

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

关于angular - 如何为 ng2-charts 条形图设置静态标签?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529107/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com