Completed
Push — master ( 55a779...bf7294 )
by
unknown
16s queued 13s
created

loc.php ➔ percentile()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 2
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php require __DIR__ . '/_header.php'; ?>
2
3
4
<?php
5
// calculate percentiles
6
function percentile($arr, $percentile = 0.95)
7
{
8
    sort($arr);
9
    return $arr[max(round($percentile * count($arr) - 1.0 - $percentile), 0)];
10
}
11
12
// 1. build an associative array
13
$array = [];
14
foreach ($classes as $class) {
15
    if (isset($class['lloc'])) {
16
        array_push($array, $class['lloc']);
17
    }
18
}
19
20
// 2. percentile map
21
$json = [];
22
if(count($array) > 1) {
23
    $range = range(0.5, 1, .05);
24
    foreach ($range as $percentile) {
25
        $json[] = (object)[
26
            'lloc' => percentile($array, $percentile),
27
            'percentile' => round($percentile * 100),
28
        ];
29
    }
30
}
31
32
?>
33
34
35
<div class="row">
36
    <div class="column">
37
        <div class="bloc">
38
            <h4>Percentile distribution of logical lines of code by class</h4>
39
            <div id="lloc-repartition" style="height: 200px"></div>
40
            <div class="help" style="text-align: center">Percentile</div>
41
        </div>
42
    </div>
43
</div>
44
45
<div class="row">
46
    <div class="column">
47
        <div class="bloc">
48
            <h4>Explore</h4>
49
            <table class="js-sort-table" id="table-length">
50
                <thead>
51
                <tr>
52
                    <th>Class</th>
53
                    <th class="js-sort-number">LLOC</th>
54
                    <th class="js-sort-number">CLOC</th>
55
                    <th class="js-sort-number">Volume</th>
56
                    <th class="js-sort-number">Intelligent content</th>
57
                    <th class="js-sort-number">Comment Weight</th>
58
                </tr>
59
                </thead>
60
                <?php
61
                foreach ($classes as $class) { ?>
62
                    <tr>
63
                        <td><?php echo $class['name']; ?></td>
64
                        <td><?php echo isset($class['lloc']) ? $class['lloc'] : ''; ?></td>
65
                        <td><?php echo isset($class['cloc']) ? $class['cloc'] : ''; ?></td>
66
                        <td><?php echo isset($class['volume']) ? $class['volume'] : ''; ?></td>
67
                        <td><?php echo isset($class['intelligentContent']) ? $class['intelligentContent'] : ''; ?></td>
68
                        <td><?php echo isset($class['commentWeight']) ? $class['commentWeight'] : ''; ?></td>
69
                    </tr>
70
                <?php } ?>
71
            </table>
72
        </div>
73
    </div>
74
</div>
75
76
77
<?php require __DIR__ . '/_footer.php'; ?>
78
79
80
<script>
81
82
    // table
83
    sortTable(document.getElementById('table-length'), 1, -1);
84
85
86
    var margin = {top: 20, right: 20, bottom: 30, left: 40},
87
        width = document.getElementById('lloc-repartition').offsetWidth - margin.left - margin.right,
88
        height = document.getElementById('lloc-repartition').offsetHeight - margin.top - margin.bottom;
89
90
    var x = d3.scale.ordinal()
91
        .rangeRoundBands([0, width], .1);
92
93
    var y = d3.scale.linear()
94
        .range([height, 0]);
95
96
    var xAxis = d3.svg.axis()
97
        .scale(x)
98
        .orient("bottom");
99
100
    var yAxis = d3.svg.axis()
101
            .scale(y)
102
            .orient("left")
103
        ;
104
105
    var svg = d3.select("#lloc-repartition").append("svg")
106
        .attr("width", width + margin.left + margin.right)
107
        .attr("height", height + margin.top + margin.bottom)
108
        .append("g")
109
        .attr("transform", "translate(" + margin.left + "," + margin.top + ")");
110
111
    data = <?php echo json_encode($json, JSON_PRETTY_PRINT); ?>;
112
113
    x.domain(data.map(function (d) {
114
        return d.percentile;
115
    }));
116
    y.domain([0, d3.max(data, function (d) {
117
        return d.lloc;
118
    })]);
119
120
    svg.append("g")
121
        .attr("class", "x axis")
122
        .attr("transform", "translate(0," + height + ")")
123
        .call(xAxis)
124
        .append("text")
125
        .style("text-anchor", "end")
126
127
    svg.append("g")
128
        .attr("class", "y axis")
129
        .call(yAxis)
130
        .append("text")
131
        .attr("transform", "rotate(-90)")
132
        .attr("y", 6)
133
        .attr("dy", ".71em")
134
        .style("text-anchor", "end")
135
        .text("Logical lines of code");
136
137
    svg.selectAll(".bar")
138
        .data(data)
139
        .enter().append("rect")
140
        .attr("class", "bar")
141
        .attr("x", function (d) {
142
            return x(d.percentile);
143
        })
144
        .attr("width", x.rangeBand())
145
        .attr("y", function (d) {
146
            return y(d.lloc);
147
        })
148
        .attr("height", function (d) {
149
            return height - y(d.lloc);
150
        });
151
152
    function type(d) {
153
        d.lloc = +d.lloc;
154
        return d;
155
    }
156
157
</script>
158