D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
jalapic
Full window
Github gist
gini curves
Built with
blockbuilder.org
<!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 svg = 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 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") .on("mouseover", function(d,i){ 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]) .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.select("#cohorttext").remove() d3.select("#cohorttextx").remove() }) ; //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 d3.selectAll('path.line.mainline') .on("mouseover", function(d) { var HoveredLine = this; d3.selectAll('path.line.mainline').transition().duration(0) .style('opacity',function () { return (this === HoveredLine) ? 1.0 : 0.1; }) .style('stroke-width',function () { return (this === HoveredLine) ? 4 : 2; }) ; }) .on("mouseout", function() { d3.selectAll('path.line.mainline') .style("opacity", 1) .style("stroke-width", 2) }) ; // 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)+")") .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") .on("mouseover", function(d,i){ 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]) .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.select("#cohorttext").remove() d3.select("#cohorttextx").remove() }) ; //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 d3.selectAll('path.line.mainlinel') .on("mouseover", function(d) { var HoveredLine = this; d3.selectAll('path.line.mainlinel').transition().duration(0) .style('opacity',function () { return (this === HoveredLine) ? 1.0 : 0.1; }) .style('stroke-width',function () { return (this === HoveredLine) ? 4 : 2; }); }) .on("mouseout", function() { d3.selectAll('path.line.mainlinel') .style("opacity", 1) .style("stroke-width", 2) }) // 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)+")") .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"); }); </script>
https://d3js.org/d3.v3.min.js