Angular 9 Chart.js折线图,基于日期的多行

r1wp621o  于 2023-06-29  发布在  Chart.js
关注(0)|答案(1)|浏览(204)

这个问题已经问过了,但不能解决我的问题。
我在Angular 9应用程序中实现了Chart.js。没有预期的图表值。
我有一个API响应,我想让lineChartLabels如下所述stackblitz Demo.
这是我的API响应:

let data = [
    {operatorName: 'OSEN+', date: '05-04-2023', totalCount: 1},
    {operatorName: 'OSN+', date: '12-04-2023', totalCount: 7},
    {operatorName: 'MBC', date: '01-06-2023', totalCount: 1},
    {operatorName: 'OSEN+', date: '02-06-2023', totalCount: 1},
    {operatorName: 'MBC', date: '09-06-2023', totalCount: 1},
    {operatorName: 'MBC', date: '16-06-2023', totalCount: 1},
    {operatorName: 'MBC', date: '23-06-2023', totalCount: 15}
];

我想用API响应设置lineChartLabels
预期数据集:

this.lineChartData = [
      { data: [1, 7, 0, 1, 0, 0, 0], label: 'OSEN+' },
      { data: [0, 0, 1, 0, 1, 1, 15], label: 'MBC' },
    ];
    
this.lineChartLabels = ['05-04-2023', '12-04-2023', '01-06-2023', '02-06-2023', '09-06-2023','16-06-2023', '23-06-2023' ];

这里,如果operatorName不存在于lineChartLabels中,则设置为值0
代码:

let operators = [...new Set(data.map((x) => x.operatorName))];
      
      let subLabelDatasets = operators.map((x) => {
        let datasets = [];
      
        for (let operator of operators) {
          datasets.push(
            data.find((y) => y.operatorName == operator && y.date == x)?.totalCount || 0
          );
        }
      
        return {
          label: x,
          data: datasets,
        };
      });
console.log(data)
      console.log(subLabelDatasets)

预期结果

w51jfk4q

w51jfk4q1#

注意事项

(1)为了让我的回答更有启发性,我更改了这个变量的名称:

  • operatorsuniqueOperators
  • xuniqueOperator

(2)我将一些let更改为const,因为它从未重新分配
(3)更新

  • 在同一日期处理多个操作员
  • 添加完整代码

代码

你需要把你的逻辑更新成这样:

const uniqueOperators = [...new Set(data.map((x) => x.operatorName))];
const uniqueDates = [...new Set(data.map((x) => x.date))];

const subLabelDatasets = uniqueOperators.map((uniqueOperator) => {
  // find by operatorName and date in `data`
  // if not found, fill with `0`
  let datasets = uniqueDates.map((uniqueDate) =>
    data.find(
      (item) =>
        item.operatorName === uniqueOperator && item.date === uniqueDate
    )?.totalCount ?? 0
  );

  return {
    label: uniqueOperator,
    data: datasets,
  };
});

这将像你期望的那样输出一个数组:

[
    {label: 'OSEN+', data: [1, 7, 0, 1, 0, 0, 0]}
    {label: 'MBC', data: [0, 0, 1, 0, 1, 1, 15]}
]

即使API输出的数据在同一日期包含多个运算符,此代码也可以工作,如:

const data = [
  { operatorName: 'OSEN+', date: '01-06-2023', totalCount: 1 },
  { operatorName: 'MBC', date: '01-06-2023', totalCount: 2 },
];

将输出:

[
    {label: 'OSEN+', data: [1]}
    {label: 'MBC', data: [2]}
]

你可以用uniqueDates来替换硬编码的lineChartLabels

this.lineChartLabels = [...uniqueDates];

完整代码

(在您的stackblitz演示上测试)

export class AppComponent {
  /**Line chart */
  public lineChartType: ChartType = 'line';
  public lineChartData: any = [];
  public lineChartLabels: any = [];
  public lineChartOptions = {
    responsive: true,
    scales: {
      yAxes: [
        {
          ticks: {
            beginAtZero: true,
          },
        },
      ],
    },
  };
  /**Line chart */

  ngOnInit(): void {
    let data = [
      { operatorName: 'OSEN+', date: '05-04-2023', totalCount: 1 },
      { operatorName: 'OSEN+', date: '12-04-2023', totalCount: 7 },
      { operatorName: 'MBC', date: '01-06-2023', totalCount: 1 },
      { operatorName: 'OSEN+', date: '02-06-2023', totalCount: 1 },
      { operatorName: 'MBC', date: '09-06-2023', totalCount: 1 },
      { operatorName: 'MBC', date: '16-06-2023', totalCount: 1 },
      { operatorName: 'MBC', date: '23-06-2023', totalCount: 15 },
    ];

    const uniqueOperators = [...new Set(data.map((x) => x.operatorName))];
    const uniqueDates = [...new Set(data.map((x) => x.date))];

    this.lineChartData = uniqueOperators.map((uniqueOperator) => {
      // find by operatorName and date in `data`
      // if not found, fill with `0`
      let datasets = uniqueDates.map((uniqueDate) =>
        data.find(
          (item) =>
            item.operatorName === uniqueOperator && item.date === uniqueDate
        )?.totalCount ?? 0
      );
    
      return {
        label: uniqueOperator,
        data: datasets,
      };
    });

    this.lineChartLabels = [...uniqueDates];
  }
}

我希望它有帮助!

相关问题