D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
scotthmurray
Full window
Github gist
Taipei Youbike Rental Times since 2012
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Youbike Bar Chart</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <style type="text/css"> body { margin: 40px 0px 0px 40px ; } h1 { font-size: 24px; margin:0; font-family: sans-serif; } p { font-size: 16px; margin: 10px 0 0 0; font-family: sans-serif; } svg { background-color: white; } .bar { cursor: pointer; } .bar text { font-family: Helvetica, sans-serif; font-size: 11px; opacity: 0; } .bar:hover text { opacity: 1; } .bar:hover rect { fill: orange; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .axis text { font-family: sans-serif; font-size: 11px; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Taipei Youbike Usage</h1> <p>Monthly Rental Times Since 2012. Source <a href="https://data.taipei.gov.tw/"> Data Taipei</a>.</p> <script type="text/javascript"> var w = 800; var h = 600; var padding = [20, 30, 50, 90]; 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); 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("youbike.csv",function(data){ widthscale.domain([0, d3.max(data, function(d){ return +d.monthlyTotalRentalTimes; }) ]); heightscale.domain(data.map(function(d){return d.yearMonth;})); var groups = svg.selectAll("g.bar") .data(data) .enter() .append("g") .attr("class", "bar"); groups.append("rect") .attr("x", padding[3]) .attr("y", function(d,i){ return heightscale(d.yearMonth); }) .attr("width", function (d){ return widthscale(d.monthlyTotalRentalTimes); }) .attr("height", heightscale.rangeBand()) .attr("fill","#5292f9"); groups.append("text") .attr("x", function(d) { return padding[3] + widthscale(d.monthlyTotalRentalTimes); }) .attr("y", function(d) { return heightscale(d.yearMonth) + 10; }) .text(function(d) { return d.monthlyTotalRentalTimes; }); // .append("svg:title") // .text(function(d) { // return d.monthlyTotalRentalTimes; // }); svg.append("g") .attr("class","x axis") .attr("transform","translate(" + padding[3] + "," + (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