-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathtoxicity.js
175 lines (150 loc) · 4.83 KB
/
toxicity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
toxicity = {}
toxicity.checknames = [
"BooleanExpressionComplexity",
"ClassDataAbstractionCoupling",
"ClassFanOutComplexity",
"CyclomaticComplexity",
"FileLength",
"MethodLength",
"NestedIfDepth",
"AnonInnerLength",
"ParameterNumber",
"MissingSwitchDefault"
];
toxicity.colors = [
"#989BFA",
"#9C4B45",
"#8EA252",
"#6D5A8D",
"#5396AC",
"#CE8743",
"#96A9CD",
"#C79593",
"#BCCA98",
"#E9C197"
];
toxicity.calc = function(xmldoc) {
var filenodes = $(xmldoc).find("file");
return $.map(filenodes, toxicity.calcfile);
}
toxicity.calcfile = function(fnode, fidx) {
var path = $(fnode).attr("name").replace(/\\/g, "/");
var result = {
_name: path.split("/").slice(-1)[0],
_path: path,
total: 0
};
$(fnode).children().each(function(eidx, enode) {
var check = $(enode).attr("source").split(".").slice(-1)[0].replace(/Check$/, "")
var matches = $(enode).attr("message").replace(/,/g, "").match(/(\d+)/g)
var score = (matches && matches.length > 1) ? (matches[0] / matches [1]) : 1;
result[check] = (result[check] || 0) + score
result.total += score;
});
return result.total ? result : null;
};
toxicity.draw = function(scores) {
var CHEIGHT = 425;
var BWIDTH = 6;
var BGAP = 2;
var LEFTSPACE = 25;
scores.sort(function(da, db) { return db.total - da.total })
var checks = d3.layout.stack()(toxicity.checknames.map(function(checkname) {
return scores.map(function(d, i) {
return { x: i, y: d[checkname] || 0, score: d };
});
}));
d3.selectAll("svg").remove();
var chart = d3.select("#chart-wrapper").append("svg")
.attr("class", "chart")
.attr("width", LEFTSPACE + (BWIDTH + BGAP) * scores.length + 10)
.attr("height", CHEIGHT + 5); /* to accomodate bottom label */
var xscale = d3.scale.linear()
.domain([0, scores.length])
.rangeRound([LEFTSPACE, (BWIDTH + BGAP) * scores.length + LEFTSPACE])
var yscale = d3.scale.linear()
//.domain([0, d3.max(scores, function(d) { return d.total })])
.domain([0, 41])
.rangeRound([CHEIGHT, 1]);
var yaxis = d3.svg.axis()
.scale(yscale)
.orient("left")
.ticks(10);
var fscale = d3.scale.ordinal().range(toxicity.colors);
chart.selectAll("line")
.data(yscale.ticks(10))
.enter().append("line")
.attr("x1", function(td) { return xscale(0) })
.attr("x2", function(td) { return xscale(scores.length) })
.attr("y1", yscale)
.attr("y2", yscale)
.style("stroke", "#ccc");
var groups = chart.selectAll("g.checks")
.data(checks)
.enter().append("g")
.attr("class", "check")
.style("fill", function(d, i) { return fscale(i); })
.style("stroke", function(d, i) { return d3.rgb(fscale(i)).darker(); });
groups.selectAll("rect")
.data(Object)
.enter().append("rect")
.attr("x", function(d) { return xscale(d.x); })
.attr("y", function(d) { return yscale(d.y + d.y0); })
.attr("height", function(d) { return CHEIGHT - yscale(d.y); })
.attr("width", function(d) { return BWIDTH; })
.call(tooltip(function(d) { return d.score; }));
chart.append("g")
.attr("class", "axis")
.attr("transform", "translate(" + LEFTSPACE + ", 0)")
.call(yaxis);
}
tooltip = function(a) {
var accessor = arguments.length ? a : undefined;
function tooltip(selection) {
selection
.on("mouseover", function(d) {
if (accessor) {
d = accessor(d);
}
var div = d3.select("#chart-wrapper").selectAll("div.tooltip");
if (div.empty()) {
div = d3.select("#chart-wrapper").append("div").attr("class", "tooltip").style("opacity", 0);
}
div.html("");
div.append("h2").text(d._name);
div.append("p").attr("class", "filename").text(d._path.split("/").slice(0, -1).join("/"));
$(toxicity.checknames.slice(0).reverse()).each(function(i, c) {
if(d[c] > 0) {
var color = toxicity.colors[toxicity.colors.length - i - 1];
var p = div.append("p");
p.append("span")
.attr("class", "tooltip-swatch")
.style("color", d3.rgb(color).darker())
.style("background-color", color);
p.append("span")
.text(c + ": " + Math.round(d[c] * 10) / 10);
}
});
var p = div.append("p");
p.append("span")
.attr("class", "tooltip-swatch");
p.append("span")
.attr("class", "total")
.text("Total: " + Math.round(d.total * 10) / 10);
var ttx = d3.event.pageX;
var tty = d3.event.pageY - $("div.tooltip").height() - 15;
var hclip = (ttx + $("div.tooltip").width() + 20) - ($(window).width() + $(window).scrollLeft())
if (hclip > 0) {
ttx -= hclip
}
div.style("left", Math.max(ttx + 4, $(window).scrollLeft() + 5) + "px")
.style("top", Math.max(tty, $(window).scrollTop() + 5) + "px");
div.transition().duration(100).style("opacity", 0.95);
})
.on("mouseout", function(d) {
div = d3.select("body").select("div.tooltip")
div.transition().duration(250).style("opacity", 0);
});
}
return tooltip;
};