Angular 角度谷歌图表:x属性的意外值

Angular 角度谷歌图表:x属性的意外值,angular,angular-material,google-visualization,nan,Angular,Angular Material,Google Visualization,Nan,我有一个Angular 6服务传递数据来填充Google材质图表 app.component.ts export class CustomersComponent implements OnInit { customers: Customer[]; charts: Array<{ title: string, type: string, data: Array<Array<string | number | {}>>,

我有一个Angular 6服务传递数据来填充Google材质图表

app.component.ts

  export class CustomersComponent implements OnInit {

  customers: Customer[];

  charts: Array<{
    title: string,
    type: string,
    data: Array<Array<string | number | {}>>,
    roles: Array<{ type: string, role: string, index?: number }>,
    columnNames?: Array<string>,
    options?: {}
   }> = [];

  @ViewChild('chart')
  chart: GoogleChartComponent;

  constructor(private customerService: CustomerService) {}

ngOnInit(): void {
 this.getCustomers();
 this.customerService.getCustomers()
 .subscribe(res => {
  //  console.log(res)
   let firstname = res.map(res => res.firstname);
   console.log(firstname)
   let age = res.map(res => res.age);
   console.log(age)

   this.charts.push({
    title: 'Customer Demographics',
    type: 'Bar',
    columnNames: ['Customer', 'Age'],
    roles: [],
    data: [
      [firstname, age],
    ],
    options: {
      chart: {
        title: 'Customer Demographics',
        subtitle: 'Sales, Expenses, and Profit: 2014-2017'
      },
      bars: 'vertical' // Required for Material Bar Charts.
    }
  });
 })}
导出类CustomerComponent实现OnInit{
客户:客户[];

图表:数组数据的格式似乎不正确

而不是两个具有单独列值的数组

[ "Joe", "Peter", "Lauren"...
[ 36, 18, 31...
数据中的每一行都应该是一个数组,每个列的值

[ "Joe", 36 ],
[ "Peter", 18 ],
[ "Lauren", 31 ],
您应该能够通过组合
map
语句中的值进行更正

替换

let firstname = res.map(res => res.firstname);
let age = res.map(res => res.age);

let data = res.map(res => [res.firstname, res.age]);
然后将数据添加到图表对象

this.charts.push({
  title: 'Customer Demographics',
  type: 'Bar',
  columnNames: ['Customer', 'Age'],
  roles: [],
  data: data,  // <-- add data here
  options: {
    chart: {
      title: 'Customer Demographics',
      subtitle: 'Sales, Expenses, and Profit: 2014-2017'
    },
    bars: 'vertical' // Required for Material Bar Charts.
  }
this.charts.push({
标题:“客户人口统计”,
类型:'Bar',
列名称:[“客户”、“年龄”],
角色:[],

数据:数据,//数据的格式似乎不正确

而不是两个具有单独列值的数组

[ "Joe", "Peter", "Lauren"...
[ 36, 18, 31...
数据中的每一行都应该是一个数组,每个列的值

[ "Joe", 36 ],
[ "Peter", 18 ],
[ "Lauren", 31 ],
您应该能够通过组合
map
语句中的值进行更正

替换

let firstname = res.map(res => res.firstname);
let age = res.map(res => res.age);

let data = res.map(res => [res.firstname, res.age]);
然后将数据添加到图表对象

this.charts.push({
  title: 'Customer Demographics',
  type: 'Bar',
  columnNames: ['Customer', 'Age'],
  roles: [],
  data: data,  // <-- add data here
  options: {
    chart: {
      title: 'Customer Demographics',
      subtitle: 'Sales, Expenses, and Profit: 2014-2017'
    },
    bars: 'vertical' // Required for Material Bar Charts.
  }
this.charts.push({
标题:“客户人口统计”,
类型:'Bar',
列名称:[“客户”、“年龄”],
角色:[],

data:data,//是的!这正是问题所在。非常感谢!是的!这正是问题所在。非常感谢!