A horizontal bar chart with overlaid labels. Not as cool as the Live-Updating Bar Chart.
Last active
August 29, 2015 14:00
-
-
Save charlesdguthrie/11356770 to your computer and use it in GitHub Desktop.
Static Bar Chart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[ | |
{"key":"Obama Spends Another Night Searching Behind White House Paintings For Safes", "value":99}, | |
{"key":"Report: Average American Consuming 4 Ounces Of Cheese Right Now", "value":78}, | |
{"key":"Colorado Legalizes Medicinal Fireworks", "value":77}, | |
{"key":"15-Second Meals", "value":64}, | |
{"key":"Statshot: Why Aren't We Going To Prom?", "value":35}, | |
{"key":"Report: U.S. Still Leads World With Highest Density Of Kevins", "value":33}, | |
{"key":"Xylophonist Shredding It", "value":29}, | |
{"key":"Man Has Story For Every Stain On Pants", "value":28}, | |
{"key":"Music Playing In Bar Could Stand To Be Louder, Worse", "value":25}, | |
{"key":"Grandfathers Accidentally Switched At Hospital", "value":21}, | |
{"key":"Visit Home Referred To As Vacation By Parents", "value":19}, | |
{"key":"Area Man Cleans Apartment Once Every Relationship", "value":18}, | |
{"key":"Horoscopes", "value":15}, | |
{"key":"White Male Privilege Squandered On Job At Best Buy", "value":14}, | |
{"key":"Something Happens in Ukraine", "value":11} | |
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script> | |
<link rel="stylesheet" href="style.css"> | |
</head> | |
<body> | |
<div id="everything"> | |
<h1>What People Are Reading Right Now (Static, Fake Data)</h1> | |
<h2>Top 10 Articles by Concurrent Page Views</h2> | |
<div id="chart"></div> | |
<h2>Headlines borrowed from <a href="http://www.theonion.com/">theonion.com</a></h2> | |
</div> | |
<script> | |
//staticBarChart.js | |
var setup = function(targetID){ | |
//Set size of svg element and chart | |
var margin = {top: 0, right: 0, bottom: 0, left: 0}, | |
width = 600 - margin.left - margin.right, | |
height = 400 - margin.top - margin.bottom, | |
categoryIndent = 4*15 + 5, | |
defaultBarWidth = 2000; | |
//Set up scales | |
var x = d3.scale.linear() | |
.domain([0,defaultBarWidth]) | |
.range([0,width]); | |
var y = d3.scale.ordinal() | |
.rangeRoundBands([0, height], 0.1, 0); | |
//Create SVG element | |
d3.select(targetID).selectAll("svg").remove() | |
var svg = d3.select(targetID).append("svg") | |
.attr("width", width + margin.left + margin.right) | |
.attr("height", height + margin.top + margin.bottom) | |
.append("g") | |
.attr("transform", "translate(" + margin.left + "," + margin.top + ")"); | |
//Package and export settings | |
var settings = { | |
margin:margin, width:width, height:height, categoryIndent:categoryIndent, | |
svg:svg, x:x, y:y | |
} | |
return settings; | |
} | |
var drawChart = function(settings,dataset) { | |
//Import settings | |
var margin=settings.margin, width=settings.width, height=settings.height, categoryIndent=settings.categoryIndent, | |
svg=settings.svg, x=settings.x, y=settings.y; | |
//Set domains | |
y.domain(dataset.map(function(d) { return d.key; })); | |
var barmax = d3.max(dataset, function(e) { | |
return e.value; | |
}); | |
x.domain([0,barmax]); | |
//Create chart row and move to the correct height | |
var chartRow = svg.selectAll("g") | |
.data(dataset, function(d){return d.key}) | |
.enter() | |
.append("g") | |
.attr("class", "chartRow") | |
.attr("transform", function(d){ return "translate(0," + y(d.key) + ")"; }); | |
//Add rectangles | |
chartRow.append("rect") | |
.attr("class","bar") | |
.attr("x", 0) | |
.attr("height", y.rangeBand()) | |
.attr("width", function(d) { return x(d.value);}) | |
//Add value labels | |
chartRow.append("text") | |
.attr("class","label") | |
.attr("y", y.rangeBand()/2) | |
.attr("x",0) | |
.attr("dy",".35em") | |
.attr("dx","0.5em") | |
.text(function(d){return d.value;}); | |
//Add Headlines | |
chartRow.append("text") | |
.attr("class","category") | |
.attr("text-overflow","ellipsis") | |
.attr("y", y.rangeBand()/2) | |
.attr("x",categoryIndent) | |
.attr("dy",".35em") | |
.attr("dx","0.5em") | |
.text(function(d){return d.key;}); | |
} | |
//Pulls data. | |
//Uses a callback because d3.json loading is asynchronous | |
var pullData = function(settings,callback){ | |
d3.json("fakeData.json", function (err, data){ | |
if (err) return console.warn(err); | |
formattedData = formatData(data); | |
callback(settings,formattedData); | |
}) | |
} | |
//Sort data in descending order and take the top 10 values | |
var formatData = function(data){ | |
return data.sort(function (a, b) { | |
return b.value - a.value; | |
}) | |
.slice(0, 10); | |
} | |
//I like to call it what it does | |
var draw = function(settings){ | |
pullData(settings,drawChart) | |
} | |
//setup | |
var settings = setup('#chart') | |
draw(settings) | |
</script> | |
</body> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
html, body, div, span, applet, object, iframe, | |
h1, h2, h3, h4, h5, h6, p, blockquote, pre, | |
a, abbr, acronym, address, big, cite, code, | |
del, dfn, em, img, ins, kbd, q, s, samp, | |
small, strike, strong, sub, sup, tt, var, | |
b, u, i, center, | |
dl, dt, dd, ol, ul, li, | |
fieldset, form, label, legend, | |
table, caption, tbody, tfoot, thead, tr, th, td, | |
article, aside, canvas, details, embed, | |
figure, figcaption, footer, header, hgroup, | |
menu, nav, output, ruby, section, summary, | |
time, mark, audio, video { | |
margin: 0; | |
padding: 0; | |
border: 0; | |
font: inherit; | |
font-size: 100%; | |
vertical-align: baseline; | |
font-family: "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; | |
font-weight: inherit; | |
} | |
h1 { | |
font-size:150%; | |
font-family: "HelveticaNeue-Light", "Helvetica Neue Light", "Helvetica Neue", Helvetica, Arial, "Lucida Grande", sans-serif; | |
font-weight: 300; | |
text-align: left; | |
margin-top: 5px; | |
margin-bottom: 5px; | |
} | |
h2 { | |
font-size: 12px; | |
font-style: italic; | |
color: gray; | |
margin-top:5px; | |
margin-bottom:5px; | |
} | |
#everything{ | |
width:600px; | |
margin:20px; | |
} | |
#chart{ | |
width:600px; | |
height:400px; | |
} | |
.bar{ | |
fill:#eaeaea; | |
} | |
text.label{ | |
fill: #777777; | |
color: #777777; | |
font-size: 20px; | |
font-weight: bold; | |
} | |
text.category{ | |
fill: #666666; | |
font-size: 14px; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment