Skip to content

Latest commit

 

History

History
92 lines (88 loc) · 5.51 KB

documentation.md

File metadata and controls

92 lines (88 loc) · 5.51 KB

#piechart.js

var x, string="", user = new Array(), commit = new Array(), addition = new Array(), deletion = new Array(), yes = new Array();

Defining the variables which are empty or containing nothing for now. x is the data which is coming from the server, string for displaying the data in the text form, user is an array for storing the user names, commit, addition and deletion for storing the number of commits, additions and deletions done by the user. yes array is storing the contents for displaying in the pie chart.

jsgoogle.load("visualization", "1", {packages: ["corechart"]});

It requests the Google to load the visualization and packages of the corechart.

$(document).ready(function(){
    $.ajax({
        url: "https://api.github.com/repos/gearsystems/publicportal/stats/contributors",
        dataType: "json",
        success: function (data_from_server)
        {
            x = data_from_server;
            string="JSON DATA" + "<br>";
            yes.push('Users','Commits','Additions','Deletions');
            for (var i = 0; i < x.length; i++ ) {
                string += x[i].author.login + "<br>";
                user.push(x[i].author.login);
                var count_commits = 0, count_additions = 0, count_deletions = 0;
                for (var j = 0 ;j < 4 ; j++){
                    string += "No of commits: " + x[i].weeks[j].c + "<br>";
                    count_commits += x[i].weeks[j].c;
                    string += "No of additions: " + x[i].weeks[j].a + "<br>";
                    count_additions += x[i].weeks[j].a;
                    string += "No of deletions: " + x[i].weeks[j].d + "<p>";
                    count_deletions += x[i].weeks[j].d;
                }
                commit.push(count_commits);
                addition.push(count_additions);
                deletion.push(count_deletions);
            };
            for (var i = 0; i < x.length; i++ ){
                yes.push(user[i],commit[i],addition[i],deletion[i]);
            }

            //Commits
            var data = google.visualization.arrayToDataTable([
                [yes[0], yes[1]],
                [yes[4], yes[5]]
                ]);
            for (var i = 2; i < (yes.length)/4; i++ ){
                data.addRows([[yes[(4*i)],yes[(4*i)+1]]]);
            }
            var options = {
                title: 'Commits'
            };
            var chart = new google.visualization.PieChart(document.getElementById('Commits'));
            chart.draw(data, options);

            //Additions
            var data = google.visualization.arrayToDataTable([
                [yes[0], yes[2]],
                [yes[4], yes[6]]
                ]);
            for (var i = 2; i < (yes.length)/4; i++ ){
                data.addRows([[yes[(4*i)],yes[(4*i)+2]]]);
            }
            var options = {
                title: 'Additions'
            };
            var chart = new google.visualization.PieChart(document.getElementById('Additions'));
            chart.draw(data, options);

            //Deletions
            var data = google.visualization.arrayToDataTable([
                [yes[0], yes[3]],
                [yes[4], yes[7]]
                ]);
            for (var i = 2; i < (yes.length)/4; i++ ){
                data.addRows([[yes[(4*i)],yes[(4*i)+3]]]);
            }
            var options = {
                title: 'Deletions'
            };
            var chart = new google.visualization.PieChart(document.getElementById('Deletions'));
            chart.draw(data, options);
        }  
    });
});

This is using ajax in the $.ajax();

  • url is containing the url.
  • datatype is json.
  • On success, the data from server is stored in the variable data_from_server.

Variable x is storing the data_from_server. string stores 'JSON DATA'. 'Users', 'Commits', 'Additions' and 'Deletions' are being pushed or appended in the yes array. Now, in the for loop whose size is 'x.length' i.e. the number of users, string is appended by the author's name which is being pushed in the user array. Variable count_commits, count_additions and count_deletions for storing the number of commits, additions and deletions made by a particular author/user respectively. In a nested for loop of size 4, count_commits, count_additions and count_deletions are calculated respectively and the string is appended. Exiting the nested for loop, count_commits, count_additions and count_deletions are pushed into the commit, addition and deletion respectively. Exiting the for(earlier) loop, created an extra for loop in which the user, commit, addition and deletion ae pushed into the yes array. For the piechart of 'Commits', data is a 2-d array which has 'Users' and 'Commits' as the first row and then the second row contains the 'user1' and the 'noCommits1'. The data is appended by the remaining users/authors and their commits. In variable options, title is made as 'Commits'. Now chart variable is created, which the 'div' element in the body has id: 'Commits' and the visualization of the piechart for the 'Commits' is made. Finally the chart is drawn. Similarly, the 'Additions' and 'Deletions' piecharts are made.

Firstly a Ajax call brings the JSON corresponding to the repositories that the organisation has. This is then parsed for the repository names which is merged with the base URL string. The contributor data is then retrived as expained above and appended with the data from the next repository.