D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
ldaly
Full window
Github gist
Summary of Monetary Contributions
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Monetary Contributions</title> <script type="text/javascript" src="https://d3js.org/d3.v3.js"></script> <link href='https://fonts.googleapis.com/css?family=Hind:300,500' rel='stylesheet' type='text/css'> <style type="text/css"> body { background-color: #DEFFF2; font-family: 'Hind', sans-serif; } h1 { font-family: 'Hind'; font-weight: 500; font-size: 27px; color: white; padding-left: 8px; margin: 0; } h4 { font-size: 18px; font-family: 'Hind'; font-weight: 300; color: white; margin: 0; padding-left: 8px; } p { font-size: 12px; font-family: 'Hind'; font-weight: 300; color: white; padding-left: 5px; } .title { background-color: #8C2D1C; } svg { background-color: #DEFFF2; } rect:hover { fill: #8C2D1C; } .axis path, .axis line { fill: none; stroke: black; /*stroke: #8C2D1C;*/ shape-rendering: crispEdges; } .y.axis path, .y.axis line { opacity: 0; } .axis text { font-family: 'Hind'; font-weight 300; font-size: 11px; color: #8C2D1C; } </style> </head> <body> <div class="title"> <h1>Summary of Monetary Contributions in the Oakland Election*</h1> <h4>The chart shows the total dollar amount that was contributed to a particular campaign during the 2014 city election.</h4> </div> <script type="text/javascript"> var w = 700; var h = 550; var padding = [ 15, 10, 40, 270 ]; //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); 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("CF_AllSummary.csv", function(data) { //example typed in by LD //console.log(data); //console.log(data.map(function(d) {return d.Name;} )); data.sort(function(a, b) { return d3.descending(+a.Total, +b.Total); }); widthScale.domain([ 0, d3.max(data, function(d) { return +d.Total; }) ]); heightScale.domain(data.map(function(d) { return d.Name; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect") .attr("fill", "#D9543C"); rects.attr("x", padding[3]) .attr("y", function(d) { return heightScale(d.Name); }) .attr("width", function(d) { return widthScale(d.Total); }) .attr("height", heightScale.rangeBand()) .append("title") .text(function(d) { return d.Name + ": Contributions $" + d.Total; }); 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> <div class="title"> <p>*The data that is displayed is a summary for a selection of city council and mayoral candidates in the 2014 election cycle in Oakland, CA. Some Filer Names have been shortened for display purposes. <br>Data source: <a href="https://data.oaklandnet.com/"> City of Oakland Public Ethics Commission</a></p> </div> </body> </html>
Modified
http://d3js.org/d3.v3.js
to a secure url
https://d3js.org/d3.v3.js