D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
Qbelil
Full window
Github gist
Exercise Module 4
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Exercise Module 4</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: Ghostwhite; font-family: Helvetica bold, Arial bold, sans-serif; } h1 { font-size: 30px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } .key { font-size: 12px; margin: 10px 0 0 0; } svg { background-color: Ghostwhite; } rect:hover { fill: #339933; } .axis path, .axis line { fill: none; stroke: #000000; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 10px; } .y.axis path, .y.axis line { opacity: 0; } form {width:125px;height:60px;border:1px solid #ccc;padding:10px;} </style> </head> <body> <h1>Barcelona family income by neighborhoods</h1> <p>The 2013 family income in Barcelona neighborhoods </a></p> <p class="key">Source: <a href="https://opendata.bcn.cat/opendata/es">OpenDataBCN</a>. The number after the name of the neighborhood is the district number. (Index RFD Barcelona = 100)</p> <script> var w = 680; var h = 1000; var padding = [ 20, 30, 30, 245 ]; //Top, right, bottom, left var paddingaxis = [ 20, 30 ]; //Top, bottom var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.10); var xAxistop = d3.svg.axis() // top function axis .scale(widthScale) .orient("top"); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom"); var yAxis = d3.svg.axis() .scale(heightScale) .orient("left"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("BCN_Demographics_2013_family_Income.csv", function(data) { /* això carrega les dades a la consola del navegador console.log(data); console.log(data.map(function(d) { return d.Barri; } )); */ data.sort(function(a, b) { return d3.ascending(a.Ordre, b.Ordre); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.Renda_familiar; }) ]); heightScale.domain(data.map(function(d) { return (d.Barri + ", " + d.Districte); } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Barri + ", " + d.Districte); }) .attr("width", function(d) { return widthScale(d.Renda_familiar); }) .attr("height", heightScale.rangeBand()) .attr("fill", "steelblue") .append("title") .text(function(d) { return d.Any + ", " + d.Barri + " (" + d.Codibarri + ") " + " del Districte " + d.Districte + " té una renda familiar de " + d.Renda_familiar + " % respecte la mitjana de Barcelona"; }); // Data text at the bottom of the bars svg.selectAll("text") .data(data) .enter() .append("text") .text(function(d) { return d.Renda_familiar; }) .attr("x", function(d) { return widthScale(d.Renda_familiar) + padding[3] + 5; }) .attr("y", function(d) { return heightScale(d.Barri + ", " + d.Districte) + 9; }) .attr("font-family", "sans-serif") .attr("font-size", "9px") .attr("fill", "Lightgray"); // Axis on the top of the chart svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (paddingaxis[0] + padding[2]) + ")") .call(xAxistop); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - paddingaxis[1] - padding[2]) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding[3] + ",0)") .call(yAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js