AngularJS Group平均值

时间:2015-04-26 22:53:28

标签: angularjs grouping average

我正在进行测验,一旦完成就会显示雷达图。测验有~15个问题,但图表只有大约10个数据点要显示。

将一些答案分组,然后为该组返回平均分数。

我需要数据返回一个使用10个数据点匹配图形的数组。

然而,由于我有超过10个问题,我需要分组,然后平均值。

我在想的那种逻辑

//group_average = question_score + question_score / question_score_group_array.length
//Score grouping is to be decided by a diagramGroupId property
X = (3 + 4) / 2
data: [7, 7, 7, X, 7, 7, 7, 7, 7, 7]

我知道我需要遍历答案,然后可能通过diagramGroupId对答案进行分组,计算平均值然后再添加回图表数据集数组

这是我当前的控制器onSubmit功能

$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,...] */
  });

这是我的Graph对象(使用TC Angular ChartJS

  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]
      }
    ]
  };

JSON

//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
}

0 个答案:

没有答案