D3
OG
Old school D3 from simpler times
All examples
By author
By category
About
micahstubbs
Full window
Github gist
Materials in Houston Public Art v2
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>Materials in Houston Public Art</title> <script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script> <style type="text/css"> body { background-color: white; font-family: Helvetica, Arial, sans-serif; } h1 { font-size: 24px; margin: 0; /*color: #072854;*/ } p { font-size: 14px; margin: 10px 0 10 0; /*color: #072854;*/ } a:link { /*color: #072854;*/ } a:visited { color: #FF7F00; } a:hover{ opacity: 0.8; } svg { background-color: white; } rect:hover { fill: #FF7F00; } .axis path, .axis line { fill: none; stroke: black; shape-rendering: crispEdges; } .x.axis text { font-family: sans-serif; font-size: 11px; } .y.axis text { font-family: sans-serif; font-size: 11px; fill: white; stroke: none; } .y.axis path, .y.axis line { opacity: 0; } </style> </head> <body> <h1>Materials in Houston Public Art</h1> <p>Public Art on display in Houston ranked by the number of unique materials used in its construction. You can explore the source data over at the City of Houston's <a href="https://data.ohouston.org/dataset/public-art-on-display">Data Portal</a></p> <script type="text/javascript"> var w = 900; var h = 800; var padding = { top:20, right:10, bottom:30, left: 20 }; var widthScale = d3.scale.linear() .rangeRound([0, w - padding.right - padding.left], 1); var heightScale = d3.scale.ordinal() .rangeRoundBands([padding.top, h - padding.bottom], 0.1); var xAxis = d3.svg.axis() .scale(widthScale) .orient("bottom") .tickFormat(d3.format("d")); var yAxis = d3.svg.axis() .scale(heightScale) .orient("right"); var svg = d3.select("body") .append("svg") .attr("width", w) .attr("height", h); d3.csv("houston-public-art.csv", function(data) { // Sort first by count of Materials in the artwork // then by then number of characters in the description of the materials // then alphabetically by the first charcter of the description of the materials data.sort( firstBy(function (v1, v2) { return v2.countOfMaterials - v1.countOfMaterials; }) .thenBy(function (v1, v2) { return v2.mediaAndSupport.length - v1.mediaAndSupport.length; }) .thenBy(function (v1, v2) { return v2.mediaAndSupport - v1.mediaAndSupport; }) ); widthScale.domain([0, d3.max(data, function(d){ return +d.countOfMaterials; }) ]); heightScale.domain(data.map(function(d) { return d.mediaAndSupport; } )); xAxis.ticks(d3.max(data, function(d){ return +d.countOfMaterials; } )); var rects = svg.selectAll("rect") .data(data) .enter() .append("rect"); rects.attr("x", padding.left) .attr("y", function(d){ return heightScale(d.mediaAndSupport); }) .attr("width", function(d){ return widthScale(+d.countOfMaterials); }) .attr("height", heightScale.rangeBand()) .attr("fill", "#072854") .append("title") .text(function(d){ return d.displayTitle + " by " + d.displayArtist/* + " - " + d.mediaAndSupport*/; }) svg.append("g") .attr("class", "x axis") .attr("transform", "translate(" + padding.left + "," + (h-padding.bottom) + ")") .call(xAxis); svg.append("g") .attr("class", "y axis") .attr("transform", "translate(" + padding.left + ",0)") .call(yAxis); }); /*** Copyright 2013 Teun Duynstee Licensed under the Apache License, Version 2.0 ***/ /* https://github.com/Teun/thenBy.js */ firstBy=(function(){ function e(f){ f.thenBy=t; return f} function t(y,x){ x=this; return e(function(a,b){ return x(a,b)||y(a,b) })} return e })(); </script> </body> </html>
https://d3js.org/d3.v3.min.js