D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
andretartar
Full window
Github gist
CampaignSpendingScatterplot
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Module 4 Exercise</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, sans-serif; } h1 { font-size: 28px; margin: 0; } p { font-size: 14px; margin: 5; } svg { background-color: white; } circle:hover { opacity:0.5; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>UKIP Gives Tories Run for Their Money in 2014 European Elections</h1> <p>Snapshot of how the four U.K. political parties who contested the May 2014 European Parliament elections split their campaign spending between England and Scotland. Source: <a href="https://search.electoralcommission.org.uk">The Electoral Commission</a> as of April 2015.</p> <script type="text/javascript"> var w = 500; var h = 500; var padding = [ 10, 20, 40, 60]; //Top, right, bottom, left var xScale = d3.scale.linear() .range([ padding[3],w - padding[1] ]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); var commasFormatter = d3.format(",.0f"); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(5) .tickFormat(function(d) { return "£" + commasFormatter(d);}) .outerTickSize(0); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") . ticks(10) .tickFormat(function(d) { return "£" + commasFormatter(d);}) .outerTickSize(0); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("CampaignSpending2.csv", function(data) { xScale.domain([d3.min(data,function(d) { return +d.EnglandSpending;}),d3.max(data, function(d) { return +d.EnglandSpending; }) ]); yScale.domain([d3.max(data,function(d) { return +d.ScotlandSpending;}),d3.min(data, function(d) { return +d.ScotlandSpending; }) ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", function(d) { return xScale(d.EnglandSpending); }) .attr("cy", function(d) { return yScale(d.ScotlandSpending); }) .attr("r",0) .attr("fill", function(d){ if (d.PoliticalParty=="Conservative Party"){ return "blue"} else if (d.PoliticalParty=="Labour Party"){ return "red"} else if (d.PoliticalParty=="Liberal Democrats"){return "orange"} else if (d.PoliticalParty=="UK Independence Party (UKIP)"){return "purple"} ;}) .append("title") .text(function(d) { return d.PoliticalParty + " spent " + d.EnglandSpending + " pounds in England and "+d.ScotlandSpending + " pounds in Scotland on " + d.SpendingDate }); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2] + 10) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 10) + ",0)") .call(yAxis); circles.sort(function(a, b) { return d3.ascending(+a.TotalSpending, +b.TotalSpending); }) .transition() .delay(function(d, i) { return i*5; }) .duration(2500) .attr("r", 5); svg.append("text") .attr("x",w - 50) .attr("y",h-15) .attr("font-size","10px") .text("England"); svg.append("text") .attr("x",w - 50) .attr("y",h-2) .attr("font-size","10px") .text("Spending"); svg.append("text") .attr("x",0) .attr("y",12) .attr("font-size","10px") .text("Scotland"); svg.append("text") .attr("x",0) .attr("y",25) .attr("font-size","10px") .text("Spending"); }); var Parties = ['Conservative Party','Labour Party','Liberal Democrats','U.K. Independence Party']; var legend = svg.selectAll(".legend") .data(Parties) .enter() .append("g") .attr("class", "legend") .attr("transform","translate("+ 0.75*w+","+0.75*h+")"); legend.append("circle") .attr("cx", 0) .attr("cy", function(d,i){ return 15 * i;}) .attr("r",5) .style('fill', function(d,i){ if (i==0){ return 'blue'} else if (i==1){ return 'red'} else if (i==2){return 'orange'} else if (i==3){return 'purple'} ;}) legend.append("text") .attr("x",10) .attr("y",function(d,i){ return 5 + 15 * i; }) .attr('font-size','10px') .text(function(d,i){ return Parties[i]; }) </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js