D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
romsson
Full window
Github gist
ranking chart (soccer Ligue 1)
Built with
blockbuilder.org
<!DOCTYPE html> <meta charset="utf-8"> <style> body { font: 15px sans-serif; font-weight: 1000; } .axis path, .axis line { fill: none; stroke: #000; shape-rendering: crispEdges; } .x.axis path { display: none; } .line { fill: none; stroke: steelblue; stroke-width: 1.5px; } </style> <body> <script src="https://d3js.org/d3.v3.min.js"></script> <script> // Source: https://bl.ocks.org/mbostock/3884955 // Drag and drop manually on this chart var margin = {top: 20, right: 80, bottom: 30, left: 50}, width = 960 - margin.left - margin.right, height = 500 - margin.top - margin.bottom; // var parseDate = d3.time.format("%Y%m%d").parse; var parseDate = d3.time.format("%d").parse; var ease_type = 'linear'; var x = d3.scale.linear() .range([0, width]); var y = d3.scale.linear() .range([0, height]); var color = d3.scale.category10(); var var_y = 'rank'; var var_x = 'date'; var line = d3.svg.line() .interpolate("linear") .x(function(d) { return x(d[var_x]); }) .y(function(d) { return y(d[var_y]); }); 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 + ")"); function flatten_values(data, attr) { return Array.prototype.concat.apply([], data.map(function(d) { return d3.values(d.values); })).map(function(d) { return d[attr]; }); } d3.csv("soccer_ligue1.csv", function(error, data) { if (error) throw error; teams = d3.set(data.map(function(d) { return d.team })).values(); color.domain(teams) cities = teams.map(function(team) { return { name: team, values: data.filter(function(d) { d.date = +d.day; d.rank = +d.rank; d.pts = +d.pts; return d.team == team; }) }; }); x.domain(d3.extent(flatten_values(cities, var_x))); y.domain(d3.extent(flatten_values(cities, var_y))); var xAxis = d3.svg.axis() .scale(x) .ticks(x.domain()[1]) .orient("bottom"); var yAxis = d3.svg.axis() .scale(y) .orient("left"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + height + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .style("display", "none") .call(yAxis) .append("text") .attr("transform", "rotate(-90)") .attr("y", 6) .attr("dy", ".71em") .style("text-anchor", "end") .text("Rank"); city = svg.selectAll(".city") .data(cities) .enter() .append("g") .attr("class", "city"); path = city.append("path") .attr("class", "line") .style("stroke-width", "10px") .style("stroke-opacity", function(d) { if(d.name !== "Paris SG") { return .5; } else { return 1; } }) .attr("d", function(d) { return line(d.values); }) .style("stroke", function(d) { return color(d.name); }); city.append("text") // .datum(function(d) { return {name: d.name, value: d.values[d.values.length - 1]}; }) .attr("transform", function(d) { return "translate(" + x(d.values[d.values.length - 1][var_x]) + "," + y(d.values[d.values.length - 1][var_y]) + ")"; }) .attr("x", 3) .attr("dy", ".35em") .attr("class", "names") .style("fill-opacity", function(d) { if(d.name !== "Paris SG") { return .3; } else { return 1; } }) .text(function(d) { return d.name; }); d3.select("body").selectAll(".clearSelection").data(["absolute"]).enter().append("button") .attr("type", "button") .attr("class", "clearSelection") .on("click", function() { var_y = 'pts'; update(); }) .html("absolute"); d3.select("body").selectAll(".relative").data(["relative"]).enter().append("button") .attr("type", "button") .attr("class", "relative") .on("click", function() { var_y = 'rank'; update(); }) .html("relative"); }); function update() { y.domain(d3.extent(flatten_values(cities, var_y))); if(var_y === 'rank') { ease_type = 'linear' } else { ease_type = 'bounce' } yAxis = d3.svg.axis() .scale(y) .orient("left"); svg.selectAll(".y.axis") .transition().duration(1000).ease(ease_type) .call(yAxis); city.selectAll(".names") .transition().duration(1000).ease(ease_type) .attr("transform", function(d) { return "translate(" + x(d.values[d.values.length - 1][var_x]) + "," + y(d.values[d.values.length - 1][var_y]) + ")"; }); // .attr("x", 3) // .attr("dy", ".35em") // .text(function(d) { return d.name; }); path.transition().duration(1000).ease(ease_type).attr("d", function(d) { return line(d.values); }) } </script>
https://d3js.org/d3.v3.min.js