Modified version of mbostock's scatterplot to support filtering by labels
forked from jkutianski's block: Scatterplot w/label selectors
xxxxxxxxxx
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
.legend rect{
cursor: pointer;
}
</style>
<body>
<script src="https://d3js.org/d3.v3.min.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 40},
width = 960 - margin.left - margin.right,
height = 500 - margin.top - margin.bottom;
var x = d3.scale.linear()
.range([0, width]);
var y = d3.scale.linear()
.range([height, 0]);
var color = d3.scale.category10();
var xAxis = d3.svg.axis()
.scale(x)
.orient("bottom");
var yAxis = d3.svg.axis()
.scale(y)
.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 + ")");
d3.conventions = function(c){
c = c || {}
c.width = c.width || 900
c.height = c.height || 460
c.margin = c.margin || {top: 20, right: 20, bottom: 20, left: 25}
c.parentSel = c.parentSel || d3.select('body')
c.svg = c.svg || c.parentSel.append("svg")
.attr("width", c.width + c.margin.left + c.margin.right)
.attr("height", c.height + c.margin.top + c.margin.bottom)
.append("g")
.attr("transform", "translate(" + c.margin.left + "," + c.margin.top + ")")
c.color = c.color || d3.scale.category10()
c.x = c.x || d3.scale.linear().range([0, c.width])
c.y = c.y || d3.scale.linear().range([c.height, 0])
c.rScale = c.rScale || d3.scale.sqrt().range([5, 20])
c.line = c.line || d3.svg.line()
c.xAxis = c.xAxis || d3.svg.axis().scale(c.x).orient("bottom");
c.yAxis = c.yAxis || d3.svg.axis().scale(c.y).orient("left")
c.drawAxis = function(){
c.svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + c.height + ")")
.call(c.xAxis);
c.svg.append("g")
.attr("class", "y axis")
.call(c.yAxis);
}
return c
}
function ƒ(str){
return function(object){
return object[str]
}
}
d3.selection.prototype.dataAppend = function(data, name){
return this.selectAll(name)
.data(data).enter()
.append(name)
d3.tsv("data.tsv", function(error, data) {
data.forEach(function(d) {
d.sepalLength = +d.sepalLength;
d.sepalWidth = +d.sepalWidth;
});
x.domain(d3.extent(data, function(d) { return d.sepalWidth; })).nice();
y.domain(d3.extent(data, function(d) { return d.sepalLength; })).nice();
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis)
.append("text")
.attr("class", "label")
.attr("x", width)
.attr("y", -6)
.style("text-anchor", "end")
.text("Sepal Width (cm)");
svg.append("g")
.attr("class", "y axis")
.call(yAxis)
.append("text")
.attr("class", "label")
.attr("transform", "rotate(-90)")
.attr("y", 6)
.attr("dy", ".71em")
.style("text-anchor", "end")
.text("Sepal Length (cm)")
var dot = svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr("class", "dot")
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.sepalWidth); })
.attr("cy", function(d) { return y(d.sepalLength); })
.style("fill", function(d) { return color(d.species); })
.attr("data-species", function (d) {
return d.species;
});
var legend = svg.selectAll(".legend")
.data(color.domain())
.enter().append("g")
.attr("class", "legend")
.attr("transform", function(d, i) { return "translate(0," + i * 20 + ")"; });
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.on("click", function (d) {
dot.transition().duration(750)
.attr("opacity", function () {
return (this.dataset.species === d) ? 1 : 0;
});
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
svg.select(".legend").append("rect")
.attr("x", width - 18)
.attr("transform", function () {
return "translate(0," + legend.data().length * 20 + ")";
})
.attr("width", 18)
.attr("height", 18)
.style("fill", "black")
.on("click", function (d) {
dot.transition().duration(750)
.attr("opacity", 1);
});
svg.select(".legend").append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("transform", function () {
return "translate(0," + legend.data().length * 20 + ")";
})
.attr("dy", ".35em")
.style("text-anchor", "end")
.text("all");
var c = d3.conventions()
c.x.domain(d3.extent(data, ƒ('sepalWidth')))
c.y.domain(d3.extent(data, ƒ('sepalLength')))
c.drawAxis()
c.svg.dataAppend(data, "circle.dot")
.attr("r", 3.5)
.attr("cx", ƒ('sepalWidth', c.x))
.attr("cy", ƒ('sepalLength', c.y))
.style("fill", ƒ('species', c.color))
.call(d3.attachTooltip)
});
</script>
Modified http://d3js.org/d3.v3.min.js to a secure url
https://d3js.org/d3.v3.min.js