D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Abechtel
Full window
Github gist
Loans to Individuals vs Net Interest Income - Exercise 5
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Chicago Fed Loans to Individuals (in billions)</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica; } svg { background-color: white; } circle:hover { fill: lightblue; } .axis path, .axis line { fill:none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } </style> </head> <body> <h1>Relationship of Loans to Individuals and Net Interest Income (in billions)</h1> <p> This scatterplot shows the relationship between the Chicago Fed's Loans to Individuals and its Net Interest Income for a given year between 1966 and 2012; Source: <a href="https://fdic.gov">FDIC</a> <p>Loans to Individuals are plotted on the X axis; Net Interest Income is plotted on the Y axis</p> <script type="text/javascript"> var w = 800; var h = 400; var padding = [ 20, 50, 30, 50 ]; var xScale = d3.scale.linear() .range([ padding[3], w - padding[3]]); var yScale = d3.scale.linear() .range([ padding[0], h - padding[2] ]); var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks (10) .tickFormat(function(d) { return "$" + d }); var yAxis = d3.svg.axis() .scale(yScale) .orient("left") .ticks(15) .tickFormat(function(d) { return "$" + d }); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("ChicagoFDIC.csv", function(data) { data.sort(function(a, b) { return d3.descending(+a.individualloans, +b.individualloans); }); xScale.domain([ 0, d3.max(data, function(d) { return +d.individualloans; }) ]); yScale.domain([ d3.max(data, function(d) { return +d.netinterestincome; }), 0 ]); var circles = svg.selectAll("circle") .data(data) .enter() .append("circle"); circles.attr("cx", -100) .attr("cy", function(d) { return yScale(d.netinterestincome); }) .attr("r", 4) .attr("fill", "darkblue") .append("title") .text(function(d) { return "In " + d.Year + ", the Chicago Fed held $" + d.individualloans + " billion in Loans to Individuals"; }); circles.sort(function(a, b) { return d3.ascending(+a.individualloans, +b.netinterestincome); }) .transition() .delay(function(d, i) { return i * 50; }) .duration(2000) .attr("cx", function(d) { return xScale(d.individualloans); }) .attr("r",4); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(0," + (h - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + (padding[3] - 5) + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js