Skip to content

Instantly share code, notes, and snippets.

@jalapic
Created March 4, 2016 04:50
Show Gist options
  • Save jalapic/54afbb8699d8954b46d9 to your computer and use it in GitHub Desktop.
Save jalapic/54afbb8699d8954b46d9 to your computer and use it in GitHub Desktop.
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;
}
.androgs {
fill: none;
stroke: #aaa;
stroke-linejoin: round;
stroke-linecap: round;
stroke-width: 1.5px;
}
.androg--hover {
stroke: #000;
stroke-width: 20px
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 60, right: 80, bottom: 60, left: 80},
width = 700 - 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 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 svg = d3.select("body").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
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
//Append the lines
var lines = svg.selectAll(".lines")
.data(cohortLines)
.enter().append("g")
.attr("class", "lines");
//Very thick barely visible line
lines.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); })
.style("stroke-width", 10)
.style("opacity", 0.05)
.on("mouseover", function() {
d3.select(this)
.style("stroke-width", 20)
.classed("androg--hover", true )
})
.on("mouseout", function() {
d3.select(this)
.style("stroke-width", 10)
.classed("androg--hover", true)
})
;
//Thick rather difficult to see line
lines.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); })
.style("stroke-width", 6)
.style("opacity", 0.1)
;
//Actual full opacity thin line
lines.append("path")
.attr("class", "line")
.attr("d", function(d) { return line(d.values); })
.style("stroke", function(d) { return color(d.name); })
.style("stroke-width", 1.5)
;
});
</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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment