Built with blockbuilder.org
forked from jalapic's block: gini curves
Built with blockbuilder.org
forked from jalapic's block: gini curves
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<style> | |
body { | |
font: 14px sans-serif; | |
} | |
.axis path, | |
.axis line { | |
fill: none; | |
stroke: #e5e5e5; | |
shape-rendering: crispEdges; | |
} | |
.axis path { | |
display: none; | |
} | |
.line { | |
fill: none; | |
stroke-width: 1px; | |
opacity: 1; | |
} | |
</style> | |
<body> | |
<p> Work in Progress !!!!!</p> | |
<div style="width: 1000px; padding-left: 0.75cm"> | |
<div class="container" id="chartw" style="float: left; width: 500px; height: 500px;margin-top: 25px;"><h2>Wins</h2></div> | |
<div class="container" id="chartl" style="float: left; width: 500px; height: 500px;margin-top: 25px;"><h2>Losses</h2></div> | |
</div> | |
<script src="//d3js.org/d3.v3.min.js"></script> | |
<script> | |
var margin = {top: 60, right: 80, bottom: 60, left: 80}, | |
width = 500 - margin.left - margin.right, | |
height = 460 - margin.top - margin.bottom; | |
var x = d3.scale.linear() | |
.range([0, width]) | |
.domain([12,1]); | |
var y = d3.scale.linear() | |
.range([height, 0]) | |
.domain([0,1]); | |
var color = d3.scale.ordinal() | |
.range(["#e4b31f", "#9d1422", "#f9db9e", "#f4a3a5", "#f1624f"]) | |
.domain(["A","B","C","D","E"]); | |
var giniw = [0.62,0.79,0.55,0.69,0.78]; | |
var ginil = [0.22,0.22,0.25,0.21,0.20]; | |
var xAxis = d3.svg.axis() | |
.scale(x) | |
.orient("bottom") | |
.tickFormat(d3.format("d")); | |
var yAxis = d3.svg.axis() | |
.scale(y) | |
.orient("left") | |
.ticks(5) | |
.tickSize(-width, 0, 0); | |
var line = d3.svg.line() | |
.interpolate("basis") | |
.x(function(d) { return x(d.rank); }) | |
.y(function(d) { return y(d.cohort); }); | |
var svgw = d3.select("body").select("div#chartw").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
var svgl = d3.select("body").select("div#chartl").append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
d3.csv("wins.txt", function(error, data) { | |
if (error) throw error; | |
data.forEach(function (d,i) { | |
d.rank = +d.rank; | |
d.A = +d.A; | |
d.B = +d.B; | |
d.C = +d.C; | |
d.D = +d.D; | |
d.E = +d.E; | |
}) | |
var keys = d3.keys(data[0]).filter(function(key) { return key !== "rank"; }); | |
var cohortLines = keys.map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return { | |
rank: d.rank, | |
cohort: d[name]}; | |
}) | |
}; | |
}); | |
//Append axes | |
svgw.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svgw.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
//Append the lines | |
var lines = svgw.selectAll(".lines") | |
.data(cohortLines) | |
.enter().append("g") | |
; | |
//Actual full opacity thin line | |
lines.append("path") | |
.attr("class", "line") | |
.classed("mainline", true) | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }) | |
.style("stroke-width", 2); | |
// Fading and Selecting Lines | |
doHover(); | |
// X-axis label | |
d3.select("svg").append("text") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(" | |
+((width + margin.left + margin.right)/2) +"," | |
+(height + margin.top + margin.bottom/1.4)+")") | |
.text("Rank"); | |
// Y-axis label | |
d3.select("svg").append("text") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(" | |
+ (margin.left/3) +"," | |
+((height + margin.top + margin.bottom)/2)+")rotate(-90)") | |
.text("Cumulative Proportion of Wins"); | |
}); | |
// losses | |
d3.csv("losses.txt", function(error, data) { | |
if (error) throw error; | |
data.forEach(function (d,i) { | |
d.rank = +d.rank; | |
d.A = +d.A; | |
d.B = +d.B; | |
d.C = +d.C; | |
d.D = +d.D; | |
d.E = +d.E; | |
}) | |
var keys = d3.keys(data[0]).filter(function(key) { return key !== "rank"; }); | |
var cohortLines = keys.map(function(name) { | |
return { | |
name: name, | |
values: data.map(function(d) { | |
return { | |
rank: d.rank, | |
cohort: d[name]}; | |
}) | |
}; | |
}); | |
//Append axes | |
svgl.append("g") | |
.attr("class", "x axis") | |
.attr("transform", "translate(0," + height + ")") | |
.call(xAxis); | |
svgl.append("g") | |
.attr("class", "y axis") | |
.call(yAxis); | |
//Append the lines | |
var lines = svgl.selectAll(".lines") | |
.data(cohortLines) | |
.enter().append("g") | |
.attr("class", "lines") | |
//Actual full opacity thin line | |
lines.append("path") | |
.attr("class", "line") | |
.classed("mainlinel", true) | |
.attr("d", function(d) { return line(d.values); }) | |
.style("stroke", function(d) { return color(d.name); }) | |
.style("stroke-width", 2) | |
; | |
// Fading and Selecting Lines | |
doHover() | |
// X-axis label | |
d3.select("body").select("div#chartl").select("svg").append("text") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(" | |
+((width + margin.left + margin.right)/2) +"," | |
+(height + margin.top + margin.bottom/1.4)+")") | |
.text("Rank"); | |
// Y-axis label | |
d3.select("body").select("div#chartl").select("svg").append("text") | |
.attr("text-anchor", "middle") | |
.attr("transform", "translate(" | |
+ (margin.left/3) +"," | |
+((height + margin.top + margin.bottom)/2)+")rotate(-90)") | |
.text("Cumulative Proportion of Losses"); | |
}); | |
function doHover() { | |
d3.selectAll('path.line')//register this to all paths | |
.on("mouseover", function(d,i) { | |
//first make all lines vanish | |
d3.selectAll('path.line') | |
.style("opacity", 0.1) | |
.style("stroke-width", 2) | |
//only show lines which have same name. | |
d3.selectAll('path.line').filter(function(d1) { | |
return d.name == d1.name | |
}) | |
.style("opacity", 1) | |
.style("stroke-width", 4); | |
d3.select("div#chartw.container svg") | |
.append("text") | |
.attr("id", "cohorttext") | |
.html("Cohort " + d.name) | |
.attr("x", (width) / 1.2) | |
.attr("y", margin.top * 1.5) | |
.style("fill", color(d.name)) | |
.style("font-weight", "bold") | |
.style("font-size", "18px"); | |
d3.select("div#chartw.container svg") | |
.append("text") | |
.attr("id", "cohorttextx") | |
.html("Gini = " + giniw[i%giniw.length])//so that its always within the max length | |
.attr("x", (width) / 1.2) | |
.attr("y", 20 + margin.top * 1.5) | |
.style("fill", color(d.name)) | |
.style("font-size", "14px"); | |
d3.select("div#chartl.container svg") | |
.append("text") | |
.attr("id", "cohorttext") | |
.text("Cohort " + d.name) | |
.attr("x", (width) / 1.2) | |
.attr("y", margin.top * 1.5) | |
.style("fill", color(d.name)) | |
.style("font-weight", "bold") | |
.style("font-size", "18px"); | |
d3.select("div#chartl.container svg") | |
.append("text") | |
.attr("id", "cohorttextx") | |
.html("Gini = " + ginil[i%ginil.length])//so that its always within the max length | |
.attr("x", (width) / 1.2) | |
.attr("y", 20 + margin.top * 1.5) | |
.style("fill", color(d.name)) | |
.style("font-size", "14px"); | |
}) | |
.on("mouseout", function() { | |
d3.selectAll('path.line') | |
.style("opacity", 1) | |
.style("stroke-width", 2); | |
//selectALL because we are giving same id to both text in 2 svgs | |
d3.selectAll("#cohorttext").remove() | |
d3.selectAll("#cohorttextx").remove() | |
}) | |
} | |
</script> |
"rank","A","B","C","D","E" | |
1,1,1,1,1,1 | |
2,0.87906976744186,0.854807692307692,0.83034379671151,0.84828349944629,0.855758880516685 | |
3,0.761860465116279,0.736538461538462,0.721973094170404,0.739756367663344,0.734122712594187 | |
4,0.644651162790698,0.624038461538462,0.615844544095665,0.638981173864895,0.632938643702906 | |
5,0.536744186046512,0.529807692307692,0.511210762331839,0.539313399778516,0.539289558665231 | |
6,0.436279069767442,0.4375,0.414050822122571,0.440753045404208,0.446716899892357 | |
7,0.336744186046512,0.349038461538462,0.32660687593423,0.35437430786268,0.355220667384284 | |
8,0.252093023255814,0.261538461538462,0.243647234678625,0.272425249169435,0.266953713670614 | |
9,0.177674418604651,0.190384615384615,0.174140508221226,0.193798449612403,0.196986006458558 | |
10,0.113488372093023,0.125,0.105381165919283,0.120708748615725,0.135629709364909 | |
11,0.052093023255814,0.0644230769230769,0.0523168908819133,0.062015503875969,0.077502691065662 | |
12,0.0027906976744186,0.00961538461538462,0.0254110612855007,0.00332225913621262,0.031216361679225 |
"rank","A","B","C","D","E" | |
1,1,1,1,1,1 | |
2,0.543255813953488,0.315384615384615,0.616591928251121,0.39202657807309,0.369214208826695 | |
3,0.40093023255814,0.144230769230769,0.463378176382661,0.30343300110742,0.181916038751346 | |
4,0.267906976744186,0.0836538461538461,0.366218236173393,0.231450719822813,0.0742734122712594 | |
5,0.183255813953488,0.0625,0.273542600896861,0.172757475083056,0.0581270182992465 | |
6,0.122790697674419,0.0451923076923077,0.195814648729447,0.116279069767442,0.0441334768568353 | |
7,0.0874418604651163,0.0355769230769231,0.121076233183857,0.0786267995570321,0.0322927879440258 | |
8,0.0623255813953488,0.0269230769230769,0.0852017937219731,0.0409745293466224,0.0215285252960172 | |
9,0.0427906976744186,0.0192307692307692,0.0568011958146487,0.0276854928017719,0.0129171151776103 | |
10,0.0241860465116279,0.0125,0.0298953662182362,0.0155038759689922,0.00645855758880517 | |
11,0.012093023255814,0.00673076923076923,0.00597907324364723,0.00664451827242525,0.00107642626480086 | |
12,0.00465116279069767,0.00288461538461538,0.00298953662182362,0.00221483942414175,0 |