D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
pietronencia
Full window
Github gist
San Francisco Business and Industry Week 4
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Number of San Francisco Businesses per Industry</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: 24px; margin: 0; } p { font-size: 14px; margin: 10px 0 0 0; } svg { background-color: white; } rect:hover { fill: silver; } .axis path, .axis line { fill: none; stroke: silver; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 7px; fill: silver; } </style> </head> <body> <h1>San Francisco Businesses per Industry</h1> <p>Number of business per industry. Source: <a href="https://quickfacts.census.gov/qfd/states/06/06075.html">US Census</a>, 2014</p> <script type="text/javascript"> //Width and height var w = 1200; var h = 400; var barPadding = 7; var padding = [ 20, 10, 20, 20 ]; //Top, right, bottom, left var widthScale = d3.scale.linear() .range([ 0, w - padding[1] - padding[3] ]); var heightScale = d3.scale.ordinal() .rangeRoundBands([ padding[0], h - padding[2] ], 0.1); //Create SVG element var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("SF_Business&IndustryWK4.csv",function(data) { //Create scale functions var xScale = d3.scale.linear() .domain([0, d3.max(data, function(d) { return d.Number/6; })]) .range([0, w]); // var yScale = d3.scale.linear() // .domain([0, h]) // .range([h - barPadding, barPadding]); //Create Axis var xAxis = d3.svg.axis() .scale(xScale) .orient("bottom") .ticks(8); widthScale.domain([ 0, d3.max(data, function(d) { return +d.Number/6; }) ]); heightScale.domain(d3.range(data.length)); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(i); }) .attr("width", function(d) { return widthScale(d.Number/6); }) .attr("height", heightScale.rangeBand()) .attr("fill", "orange"); svg.selectAll("text") .data(data) .enter() .append("text") .text(function(d) { return d.Industry; }) .attr("x", padding[3]) .attr("y", function(d, i) { return heightScale(i) + (barPadding * 1.6); }) .attr("font-family", "sans-serif") .attr("font-size", "10px") .attr("fill", "black"); svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding[3] + "," + (h - padding[2]) + ")") .call(xAxis); }); </script> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js