Skip to content

Instantly share code, notes, and snippets.

@sarahghp
Last active August 29, 2015 14:04
Show Gist options
  • Save sarahghp/05c79b9e3eef3f64330f to your computer and use it in GitHub Desktop.
Save sarahghp/05c79b9e3eef3f64330f to your computer and use it in GitHub Desktop.
function draw(){
var width = 4800,
axisHeight = 60;
var lineScale = d3.scale.linear()
.range([0, width - 10]),
startScale = d3.time.scale()
.range([0, width - 10]);
var today = new Date(),
earliest = new Date(2010, 0, 1),
maxDate = Date.parse(today),
minDate = Date.parse(earliest);
function setPossession (possession) {
var maxPossession = Date.parse(new Date(2013, 7, 1));
if (possession === ''){
possession = Math.floor(Math.random() * (maxPossession - minDate) + minDate);
} else {
possession = possession.split('-');
possession = new Date(possession[0], possession[1], possession[2]);
possession = Date.parse(possession);
}
return possession;
}
function setDiffs (type, possession) {
var diff = 0,
startDiff = 0;
diff = maxDate - possession;
startDiff = possession - minDate;
(type === 'diff') ? diff = diff : diff = startDiff;
return diff;
}
function drawLine (diff, line, reason, startDiff) {
var lineClass = '.line-' + line;
function colorGenerator(reason) {
var thisColor;
var colors = {
'interest': '163, 71%, 47%, ',
'recommended': '208, 66%, 50%, ',
'gift': '283, 51%, 50%, ',
'fascination': '1, 100%, 67%, ',
'existential crisis': '241, 12%, 50%, ',
'comfort': '13, 82%, 61%, ',
'consolation': '13, 82%, 61%, ',
}
thisColor = colors[reason] || '334, 82%, 47%, ';
return thisColor;
}
function boxHeightGenerator (index) {
if (index % 20 === 0){
var base = 13 - (i/800);
plusOrMinus = Math.random() > .5 ? 1 : -1 ;
adjustment = plusOrMinus * 5 * (i/width) * Math.random(),
boxHeight = Math.max(0, Math.min((base + adjustment), 10));
return boxHeight;
} else {
return 10;
}
}
function opacityGenerator (index) {
var base = (250/(i + 1)) + .2,
plusOrMinus = Math.random() > .5 ? 1 : -1 ;
adjustment = plusOrMinus * (i/width) * Math.random(),
opacity = Math.max(.1, Math.min((base + adjustment), .7));
return opacity;
}
for (var i = 0; i < diff; i+=10) {
d3.select(lineClass)
.append('rect')
.attr('x', i + startDiff)
.attr('y', (line * 30) + 15)
.attr('width', 10)
.attr('height', boxHeightGenerator(i))
.attr('fill', 'hsla(' + colorGenerator(reason) + opacityGenerator(i) +')');
}
}
d3.csv('timeline.csv', function(error, data){
height = data.length * 30;
var svg = d3.select('.chart')
.append('svg')
.attr('width', width)
.attr('height', height);
data.forEach(function(element, index){
element.periodical = +element.periodical;
element.reason = element['reason'].toLowerCase();
element.possession = setPossession(element.possession);
element.diff = setDiffs('diff', element.possession);
element.startDiff = setDiffs('startDiff', element.possession);
});
var diffMax = d3.max(data, function(d){ return d.diff });
lineScale.domain([0, diffMax]);
var startDiffMax = d3.max(data, function(d){ return d.startDiff });
startScale.domain([0, startDiffMax]);
data.forEach(function(element, index){
element.diff = Math.floor(lineScale(element.diff));
element.startDiff = Math.floor(startScale(element.startDiff));
});
svg.selectAll('g')
.data(data)
.enter()
.append('g')
.attr('class', function(d, i){ return 'line-' + i;})
.on('mouseover', function(d){
var xPosition = event.clientX + scrollX < width - 450 ? event.clientX + scrollX : event.clientX + scrollX - 450,
yPosition = event.clientY + scrollY + 100 > height ? event.clientY + scrollY - 25 : event.clientY + scrollY + 5,
text = '' + d.title + ' by ' + d.author + ', ' + d.length + ' pages';
d3.select('#tooltip')
.style('left', xPosition + 'px')
.style('top', yPosition + 'px')
.select('#values')
.text(text);
d3.select('#tooltip').classed('hidden', false);
})
.on('mouseout', function(){
d3.select('#tooltip').classed('hidden', true);
});
data.forEach(function(element, index){
drawLine(element.diff, index, element.reason, element.startDiff);
});
(function drawAxis(){
var axisSvg = d3.select('.axis')
.append('svg')
.attr('width', width)
.attr('height', axisHeight);
axisSvg.selectAll('circle')
.data(data)
.enter()
.append('circle')
.attr('cx', function(d){return d.startDiff;})
.attr('cy', 20)
.attr('r', 4)
.attr('fill', 'blue');
})();
}
)
}
document.onreadystatechange = function() {
if (document.readyState == 'complete'){
draw();
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title Goes Here</title>
<link rel="stylesheet" href="styles.css">
<script src="http://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="draw.js"></script>
</head>
<body>
<header>
<article class="intro">
<h1>Timeline of Neglect</h1>
<p>Explanation</p>
</article>
</header>
<section>
<div class="axis"></div>
<div class="chart"></div>
<div id="tooltip" class="hidden">
<p><span id="values">Book values go here</span></p>
</div>
</section>
</body>
</html>
body {
margin: 0;
font-family: "Open Sans", sans-serif;
font-size: 12px;
}
g {
cursor: pointer;
}
header {
position: fixed;
width: 100%;
padding: 30px;
top: 0;
background-color: rgba(81, 171, 251, 0.7);
color: #033968;
z-index: 5;
}
header .intro {
width: 800px;
}
.axis {
height: 100%;
width: 4800px;
position: absolute;
top: 200px;
margin-bottom: 60px;
}
.chart {
height: 100%;
}
#tooltip {
position: absolute;
width: 400px;
height: auto;
padding: 5px 10px;
background-color: rgba(255, 255, 255, 0.7);
-webkit-box-shadow: 0 0 5px rgba(0, 0, 0, 0.7);
-moz-box-shadow: 0 0 5px rgba(0, 0, 0, 0.7);
box-shadow: 0 0 5px rgba(0, 0, 0, 0.7);
pointer-events: none;
}
#tooltip.hidden {
display: none;
}
#tooltip p {
margin: 0;
line-height: 20px;
}
title author genre length possession begins ends reason periodical
An Attempt at Exhausting a Place in Paris Georges Perec Fiction: Contemporary 54 2010-03-01 2010-03-02 Interest 0
PS1 Symposium: A Practical Avant Garde n+1 Nonfiction: Essay 63 2014-04-05 2014-04-06 Interest 0
Model View Culture No.1 Multi Periodical 96 2014-04-01 Support 1
Names Marilyn Hacker Poetry 105 Fascination 0
A Minute's Silence Sigfried Lenz Fiction: Modern 125 2014-01-23 Interest 0
Why Read Moby Dick Nathaniel Philbrick Nonfiction: Literature 126 2013-05-14 2013-05-14 Gift 0
Buddhism Without Beliefs Stephen Batchelor Nonfiction: Buddhism 127 2013-11-16 2013-11-18 Comfort 0
Arsene Lupin Gentleman-Burglar Maurice Leblanc Fiction: Mystery 139 2013-07-06 Mystery Obsession 0
Dr Who and the Tomb of the Cybermen Gerry Davis FIction: Contemporary 141 2012-07-20 Fandom 0
Dr Who and the Loch Ness Monster Terrance Dicks FIction: Contemporary 146 2012-07-20 Fandom 0
The Disappointment Artist Jonathan Lethem Nonfiction: Essay 149 2013-02-01 2013-02-15 Existential Crisis 0
Break It Down Lydia Davis Fiction: Contemporary 177 2013-07-23 Affection 0
Bright Lights, Big City Jay McInerney FIction: Contemporary 182 Interest 0
A Murder In Memoriam Didier Daeninckx Fiction: Mystery 185 2013-05-14 Mystery Obsession 0
Atta Jarrett Kobek Fiction: Contemporary 199 2013-12-07 Fascination 0
Vagabonding Rolf Potts Nonfiction: Travel 206 2014-02-04 Recommended 0
You Are Not a Gadget Jaron Lanier Nonfiction: Essay 210 2011-01-20 Gift 0
The Glass Key Dashiell Hammett Fiction: Mystery 214 Recommended 0
Everything Happens Today Jesse Browner Fiction: Contemporary 216 2013-07-09 Recommended 0
French Lessons Alice Kaplan Noonfiction: Memoir 221 2014-04-25 Recognition 0
The Associates Richard Rayner Nonfiction: History 223 Interest 0
VQR Multi Periodical 224 2013-06-01 2013-07-01 Interest 1
Mr Arkadin Orson Welles Fiction: Mystery 226 2013-02-02 Consolation 0
Selected Stories John O'Hara Fiction: Modern 226 2013-08-25 Interest 0
The Secret Life of the Grown-Up Brain Barbara Strauch Nonfiction: Biology 228 Interest 0
Fun Home Alison Bechdel Fiction: Graphic 232 2014-05-07 2014-05-11 2014-05-11 Excitement 0
The Mountain Lion Jean Stafford Fiction: Modern 240 Hope 0
The Dud Avocado Elaine Dundy Fiction: Modern 260 2014-05-07 Recognition 0
Snoop Sam Gosling Nonfiction: Psychology 263 Interest 0
Apartamento Multi Periodical 264 2014-04-16 2014-04-26 Hope 1
The Sea John Banville Fiction: Contemporary 264 2013-11-15 2013-12-14 Reward 0
Maphead Ken Jennings Nonfiction: General 276 2012-02-12 2012-02-14 Gift 0
O My America Sara Wheeler Nonfiction: Biography 281 2014-02-14 Gift 0
College of One Sheilah Graham Nonfiction: Biography 286 2013-05-14 Interest 0
Daring Greatly Brene Brown Nonfiction: General 288 2013-03-01 Comfort 0
Pedaling Revolution Jeff Mapes Nonfiction: History 288 Interest 0
Are You My Mother? Alison Bechdel Fiction: Graphic 290 2014-05-07 2014-05-17 2014-05-21 Excitement 0
Why Does the World Exist Jim Holt Nonfiction: Essay 307 2013-07-23 Existential Crisis 0
A Handful of Dust Evelyn Waugh Fiction: Modern 308 Interest 0
Tne Chronology of Water Lidia Yuknavitch Nonfiction: Memoirt 310 Interest 0
Slow Reding in a Hurried Age David Mikics Nonfiction: Essay 320 2013-12-07 2014-12-16 Hope 0
The Wilder Life Wendy McClure Nonfiction: History 336 2013-01-26 Interest 0
Spade & Archer Joe Gores Fiction: Mystery 337 2013-02-02 Consolation 0
Soundings Hali Felt Nonfiction: History 340 2013-12-25 Gift 0
Elegy for April Benjamin Black Fiction: Mystery 343 2013-08-25 Interest 0
Rambling On: An Apprentices Guide to the Gift of Gab Bohumil Hrabal Fiction: Modern 350 2014-04-25 Nostalgia 0
The Long Embrace Judith Freeman Nonfiction: Biography 353 2013-03-01 2013-03-15 Existential Crisis 0
Girls To The Front Sara Marcus Nonfiction: Music 367 2014-01-15 2014-01-15 Interest 0
Oom Robert Love Nonficiton: History 402 2013-11-18 Interest 0
Best American Mystery Stories 2011 Multi Fiction: Mystery 410 2013-02-02 Consolation 0
Being Wrong Kathryn Schulz Nonfiction: History 440 2013-10-12 Comfort 0
Where the Heart Beats Kay Larson Nonfiction: Biography 474 2013-08-25 2013-08-28 Recognition 0
Cultural Amnesia Clive James Nonfiction: General 876 2013-01-26 Hope 0
Mr Penumbras 24 Hour Bookstore Robin Sloan Fiction: Contemporary 2014-03-30 2014-06-01 2014-06-07 Recommended 0
Show Your Work Austin Kleon Nonfiction: Motivational 2014-03-30 Motivation 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment