【问题标题】:Grouping the object by key for Chartjs bar chartChartjs条形图按键分组对象
【发布时间】:2020-07-31 19:15:02
【问题描述】:

我正在尝试使用 Chart.js 创建条形图。我在尝试根据每个用户的状态创建分组条形图时遇到了困难。数据如下:

[{statusId: 0, firstName: "Joe", status: "appealed", count: 1},
{statusId: 0, firstName: "Jane", status: "approved", count: 100},
{statusId: 0, firstName: "Smith", status: "approved", count: 63},
{statusId: 0, firstName: "Mike", status: "approved", count: 63},
{statusId: 0, firstName: "Ken", status: "approved", count: 35},
{statusId: 0, firstName: "Kim", status: "approved", count: 193},
{statusId: 0, firstName: "Joe", status: "approved", count: 1},
{statusId: 0, firstName: "Jane", status: "closed", count: 1},
{statusId: 0, firstName: "Joe", status: "concluded", count: 1},
{statusId: 0, firstName: "Jane", status: "denied", count: 6},
{statusId: 0, firstName: "Smith", status: "denied", count: 9},
{statusId: 0, firstName: "Mike", status: "denied", count: 1},
{statusId: 0, firstName: "Mark", status: "denied", count: 8},
{statusId: 0, firstName: "Ken", status: "denied", count: 2},
{statusId: 0, firstName: "Kim", status: "denied", count: 20},
{statusId: 0, firstName: "Joe", status: "denied", count: 2},
{statusId: 0, firstName: "Joe", status: "transferred", count: 1}]

根据这些数据,我需要在 x 轴上为用户创建一个图表,其中包含用户每个状态的计数。它可以在 Chartjs 中通过具有如下数据集的数组轻松完成:

datasets:[{
      data: [//some counts for a group],
     },
     {
       data: [// counts for another group],
     }
    // and so on
}]

问题是我需要根据状态对这些数据进行分组。所以,我能想到的一种解决方案是:

angular.forEeach(data, function(val)){ 
   switch(val.status){
       case 'approved':
        // add count to an approved count array
        break;
       case 'appealed':
       // add count to appealed count array
       break;
   }

}

但我认为这个有问题。如果他们创建另一个状态怎么办?待办的。然后我将不得不返回并更改代码。无论如何我可以按状态对对象进行分组,并为每个组创建一个计数数据数组,然后我可以在数据集中使用这些数据吗?

我刚刚注册了复数形式的 javascript 课程,所以我仍然需要一段时间来学习高级 javascript。同时,谁能告诉我解决这个难题的正确有效的方法?

示例

Chart.js 要求数据格式如下:

var data = {
    labels: ['Joe', 'Jane', 'Smith', 'Mike', 'Ken', 'Kim', 'Mark'],
    datasets: [
        {
            label: 'Appealed',
            fillColor: '#382765',
            data: [1,0,0,0,0,0,0]
        },
        {
            label: 'Approved',
            fillColor: '#7BC225',
            data: [1, 100, 63, 63, 35, 193,0]
        },
        {
            label: 'Denied',
            fillColor: '#2196F3',
            data: [2, 6, 9, 1, 2, 20, 8]
        },

    ]

}

所以,这里发生的情况是,对于labels 中的每个itemstatus 都有一个count,即data 数组中的labeldatasets 数组中。例如:dataAppealed 的数组 label1countappealedJoe,其余为 0 对于所有其他用户。

【问题讨论】:

  • 数据集的数据有顺序还是和给定的数据一样?数据是按状态排序的吗?
  • 与给定数据相同。如果有帮助,我可以按状态或任何其他标准对其进行排序。
  • 如果我正确理解您的问题,您可以使用 lodash 的 groupBy 函数

标签: javascript angularjs chart.js


【解决方案1】:

您可以使用哈希表作为对具有相同 status 的数组的引用,并使用另一个哈希表作为名称索引。然后构建标签数组和数据集。

var raw = [{ statusId: 0, firstName: "Joe", status: "appealed", count: 1 }, { statusId: 0, firstName: "Jane", status: "approved", count: 100 }, { statusId: 0, firstName: "Smith", status: "approved", count: 63 }, { statusId: 0, firstName: "Mike", status: "approved", count: 63 }, { statusId: 0, firstName: "Ken", status: "approved", count: 35 }, { statusId: 0, firstName: "Kim", status: "approved", count: 193 }, { statusId: 0, firstName: "JoeJoe", status: "approved", count: 1 }, { statusId: 0, firstName: "Jane", status: "closed", count: 1 }, { statusId: 0, firstName: "Joe", status: "concluded", count: 1 }, { statusId: 0, firstName: "Jane", status: "denied", count: 6 }, { statusId: 0, firstName: "Smith", status: "denied", count: 9 }, { statusId: 0, firstName: "Mike", status: "denied", count: 1 }, { statusId: 0, firstName: "Mark", status: "denied", count: 8 }, { statusId: 0, firstName: "Ken", status: "denied", count: 2 }, { statusId: 0, firstName: "Kim", status: "denied", count: 20 }, { statusId: 0, firstName: "Joe", status: "denied", count: 2 }, { statusId: 0, firstName: "Joe", status: "transferred", count: 1 }],
    nameIndices = Object.create(null),
    statusHash = Object.create(null),
    data = { labels: [], datasets: [] };

raw.forEach(function (o) {
    if (!(o.firstName in nameIndices)) {
        nameIndices[o.firstName] = data.labels.push(o.firstName) - 1;
        data.datasets.forEach(function (a) { a.data.push(0); });
    }
    if (!statusHash[o.status]) {
        statusHash[o.status] = { label: o.status, fillcolor: 'f00', data: data.labels.map(function () { return 0; }) };
        data.datasets.push(statusHash[o.status]);
    }
    statusHash[o.status].data[nameIndices[o.firstName]] = o.count;
});

console.log(data);
.as-console-wrapper { max-height: 100% !important; top: 0; }

【讨论】:

  • 哇,真快!这似乎是我需要的。我正在阅读代码并从中学习。我将把它应用到我的应用程序中,看看我是否还有其他问题。一旦我理解了一切,我会将此标记为答案。这太棒了!
  • 实际上,这完全不是我所期待的。事实证明,虽然它提供了我需要的格式,但它与firstName 不匹配。我一直在努力解决这个问题。你能帮我解决这个问题吗?图表的 x 轴上应该有 firstname。因此,数据集数组的长度应等于用户数。并且它们的状态和计数应该在该对象内。
  • 所以,唯一的firstname 应该在legends 数组之外的datasets 数组中,如果这有意义的话。
  • 请在示例中添加想要的数据。
  • 我在原始问题中添加了示例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
相关资源
最近更新 更多