Angular 9图表js多柱图

huangapple go评论78阅读模式
英文:

Angular 9 chart js mutibar

问题

I implemented chart js in my angular 9 application. There expecting chart values not coming.

ngOnInit:

ngOnInit(): void {

    this.smdashboardservice.fetchSmDashboardData().subscribe(response => {

            //let data = response.data
            let data = [
                    {"label":"Application","sublabel":"Nice","count":2},
                    {"label":"Application","sublabel":"poor","count":1},
                    {"label":"Channels","sublabel":"Quality","count":2},
                    {"label":"Customer Care","sublabel":"support","count":2}
            ]

            this.barChartLabels = Object.keys(data);
            this.barChartLabels.forEach(label => {
                    this.barChartData[0].data.push(data[label]['count']);
            });

    })
}

Current code giving chart
Angular 9图表js多柱图

But I am expecting like this

Angular 9图表js多柱图

Demo here

英文:

I implemented chart js in my angular 9 application. There expecting chart values not coming.

ngOnInit:

ngOnInit(): void {

        this.smdashboardservice.fetchSmDashboardData().subscribe(response=>{
      
                //let data = response.data
                let data = [
                        {"label":"Application","sublabel":"Nice","count":2},
                        {"label":"Application","sublabel":"poor","count":1},
                        {"label":"Channels","sublabel":"Quality","count":2},
                        {"label":"Customer Care","sublabel":"support","count":2}
                ]
             
                this.barChartLabels = Object.keys(data);
                this.barChartLabels.forEach(label => {
                        this.barChartData[0].data.push(data[label]['count']);
                });
      
        })
}

Current code giving chart
Angular 9图表js多柱图

But I am expecting like this

Angular 9图表js多柱图

Demo here

答案1

得分: 1

需要准备对sublabel进行分类的数据集。

每个数据集应包含x轴类别(label)长度为3的值。

let labels = [...new Set(data.map((x) => x.label))];
let subLabels = [...new Set(data.map((x) => x.sublabel))];
let subLabelDatasets = subLabels.map((x) => {
  let datasets = [];

  for (let label of labels) {
    datasets.push(
      data.find((y) => y.label == label && y.sublabel == x)?.count || 0
    );
  }

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

this.barChartLabels = labels;
this.barChartData = subLabelDatasets;

在StackBlitz上查看演示

另一种方式:subLabelDatasets 可以简化为:

let subLabelDatasets = subLabels.map((x) => ({
  label: x,
  data: labels.map(
    (label) =>
      data.find((y) => y.label == label && y.sublabel == x)?.count || 0
  )
}));
英文:

You need to prepare the datasets which categorize the sublabel.

Each dataset should contain the values with the length of x-axis category (label), which is 3.

let labels = [...new Set(data.map((x) => x.label))];
let subLabels = [...new Set(data.map((x) => x.sublabel))];
let subLabelDatasets = subLabels.map((x) => {
  let datasets = [];

  for (let label of labels) {
    datasets.push(
      data.find((y) => y.label == label && y.sublabel == x)?.count || 0
    );
  }

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

this.barChartLabels = labels;
this.barChartData = subLabelDatasets;

Demo @ StackBlitz

Alternative: subLabelDatasets can be simplified as:

let subLabelDatasets = subLabels.map((x) => ({
  label: x,
  data: labels.map(
    (label) =>
      data.find((y) => y.label == label && y.sublabel == x)?.count || 0
  )
}));

huangapple
  • 本文由 发表于 2023年5月6日 20:43:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/76188955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定