按Chartjs条形图的键对象进行分组

时间:2017-05-19 13:49:58

标签: javascript angularjs chart.js

我正在尝试使用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]
        },

    ]

}

所以,这里发生的事情是itemlabels count status label data里面的datasets data数组。例如:Appealed label的{​​{1}}数组:1count appealed的{​​{1}},其余为0所有其他用户。

1 个答案:

答案 0 :(得分:3)

您可以使用哈希表作为对具有相同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; }