Ngx-Chart不显示数据
Ngx-Chart does not display data
我目前在我的 angular 项目中使用 Ngx-Charts。我的图表数据来自 Node.js 网络服务,该服务提供以下响应:
[{name: "ALB", value: 2188679.1133568},{name: "OFF", value: 1440804.2389714},{name: "SL1", value: 971553.8710496},{name: "HDS", value: 793200.8299244},{name: "YAC", value: 183228.7398208},{name: "DWK", value: 104339.702865},{name: "SS1", value: 54080.201656},{name: "SIM", value: 44772.373923},{name: "ELS", value: 755.211371}]
我试图将其解析为我的 component.ts 文件中的一个数组,然后将该数组传递到我的 component.html 文件中的图表 canvas 中。然而,尽管我能够在控制台上显示响应,但我无法在图表上显示数据。我所看到的只是带有 x 轴和 y 轴的空白图表,但上面没有数据。我不明白我哪里错了。
我的 component.ts 文件:
export class DashboardComponent implements OnInit{
isBrowser: boolean;
verticalBarOptions = {
showXAxis: true,
showYAxis: true,
gradient: false,
showLegend: true,
showGridLines: true,
barPadding: 25,
showXAxisLabel: false,
xAxisLabel: 'Country',
showYAxisLabel: true,
yAxisLabel: 'Sales'
};
locWData: any[];
colorScheme = {
domain: ['#0000FF', '#154360', '#17202A', '#21618C', '#2E86C1', '#85C1E9', '#2E86C1', '#34495E', '#21618C', '#154360']
};
constructor(@Inject(PLATFORM_ID) private platformId: object, private service: ReportsService) {
this.locWData = [];
this.isBrowser = isPlatformBrowser(platformId);
}
ngOnInit(): void {
this.service.getAllLocWiseStock().subscribe((response : any) => {
//console.log(response.recordset);
for (let i = 0; i < response.recordset.length; i++) {
this.locWData.push({name: response.recordset[i].LOCATION_ID, value: response.recordset[i].AMOUNT})
}
console.log(this.locWData);
});
}
}
和component.html文件:
<div class="col-12 chart-card”>
<div>
<div class="chart-card-header">
<span>Location-wise Sales</span>
</div>
<div class="chart-card-body bar-vertical">
<ngx-charts-bar-vertical *ngIf="isBrowser"
[scheme]="colorScheme"
[results]="locWData"
[gradient]="verticalBarOptions.gradient"
[xAxis]="verticalBarOptions.showXAxis"
[yAxis]="verticalBarOptions.showYAxis"
[legend]="verticalBarOptions.showLegend"
[showXAxisLabel]="verticalBarOptions.showXAxisLabel"
[showYAxisLabel]="verticalBarOptions.showYAxisLabel"
[xAxisLabel]="verticalBarOptions.xAxisLabel"
[yAxisLabel]=“verticalBarOptions.yAxisLabel"
[barPadding]="verticalBarOptions.barPadding"
[showGridLines]="verticalBarOptions.showGridLines">
</ngx-charts-bar-vertical>
</div>
</div>
</div>
<div class="col-12 chart-card">
<div>
<div class="chart-card-header">
<span>Location-wise Sales Distribution</span>
</div>
<div class="chart-card-body pie-grid">
<ngx-charts-advanced-pie-chart *ngIf="isBrowser"
[label]="'Points'"
[scheme]="colorScheme"
[results]="locWData">
</ngx-charts-advanced-pie-chart>
</div>
</div>
</div>
在这个 stackblitz 中,它似乎填满了图表。即使您更改了 advance-pie-chart 的数据:
https://stackblitz.com/edit/angular-ivy-em7rez?file=src%2Fapp%2Fdashboard%2Fdashboard.component.ts
我目前在我的 angular 项目中使用 Ngx-Charts。我的图表数据来自 Node.js 网络服务,该服务提供以下响应:
[{name: "ALB", value: 2188679.1133568},{name: "OFF", value: 1440804.2389714},{name: "SL1", value: 971553.8710496},{name: "HDS", value: 793200.8299244},{name: "YAC", value: 183228.7398208},{name: "DWK", value: 104339.702865},{name: "SS1", value: 54080.201656},{name: "SIM", value: 44772.373923},{name: "ELS", value: 755.211371}]
我试图将其解析为我的 component.ts 文件中的一个数组,然后将该数组传递到我的 component.html 文件中的图表 canvas 中。然而,尽管我能够在控制台上显示响应,但我无法在图表上显示数据。我所看到的只是带有 x 轴和 y 轴的空白图表,但上面没有数据。我不明白我哪里错了。
我的 component.ts 文件:
export class DashboardComponent implements OnInit{
isBrowser: boolean;
verticalBarOptions = {
showXAxis: true,
showYAxis: true,
gradient: false,
showLegend: true,
showGridLines: true,
barPadding: 25,
showXAxisLabel: false,
xAxisLabel: 'Country',
showYAxisLabel: true,
yAxisLabel: 'Sales'
};
locWData: any[];
colorScheme = {
domain: ['#0000FF', '#154360', '#17202A', '#21618C', '#2E86C1', '#85C1E9', '#2E86C1', '#34495E', '#21618C', '#154360']
};
constructor(@Inject(PLATFORM_ID) private platformId: object, private service: ReportsService) {
this.locWData = [];
this.isBrowser = isPlatformBrowser(platformId);
}
ngOnInit(): void {
this.service.getAllLocWiseStock().subscribe((response : any) => {
//console.log(response.recordset);
for (let i = 0; i < response.recordset.length; i++) {
this.locWData.push({name: response.recordset[i].LOCATION_ID, value: response.recordset[i].AMOUNT})
}
console.log(this.locWData);
});
}
}
和component.html文件:
<div class="col-12 chart-card”>
<div>
<div class="chart-card-header">
<span>Location-wise Sales</span>
</div>
<div class="chart-card-body bar-vertical">
<ngx-charts-bar-vertical *ngIf="isBrowser"
[scheme]="colorScheme"
[results]="locWData"
[gradient]="verticalBarOptions.gradient"
[xAxis]="verticalBarOptions.showXAxis"
[yAxis]="verticalBarOptions.showYAxis"
[legend]="verticalBarOptions.showLegend"
[showXAxisLabel]="verticalBarOptions.showXAxisLabel"
[showYAxisLabel]="verticalBarOptions.showYAxisLabel"
[xAxisLabel]="verticalBarOptions.xAxisLabel"
[yAxisLabel]=“verticalBarOptions.yAxisLabel"
[barPadding]="verticalBarOptions.barPadding"
[showGridLines]="verticalBarOptions.showGridLines">
</ngx-charts-bar-vertical>
</div>
</div>
</div>
<div class="col-12 chart-card">
<div>
<div class="chart-card-header">
<span>Location-wise Sales Distribution</span>
</div>
<div class="chart-card-body pie-grid">
<ngx-charts-advanced-pie-chart *ngIf="isBrowser"
[label]="'Points'"
[scheme]="colorScheme"
[results]="locWData">
</ngx-charts-advanced-pie-chart>
</div>
</div>
</div>
在这个 stackblitz 中,它似乎填满了图表。即使您更改了 advance-pie-chart 的数据:
https://stackblitz.com/edit/angular-ivy-em7rez?file=src%2Fapp%2Fdashboard%2Fdashboard.component.ts