2017-02-24 36 views
0

我該如何使用模型中的數據(角度爲1.5)來根據學生人數(count)獲取每個班級的百分比? 有了這些DATAS我必須建立代表每個類相比,總校學生的學生的百分比donught圖..AngularJS - 如何使用模型中的數據創建圖形

"payload": { 
    "Schools": [{ 
      "Sections": [{ 
        "Name": "Class", 
        "Value": "2A" 
       }, { 
        "Name": "Count", 
        "Value": 29 
       } 
      ] 
     }, { 
      "Sections": [{ 
       "Name": "Class", 
       "Value": "3C", 
      }, { 
       "Name": "Count", 
       "Value": 31 

      }] 
     }, { 
      "Sections": [{ 
       "Name": "Class", 
       "Value": "1B", 
      }, { 
       "Name": "Count", 
       "Value": 27 

      }] 
     } 
    ], 
     "masterCategory": "School members", 
     "totalStudents": 87 
    } 
} 
+0

哪種圖形? – tavnab

+0

我的問題不是圖形,我的問題是將每個數據與相關值相關聯的方式。 – Zaits3v

+0

如果這不是關於圖形,請更新您的問題標題,因爲它要求如何創建角度圖。如果您需要知道如何從名稱/值對列表轉到名稱/百分比對列表,這不是一個真正有問題的問題。 – tavnab

回答

2

var app = angular.module("myApp", []); 
 

 
app.controller("myCtrl", function($scope) { 
 
    $scope.records = { 
 
    "Schools": [{ 
 
     "Sections": [{ 
 
     "Name": "Class", 
 
     "Value": "2A" 
 
     }, { 
 
     "Name": "Count", 
 
     "Value": 29 
 
     }] 
 
    }, { 
 
     "Sections": [{ 
 
     "Name": "Class", 
 
     "Value": "3C", 
 
     }, { 
 
     "Name": "Count", 
 
     "Value": 31 
 

 
     }] 
 
    }, { 
 
     "Sections": [{ 
 
     "Name": "Class", 
 
     "Value": "1B", 
 
     }, { 
 
     "Name": "Count", 
 
     "Value": 27 
 

 
     }] 
 
    }], 
 
    "masterCategory": "School members", 
 
    "totalStudents": 87 
 
    } 
 

 
});
<!DOCTYPE html> 
 
<html> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script> 
 

 
<body ng-app="myApp" ng-controller="myCtrl"> 
 

 
    <h1 ng-repeat="x in records.Schools">{{x.Sections[0].Value}} = {{(x.Sections[1].Value/records.totalStudents)*100 | number : 0}}%</h1> 
 

 
</body> 
 

 
</html>

+0

這是我想要的東西!如何獲得控制器內部的x.Sections [0] .Value'和'x.Sections [1] .Value' ^^ – Zaits3v

+0

angular.forEach($ scope.records.Schools,function(value,key){ (value.Sections [0] .Value +':'+ value.Sections [1] .Value +':'+ $ scope.records.totalStudents); }); – abbasak

+0

有了這些數據,我必須建立一個代表每個班級的喜歡的圖表,學生的比例與學校的學生總數相比。 – Zaits3v

相關問題