index.html
<!DOCTYPE html>
<meta charset="utf-8">
<title>Infant Death</title>
<style>
svg {
font: 8px sans-serif;
}
.foreground path {
fill: none;
stroke: #222;
stroke-opacity: 0.4;
pointer-events: none;
stroke-width: 1px;
}
.axis .title {
font-size: 8px;
font-weight: bold;
text-transform: uppercase;
transform: rotate(-12deg) translate(-5px,-6px);
}
.axis line,
.axis path {
fill: none;
stroke: #000;
stroke-width: 1px;
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
stroke-width: 1px;
}
pre {
width: 900px;
margin: 10px 30px;
tab-size: 21;
font-size: 12px;
overflow: auto;
}
</style>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.5/d3.min.js"></script>
<script>
var margin = {top: 50, right: 80, bottom: 20, left: 100},
width = 960 - margin.left - margin.right,
height = 340 - margin.top - margin.bottom;
var types = {
"Number": {
key: "Number",
coerce: function(d) { return +d; },
extent: d3.extent,
within: function(d, extent) { return extent[0] <= d && d <= extent[1]; },
defaultScale: d3.scale.linear().range([height, 0])
},
"String": {
key: "String",
coerce: String,
extent: function (data) { return data.sort(); },
within: function(d, extent, dim) { return extent[0] <= dim.scale(d) && dim.scale(d) <= extent[1]; },
defaultScale: d3.scale.ordinal().rangePoints([0, height])
},
"Date": {
key: "Date",
coerce: function(d) { return new Date(d); },
extent: d3.extent,
within: function(d, extent) { return extent[0] <= d && d <= extent[1]; },
defaultScale: d3.time.scale().range([0, height])
}
};
var dimensions = [
{
key: "NATURE_INJURY",
description: "Nature of Injury",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d) { return d.slice(0,12); })
},
{
key: "CLASSIFICATION",
description: "Classification",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d) { return d.slice(0,12); })
},
{
key: "INJURY_SOURCE",
description: "Injury Source",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d,i) {
if (i % 3 != 0) return "";
return d.slice(0,12);
})
},
{
key: "INJ_BODY_PART",
description: "Injured Body Part",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d) { return d.slice(0,12); })
},
{
key: "MINING_EQUIP",
description: "Mining Equipment",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d,i) {
if (i % 2 != 0) return "";
return d.slice(0,12);
})
},
{
key: "OCCUPATION",
description: "Occupation",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d,i) {
if (i % 2 != 0) return "";
return d.slice(0,12);
})
},
{
key: "CONTROLLER_NAME",
description: "Controller",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d,i) {
if (i % 5 != 0) return "";
return d.slice(0,12);
})
},
{
key: "OPERATOR_NAME",
description: "Operator",
type: types["String"],
axis: d3.svg.axis().orient("left")
.tickFormat(function(d,i) {
if (i % 6 != 0) return "";
return d.slice(0,12);
})
},
{
key: "ACCIDENT_DT",
description: "Accident Date",
type: types["Date"]
},
{
key: "RETURN_TO_WORK_DT",
description: "Return to Work",
type: types["Date"]
}
];
var x = d3.scale.ordinal()
.domain(dimensions.map(function(dim) { return dim.key; }))
.rangePoints([0, width]);
var line = d3.svg.line()
.defined(function(d) { return !isNaN(d[1]); });
var yAxis = d3.svg.axis()
.orient("left");
var svg = d3.select("body").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 + ")");
var output = d3.select("body").append("pre");
var foreground = svg.append("g")
.attr("class", "foreground");
var axes = svg.selectAll(".axis")
.data(dimensions)
.enter().append("g")
.attr("class", "axis")
.attr("transform", function(d) { return "translate(" + x(d.key) + ")"; });
d3.dsv("|", "text/plain")("accidents.txt", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
for (var k in d) {
d[k] = d[k].slice(0,20);
};
dimensions.forEach(function(p) {
d[p.key] = p.type.coerce(d[p.key]);
});
});
dimensions.forEach(function(dim) {
if (!("domain" in dim)) {
dim.domain = d3.functor(dim.type.extent)(data.map(function(d) { return d[dim.key]; }));
dim.domain.sort(function(a,b) {
return a - b;
});
}
if (!("scale" in dim)) {
dim.scale = dim.type.defaultScale.copy();
}
dim.scale.domain(dim.domain);
});
foreground.selectAll("path")
.data(data)
.enter().append("path")
.attr("d", draw)
.style("stroke", function(d) { return "#6ac"; });
axes.append("g")
.attr("class", "axis")
.each(function(d) {
var renderAxis = "axis" in d
? d.axis.scale(d.scale)
: yAxis.scale(d.scale);
d3.select(this).call(renderAxis);
})
.append("text")
.attr("class", "title")
.attr("text-anchor", "start")
.text(function(d) { return "description" in d ? d.description : d.key; });
axes.append("g")
.attr("class", "brush")
.each(function(d) {
d3.select(this).call(d.brush = d3.svg.brush()
.y(d.scale)
.on("brushstart", brushstart)
.on("brush", brush));
})
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
output.text(d3.tsv.format(data));
function draw(d) {
return line(dimensions.map(function(dim) {
return [x(dim.key), dim.scale(d[dim.key])];
}));
}
function brushstart() {
d3.event.sourceEvent.stopPropagation();
}
function brush() {
var actives = dimensions.filter(function(p) { return !p.brush.empty(); }),
extents = actives.map(function(p) { return p.brush.extent(); });
var selected = [];
d3.selectAll(".foreground path").style("display", function(d) {
if (actives.every(function(dim, i) {
return dim.type.within(d[dim.key], extents[i], dim);
})) {
selected.push(d);
return null;
}
return "none";
});
output.text(d3.tsv.format(selected));
}
});
</script>