D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
nikhilkardale
Full window
Github gist
FIFA World Cups: Goals For v/s Goals Against
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Visualization using D3</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 22px; font-weight: bold; font-family: Helvetica; margin: 0; color: rgb(0, 0, 0); } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } circle:hover { fill: red; } .axis path, .axis line { fill: none; stroke: gray; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>Goals For v/s Goals Against in FIFA World Cups</h1> <p>Source: Datasets from <a href="https://tuvalabs.com/datasets/">Tuva Labs</a></p> <script type="text/javascript"> var strAttrOne = 'GoalsFor'; var strAttrTwo = 'GoalsAgainst'; var w = 800; var h = 500; var padding = [30, 10, 60, 155]; // top, right, bottom, left var xScale = d3.scale.linear() .range([padding[3], w - padding[1] - padding[3]]); var yScale = d3.scale.linear() .range([padding[0], h - padding[2]]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(10); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(10); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("fifa_world_cup_all_time_rankings.csv", function(data) { xScale.domain([0, d3.max(data, function(d) { return +d[strAttrOne]; })]); yScale.domain([d3.max(data, function(d) { return +d[strAttrTwo]; }), 0]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", -100) .attr("cy", function(d) { return yScale(d[strAttrTwo]); }) .attr("r", 3) .attr("fill", "#6270DF") .append("title") .text(function(d) { return "Goals scored for " + d.Country + " were " + d[strAttrOne] + " and those scored against them were " + d[strAttrTwo]; }); circles.sort(function(a, b) { return d3.ascending(+a[strAttrOne], +b[strAttrOne]); }) .transition() .delay(function(d, i) { return i * 10; }) .duration(500) .attr("cx", function(d) { return xScale(d[strAttrOne]); }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("text") .attr("class", "x label") .style("text-anchor", "middle") .attr("x", w / 2) .attr("y", h - padding[2] + 50) .text("Goals For") svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ", 0)") .call(yAxis); svg.append("text") .attr("class", "y label") .style("text-anchor", "middle") .attr("dy", h / 2) .attr("transform", "translate(" + -padding[3] + "," + h/2 + ") rotate(270)") .text("Goals Against"); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js