Adapted from /mbostock/4063663, this only chooses certain combinations of dimensions to display in a 3x1 matrix
forked from tomshanley's block: Brush scatterplot, three by one matrix
xxxxxxxxxx
•
<meta charset="utf-8">
<title>Trees</title>
<style>
svg {
font: 10px sans-serif;
padding: 10px;
}
.axis,
.frame {
shape-rendering: crispEdges;
}
.axis line {
stroke: #ddd;
}
.axis path {
display: none;
}
.frame {
fill: none;
stroke: #aaa;
}
.field {
fill: lightgreen;
stroke: #aaa;
fill-opacity: .5;
}
circle {
fill-opacity: .7;
}
circle.hidden {
fill: #ccc !important;
}
.extent {
fill: #000;
fill-opacity: .125;
stroke: #fff;
}
.chartTitles {
font-size: 20px;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var width = 1400,
size = 400,
padding = 40,
gap = 30,
noOfCharts = 3;
var x = d3.scale.linear()
.range([padding / 2, size - padding / 2]);
var y = d3.scale.linear()
.range([size - padding / 2, padding / 2]);
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom")
.ticks(5);
var yAxis = d3.svg.axis()
.scale(y)
.orient("left")
.ticks(5);
var color = d3.scale.category10();
d3.csv("trees2.csv", function(error, data) {
var chartTitles = [ "Annual growth by distance from spring",
"Map of the woods",
"Height by age of tree" ];
data.forEach(function(d) {
d.lng = +d.lng;
d.lat = +d.lat;
d.distanceNW = +d.distanceNW;
d.height = +d.height;
d.distanceSpring = +d.distanceSpring;
d.growth = +d.growth;
d.age = +d.age;
});
var domainByTrait = {},
traits = d3.keys(data[0]).filter(function(d) { return d !== "tree"; }),
n = traits.length;
//console.log(traits);
traits.forEach(function(trait) {
domainByTrait[trait] = d3.extent(data, function(d) { return d[trait]; });
});
//console.log(domainByTrait["distanceNW"]);
xAxis.tickSize(size - padding);
yAxis.tickSize(-size + padding);
var brush = d3.svg.brush()
.x(x)
.y(y)
.on("brushstart", brushstart)
.on("brush", brushmove)
.on("brushend", brushend);
var svg = d3.select("body").append("svg")
.attr("width", size * noOfCharts + padding)
.attr("height", size + padding)
.append("g")
.attr("transform", "translate(" + padding + "," + padding / 2 + ")");
svg.selectAll(".x.axis")
.data(["age", "lng", "distanceSpring"])
.enter().append("g")
.attr("class", "x axis")
.attr("transform", function(d, i) { return "translate(" + (((3 - i - 1) * size) ) + "," + (padding/2) +")"; })
.each(function(d) { x.domain(domainByTrait[d]); d3.select(this).call(xAxis); });
svg.selectAll(".y.axis")
.data(["growth", "lat", "height"])
.enter().append("g")
.attr("class", "y axis")
.attr("transform", function(d, i) { return "translate(" + (i * size + (padding/2)) + ", 0)"; })
.each(function(d) { y.domain(domainByTrait[d]); d3.select(this).call(yAxis); });
var cells = [{x: "age", y: "height", i: 0, j: 0},
{x: "lng", y: "lat", i: 1, j: 0} ,
{x: "distanceSpring", y: "growth", i: 2, j: 0}];
var cell = svg.selectAll(".cell")
.data(cells)
.enter().append("g")
.attr("class", "cell")
.attr("transform", function(d) { return "translate(" + (noOfCharts - d.i - 1) * size + "," + d.j * size + ")"; })
.each(plot);
cell.call(brush);
var titles = svg.selectAll(".chartTitles")
.data(chartTitles)
.enter()
.append("text")
.attr("class", "chartTitles")
.attr("text-anchor", "middle")
.attr("transform", function(d, i) { return "translate(" + ((i * size) + (size/2) ) + ", 5)" ; })
.text(function(d) {return d; });
function plot(p) {
var cell = d3.select(this);
x.domain(domainByTrait[p.x]);
y.domain(domainByTrait[p.y]);
cell.append("rect")
.attr("class", function (d) {
var classname = "frame";
if (d.i === 1) {classname = "field";} ;
return classname;
})
.attr("x", padding / 2)
.attr("y", padding / 2)
.attr("width", size - padding)
.attr("height", size - padding);
cell.selectAll("circle")
.data(data)
.enter().append("circle")
.attr("cx", function(d) { return x(d[p.x]); })
.attr("cy", function(d) { return y(d[p.y]); })
.attr("r", 5)
.style("fill", "darkgreen" /*function(d) { return color(d.species); }*/);
}
var brushCell;
// Clear the previously-active brush, if any.
function brushstart(p) {
if (brushCell !== this) {
d3.select(brushCell).call(brush.clear());
x.domain(domainByTrait[p.x]);
y.domain(domainByTrait[p.y]);
brushCell = this;
}
}
// Highlight the selected circles.
function brushmove(p) {
var e = brush.extent();
svg.selectAll("circle").classed("hidden", function(d) {
return e[0][0] > d[p.x] || d[p.x] > e[1][0]
|| e[0][1] > d[p.y] || d[p.y] > e[1][1];
});
}
// If the brush is empty, select all circles.
function brushend() {
if (brush.empty()) svg.selectAll(".hidden").classed("hidden", false);
}
/*
function cross(a, b) {
var c = [], n = a.length, m = b.length, i, j;
//for (i = -1; ++i < n;) for (j = -1; ++j < m;) c.push({x: a[i], i: i, y: b[j], j: j});
c = [{x: "distanceNW", y: "height", i: 0, j: 0},
{x: "lng", y: "lat", i: 1, j: 0} ,
{x: "distanceSpring", y: "growth", i: 2, j: 0}];
console.log(c);
return c;
}
*/
d3.select(self.frameElement).style("height", size * n + padding + 20 + "px");
});
</script>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js