Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created March 27, 2013 01:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save enjalot/5250908 to your computer and use it in GitHub Desktop.
Save enjalot/5250908 to your computer and use it in GitHub Desktop.
love & death bars
{"description":"love & death bars","endpoint":"","display":"svg","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"form.csv":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"style.css":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"fullscreen":false,"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"thumbnail":"http://i.imgur.com/gzJe5ls.png"}
date love weddings death funerals relationship married age
2/28/2013 17:34:09 3 0 1 yes yes 27
2/28/2013 17:46:27 1 15 0 5 yes yes 29
2/28/2013 17:46:30 10 5 2 5 yes no 37
2/28/2013 17:47:21 15 1 1 1 No No 27
2/28/2013 17:49:06 5 3 0 0 yes no 23
2/28/2013 17:51:54 10 18 0 6 yes yes 34
2/28/2013 17:55:01 3 25 0 5 yes yes 33
2/28/2013 17:56:55 2 3 0 3 1 1 31
2/28/2013 18:18:33 7 25 0 15 yes no 37
2/28/2013 19:09:36 7 2 1 1 yes no 31
2/28/2013 19:16:53 9 2 1 1 No Yes 25
2/28/2013 19:42:48 2 4 1 2 Yes Yes 42
2/28/2013 21:22:50 7 23 1 42 Yes No 56
2/28/2013 22:35:21 2 10 0 2 Yes No 33
2/28/2013 22:57:50 3 3 0 1 yes No 28
2/28/2013 23:04:19 2 5 1 2 yes yez 33
2/28/2013 23:28:35 48 4 1 0 yes no 39
2/28/2013 23:44:33 1 10 1 2 Yes Yes 39
2/28/2013 23:52:30 6 3 0 3 yes no 30
3/1/2013 0:37:50 1 0 0 1 no no 28
3/1/2013 3:58:36 3 10 1 10 yes yes 35
3/1/2013 5:00:52 10 1 2 4 no no 29
3/1/2013 8:08:34 5 4 1 2 yes no 29
3/1/2013 11:50:38 57 15 2 4 yes yes 31
3/1/2013 11:51:28 3 5 2 1 yes no 31
3/1/2013 11:52:28 12 12 6 5 yes yes 42
3/1/2013 11:53:10 2 5 0 1 yes no 28
3/1/2013 11:53:38 5 22 0 6 yes yes 34
3/1/2013 11:55:58 2 2 2 1 yes no 25
3/1/2013 11:57:24 3 1 1 3 no no 29
3/1/2013 12:00:50 20 15 6 5 yes yes 36
3/1/2013 12:25:09 6 3 1 1 no no 25
3/1/2013 12:54:17 5 4 0 3 yes no 44
3/1/2013 13:19:35 5 3 2 1 Yes No 26
3/1/2013 14:42:04 20 6 2 2 No No 41
3/1/2013 17:04:57 3 2 0 1 yes yes 36
3/3/2013 7:29:03 4 3 0 3 no no 27
3/6/2013 4:40:59 21 5 2 7 yes yes 43
3/6/2013 17:30:18 5 3 0 0 yes no 24
3/26/2013 17:41:01 21 4 2 1 no no 27
var questions = [
// "Timestamp",
"How old are you?",
"How many people have you loved in your life?",
"How many people you loved have died?",
"How many weddings have you attended?",
"How many funerals have you attended?",
"Are you in a love-based relationship?",
"Are you married?"
];
var dims = [
"age",
"love",
"death",
"weddings",
"funerals",
"relationship",
"married"
]
var cw = 400;
var ch = 76;
var yspace = 15;
var cx = 10;
var svg = d3.select("svg");
//click bar will highlight so you can follow
//ui for selecting sort dim
//punchcard
//styling for active
//other color bar for binary
var answers = tributary.form.map(function(d,i) {
return {
ind: i,
date: new Date(d.date),
love: +d.love,
weddings: +d.weddings,
death: +d.death,
funerals: +d.funerals,
relationship: d.relationship.toLowerCase() === "yes" ? true : false,
married: d.married.toLowerCase() === "yes" ? true: false,
age: +d.age
}
})
//bar charts
var xscale = d3.scale.ordinal()
.domain(d3.range(answers.length))
.rangeBands([0, cw], 0.22)
var sortProp = "love";
answers.sort(function(a, b) {
return a[sortProp] - b[sortProp]
})
var bgbars = svg.selectAll("rect.bg")
.data(answers);
bgbars.enter()
.append("rect")
.classed("bg", true)
bgbars
.attr({
x: function(d,i) { return cx + xscale(i) },
y: 0,
width:xscale.rangeBand(),
height: (ch+yspace) * questions.length + 25
}).on("mouseover", function(d) {
updateLabel(d)
})
var charts = svg.selectAll("g.chart")
.data(dims)
.enter()
.append("g")
.classed("chart", true)
.attr("transform", function(d,i) {
return "translate(" + [10, i * (ch + yspace)] + ")"
});
function render() {
bgbars = svg.selectAll("rect.bg")
.data(answers);
bgbars
.attr({
x: function(d,i) { return cx + xscale(i) }
})
charts.each(bar);
}
render();
//make a bar chart offset by its index
function bar(prop, i) {
if(prop === "death") {
var max = d3.max(answers, function(d) { return d["love"]});
} else {
var max = d3.max(answers, function(d) { return d[prop]});
}
var yscale = d3.scale.linear()
.domain([0, max])
.range([0, ch])
var g = d3.select(this);
var bs = g.selectAll("rect")
.data(answers)
bs.enter()
.append("rect")
.classed("bar", true)
bs.attr({
x: function(d,i) { return xscale(i) },
y: function(d,i) { return ch - yscale(d[prop]) + yscale(d[prop])/2},
width: xscale.rangeBand(),
height: function(d,i) { return yscale(d[prop])}
})
.on("mouseover", function(d) {
updateLabel(d)
})
bs.exit().remove();
if(prop === "married" || prop === "relationship") {
bs.attr({
x: function(d,i) { return xscale(i) },
y: function(d,i) { return ch/2 + 20},
width: xscale.rangeBand(),
height: function(d,i) { return 33 },
rx: 2,
ry: 2
}).style({
fill: function(d) { return d[prop] ? "#000": "#DFDFDF" },
stroke: function(d) { return d[prop] ? "#000": "#000" }
})
}
}
var label = svg.append("g")
.attr("transform", "translate(" + [cw + 20, 31] + ")");
label.selectAll("text.question")
.data(questions)
.enter()
.append("text")
.classed("question", true)
.text(function(d) { return d })
.attr({
y: function(d,i) { return ch/2 + i * (ch + yspace) }
}).on("click", function(d,i) {
var sortProp = dims[i];
console.log(sortProp);
answers.sort(function(a, b) {
return a[sortProp] - b[sortProp]
})
render();
})
label.selectAll("text.answer")
.data(questions)
.enter()
.append("text")
.classed("answer", true)
.attr({
y: function(d,i) { return ch/2 + i * (ch + yspace) }
})
function updateLabel(d) {
label.selectAll("text.answer")
.data(dims)
.text(function(dim) { return d[dim] })
.attr({
y: function(d,i) { return 20 + ch/2 + i * (ch + yspace) }
})
svg.selectAll("rect")
.classed("selected", false)
svg.selectAll("rect")
.filter(function(f) { return f.ind === d.ind })
.classed("selected", true)
}
.bg {
fill: #fff;
fill-opacity: 0.34;
}
.bar {
fill: #030530;
fill-opacity: 0.7;
}
.selected {
stroke: red;
fill-opacity: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment