D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
BenHeubl
Full window
Github gist
# of Deliberate Fires in London (Feb. 2015)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Loading CSV Data with D3</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { background-color: #eef7ff; margin: 0; font-family: Arial, sans-serif; } rect { fill: red; } .barLabel { font-size: 12px; fill: #0d2e54; text-anchor: end; } .barValue { font-size: 12px; fill: red; } h1, p { position: relative; left: 10px; color: #0d2e54; } .hover rect{ fill: #9e2f50; transition: fill 0.2s; } .hover text { font-weight: bold; font-size: 14px; } .footnote { position: relative; } </style> </head> <body> <script type="text/javascript"> var margin = {top: 10, right: 10, bottom: 0, left: 5}; var body = d3.select('body'); body.append('h1') .text('Deliberate Fires') body.append('p') .text('Number of Deliberate Fires counted by London Fire Brigade, per London Borough (for Month Feb. 2015)') var svg = d3.select('body').append('svg'), barHeight = 10, barSpace = 5 svg.attr('width', 900) .attr('height', 760) //Load in contents of CSV file d3.csv('londonfiresfinal.csv', function(data) { console.log(data); data.sort(function(a,b) { return d3.descending(+a.dellfires, +b.dellfires) }); svg.selectAll('g') .data(data) .enter() .append('g') var barGroup = d3.selectAll('g'); barGroup.append('rect') .attr({ 'width': function(d) { return d.dellfires * 20; }, 'height': barHeight, 'x': 150, 'y': function(d, i) { return margin.top + (barHeight + barSpace) * i; } }) barGroup.append('text') .text(function(d) { return d.boroughslondon; }) .attr('y', function(d, i) { return margin.top + 9 + (barHeight + barSpace) * i; }) .attr('class', 'barLabel') .attr('x', 140) barGroup.append('text') .attr('class', 'barValue') .attr('x', function(d) { return (d.dellfires * 20) + 155; } ) .attr('y', function(d, i) { return margin.top + 9 + (barHeight + barSpace) * i; }) .text(function(d) { return d.dellfires + ' #'; }) barGroup.attr('cursor', 'pointer') barGroup.on('mouseover', function() { d3.select(this) .classed('hover', true) }) barGroup.on('mouseout', function() { d3.select(this) .classed('hover', false) }) body.append('p') .text('Check out each Borough for its value by hovering over it') .attr('class', 'footnote') }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js