This parallel coordinates visualization of cars from the ‘70s and ‘80s demonstrates one of D3 2.5.0’s new interactive features: the brush component. By clicking and dragging along any axis, you can specify a filter for that dimension. The brush component is also used in the updated scatterplot matrix example.
Update: Jason Davies has derived an example with reorderable axes!
forked from mbostock's block: Parallel Coordinates
forked from gordongli's block: Parallel Coordinates
xxxxxxxxxx
<meta charset="utf-8">
<style>
svg {
font: 10px sans-serif;
}
.background path {
fill: none;
stroke: #ddd;
shape-rendering: crispEdges;
}
/* REMOVED STROKE COLOR FROM HERE */
.foreground path {
fill: none;
/* stroke: steelblue; */
}
.brush .extent {
fill-opacity: .3;
stroke: #fff;
shape-rendering: crispEdges;
}
.axis line,
.axis path {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.axis text {
text-shadow: 0 1px 0 #fff, 1px 0 0 #fff, 0 -1px 0 #fff, -1px 0 0 #fff;
}
</style>
<body>
<script src="//d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 30, right: 10, bottom: 30, left: 10},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.ordinal().rangePoints([0, width], 1),
y = {};
var line = d3.svg.line(),
axis = d3.svg.axis().orient("left"),
background,
foreground;
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 + ")");
// CREATE A COLOR SCALE
// see https://stackoverflow.com/questions/24811850/color-coding-parallel-coordinates and https://plnkr.co/edit/7LQUOzlUGvv9KZyLvuAp?p=preview
var color = d3.scale.ordinal()
.domain(['1', '2', '3'])
.range(['#69bf69', '#c9d44d', '#bf6969'])
// .range(['#69bf69', '#bdbe69', '#bf6969'])
// .range(['green', 'gold', 'firebrick']);
d3.csv("cars.csv", function(error, cars) {
// Extract the list of dimensions and create a scale for each.
x.domain(dimensions = d3.keys(cars[0]).filter(function(d) {
return d != "name" && (y[d] = d3.scale.linear()
.domain(d3.extent(cars, function(p) { return +p[d]; }))
.range([height, 0]));
}));
// Add grey background lines for context.
background = svg.append("g")
.attr("class", "background")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path);
// USE THE COLOR SCALE TO SET THE STROKE BASED ON THE DATA
foreground = svg.append("g")
.attr("class", "foreground")
.selectAll("path")
.data(cars)
.enter().append("path")
.attr("d", path)
// added after here
.attr("stroke", function(d) {
var typeNumber = d.type;
return color(typeNumber);
// but how to ignore for non-numerical dimensions in data?
});
// Add a group element for each dimension.
var g = svg.selectAll(".dimension")
.data(dimensions)
.enter().append("g")
.attr("class", "dimension")
.attr("transform", function(d) { return "translate(" + x(d) + ")"; });
// Add an axis and title.
g.append("g")
.attr("class", "axis")
.each(function(d) { d3.select(this).call(axis.scale(y[d])); })
.append("text")
.style("text-anchor", "middle")
.attr("y", -9)
.text(function(d) { return d; });
// Add and store a brush for each axis.
g.append("g")
.attr("class", "brush")
.each(function(d) { d3.select(this).call(y[d].brush = d3.svg.brush().y(y[d]).on("brush", brush)); })
.selectAll("rect")
.attr("x", -8)
.attr("width", 16);
// PREPARE LEGEND
// see https://bl.ocks.org/YuanyuanZh/28897524626fa3f1d4c0
var legend = svg.append('g')
.attr("width", 60)
.attr("height", 10)
//.attr('stroke', '#555')
.attr("transform", "translate(" + (width/3 + 3*margin.left) + "," + (height + margin.bottom - 10) + ")");
var newLegend = function(legendContainer, color, text, offsetX) {
var g = legendContainer.append('g');
if (offsetX) {
g.attr("transform", "translate(" + offsetX + ", 0)");
}
g.append('text')
.style({'font-size': '8px', 'stroke': color, 'fill': color})
.text('██');
g.append('text')
.attr("transform", "translate(24, 0)")
.attr('fill', '#000')
.text(text);
};
newLegend(legend, color('3'), 'For-profit');
newLegend(legend, color('2'), 'Private non-profit', 120);
newLegend(legend, color('1'), 'Public', 240);
});
// Returns the path for a given data point.
function path(d) {
return line(dimensions.map(function(p) { return [x(p), y[p](d[p])]; }));
}
// Handles a brush event, toggling the display of foreground lines.
function brush() {
var actives = dimensions.filter(function(p) { return !y[p].brush.empty(); }),
extents = actives.map(function(p) { return y[p].brush.extent(); });
foreground.style("display", function(d) {
return actives.every(function(p, i) {
return extents[i][0] <= d[p] && d[p] <= extents[i][1];
}) ? null : "none";
});
}
</script>
https://d3js.org/d3.v3.min.js
cars.csv | index.html |