AngularJS分组平均
我正在做一个测验,完成后会显示雷达图。测验共有15个问题,但图表仅显示约10个数据点 将部分答案分组,然后返回该组的平均分数 我需要数据返回一个数组,该数组使用10个数据点与图形匹配 然而,由于我有超过10个问题,我需要分组,然后平均出这些值 我在想一种逻辑 我知道我需要对答案进行循环,然后根据diagramGroupId对答案进行潜在分组,计算出平均值,然后将其添加回diagrams数据集数组 这在我当前的控制器onSubmit函数中AngularJS分组平均,angularjs,grouping,average,Angularjs,Grouping,Average,我正在做一个测验,完成后会显示雷达图。测验共有15个问题,但图表仅显示约10个数据点 将部分答案分组,然后返回该组的平均分数 我需要数据返回一个数组,该数组使用10个数据点与图形匹配 然而,由于我有超过10个问题,我需要分组,然后平均出这些值 我在想一种逻辑 我知道我需要对答案进行循环,然后根据diagramGroupId对答案进行潜在分组,计算出平均值,然后将其添加回diagrams数据集数组 这在我当前的控制器onSubmit函数中 $scope.questions.forEach(func
$scope.questions.forEach(function (q) {
answers.push({
//Deal with the results of the quiz
'SurveyId': $scope.survey.Id,
'QuestionId': q.Id,
'Answered': q.Answered});
// radar Diagram dataset 2
userScoreTmp.push({'diagramGroupId': q.diagramGroupId, 'score': q.Answered});
/*[
{diagramGroupId:1, score:4},
{diagramGroupId:2, score:3},
{diagramGroupId:1, score:7},
{diagramGroupId:3, score:4},
{diagramGroupId:4, score:1},
{diagramGroupId:5, score:4},
{...}
]
//which would turn into the following array if I converted it to an array
[4,3,7,4,1,4]
//I need the following (values grouped and sorted by diagramGroupId and then averaged)
[7.5,3,4,1,4,...] */
});
这是我使用的图形对象
JSON
真正的问题是什么?这些组是在哪里定义的?我目前有一个约15个值的数组。我需要将一些值组合起来创建一个平均值,然后添加到一个仅显示10个值的图形的数组中。这些组当前在我的问题JSON数据中定义,我已经给每个问题分配了一个图表,我可以在查看数据时使用它进行循环。我将尝试更新问题,使其更加明显
$scope.questions.forEach(function (q) {
answers.push({
//Deal with the results of the quiz
'SurveyId': $scope.survey.Id,
'QuestionId': q.Id,
'Answered': q.Answered});
// radar Diagram dataset 2
userScoreTmp.push({'diagramGroupId': q.diagramGroupId, 'score': q.Answered});
/*[
{diagramGroupId:1, score:4},
{diagramGroupId:2, score:3},
{diagramGroupId:1, score:7},
{diagramGroupId:3, score:4},
{diagramGroupId:4, score:1},
{diagramGroupId:5, score:4},
{...}
]
//which would turn into the following array if I converted it to an array
[4,3,7,4,1,4]
//I need the following (values grouped and sorted by diagramGroupId and then averaged)
[7.5,3,4,1,4,...] */
});
var labels = [];
var optimum = [];
$scope.radar.forEach(function (d) {
labels.push(d.Name);
optimum.push(d.Optimum);
});
$scope.diagram = {
labels: labels,
datasets: [
{
label: 'My First dataset',
fillColor: 'rgba(220,220,220,0.2)',
strokeColor: 'rgba(220,220,220,1)',
pointColor: 'rgba(220,220,220,1)',
pointStrokeColor: '#fff',
pointHighlightFill: '#fff',
pointHighlightStroke: 'rgba(220,220,220,1)',
data: optimum //[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]
},
{
label: 'My Second dataset',
fillColor: 'rgba(151,187,205,0.2)',
strokeColor: 'rgba(151,187,205,1)',
pointColor: 'rgba(151,187,205,1)',
pointStrokeColor: '#fff',
pointHighlightFill: '#fff',
pointHighlightStroke: 'rgba(151,187,205,1)',
data: usersData //could be something like [7, 2, 7, 3, 4, 1, 4, 7, 7, 7]
}
]
};
//A single question object from my JSON file, contains 15-20 questions
questions: [{
"Id" : 1,
"Name" : "Question 1?",
"Options" : [
{
"Id" : 1,
"Name" : "Yes",
"Score" : 7
},
{
"Id" : 2,
"Name" : "No",
"Score" : 1
},
{
"Id" : 1058,
"Name" : "Unsure",
"Score" : 3
}
],
"QuestionType" : "radio",
"Required" : true,
"Handle" : "Q1",
"diagramGroupId" : 1
}];
// radar object, contains 10 objects
// used to display the radar labels and initial dataset points
"radar": [
{
"Id": 1,
"Name": "Question 1",
"Optimum": 7
},
{
"Id": 2,
"Name": "Question 2",
"Optimum": 7
},
{
"Id": 3,
"Name": "Question 3",
"Optimum": 7
}