angular - 从来自后端的 Json 填充 Angular 中的谷歌饼图
问题描述
我在正确填充谷歌饼图时遇到问题。
我正在构建一个视图。不要考虑表 和我在做的进步。
但是,我的 onInit 方法从 spring boot 获取一个 json 并尝试设置定义为employees的属性。在方法结束时检查它:
import { Component } from "@angular/core";
import { FormBuilder, FormGroup , Validators} from '@angular/forms';
import { Employee } from "src/app/ab-front/employee.interface";
import { HttpClient } from "@angular/common/http";
import { EmployeesService } from "src/app/ab-front/employees/employees.service";
import { OnInit } from "@angular/core";
import { FormControl } from "@angular/forms";
import { Input } from "@angular/core";
@Component({
selector: 'ab-employee',
templateUrl: 'employee.component.html',
styleUrls: ['employee.component.css']
})
export class EmployeeComponent implements OnInit {
form : FormGroup;
employees: Employee [] = [];
@Input()
pieChartData = {
chartType: 'PieChart',
options: {'title': 'Dados do Pessoal',
pieHole:0.4,
chartArea: {
left:100,
top:50,
width: 400,
height: 300}
},
dataTable: this.populaChart()
};
populaChart(){
let titulo : string [] = ['Nome','Participacao'];
let linha = [];
for(let i = 0 ; i < this.employees.length ; i++){
linha [i] = [this.employees[i].nome + ' ' + this.employees[i].sobrenome ,
this.employees[i].participacao];
if(i < this.employees.length){
linha [i] = linha [i] + ',';
}
}
console.log(this.employees);
return [
titulo,
['Hugo Silva', 20],
['Eliza Souza', 20],
['Anderson Santos', 40]
];
}
constructor(private employeeService: EmployeesService,
private formBuilder : FormBuilder){ }
ngOnInit(){
this.form = this.formBuilder.group({
nome: ['',
[
Validators.required,
Validators.pattern('([A-Z])([a-z])+'),
Validators.minLength(2)
],
],
sobrenome: ['',
[
Validators.required,
Validators.pattern('([A-Z])([a-z])+'),
Validators.minLength(2)
]
],
participacao: ['',
[
Validators.required,
Validators.pattern('[0-9]+'),
Validators.minLength(1)
]
]
});
this.employeeService
.listEmployees()
.subscribe(employees => {
this.employees = employees;
console.log(this.employees);
});
}
}
上面,console.log 打印一个包含 2 个元素的 json。然而,我尝试在 populaChart 方法上做同样的事情,在那里, 我得到了一个空变量。
有人知道会发生什么吗?而且,如何将来自后端和表单的 json 中的数据设置为饼图?
解决方案
当下面的代码编译成 JavaScript 时,pieChartData = ...
被放置在构造函数中,它在之前运行ngOnInit
:
export class EmployeeComponent implements OnInit {
// Runs in the constructor
pieChartData = {
chartType: 'PieChart',
options: {'title': 'Dados do Pessoal',
pieHole:0.4,
chartArea: {
left:100,
top:50,
width: 400,
height: 300}
},
dataTable: this.populaChart()
};
ngOnInit() {
// Runs after the constructor
// Get employees
}
}
因此,当它运行时,您还没有任何员工。从 API收到后,您应该pieChartData
在内部创建。或者至少初始化为一个空数组,稍后当你有员工时更新它。ngOnInit
employees
dataTable
推荐阅读
- php - 我不确定为什么我的代码没有存储 XSS
- android - “字符串”类型不是“布尔”类型的子类型
- django - Django 请求将 JSON 返回为“str”而不是“dict”
- sql - 在蜂巢中将日期转换为午夜
- tensorflow-federated - TFF : 测试精度波动
- java - Java:运行 .exe 程序并获取 GUI 输出的 exery 像素
- ios - 适用于 ionic iOS 的 Azure Devops Pipelines - 预配配置文件错误。在签名和功能编辑器中选择配置文件
- powershell - 使用 tfs api 连接到不同用户的 tfs
- algorithm - 计算输出向量,对于每个元素 i,output[i] 是输入向量除 i 之外的所有值的乘积
- javascript - 获取不带小数的整数