我一直在关注codepen示例here,并挂在图例中可以单击图例块以隐藏/显示这些点的部分。
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.on("click", function(year) {
var yearClass = '.y' + year;
if (d3.selectAll(yearClass).classed('hide')) {
d3.selectAll(yearClass).classed('hide', false);
} else {
d3.selectAll("circle:not(" + yearClass + ")")
.classed("hide", function(d, i) {
return !d3.select(this).classed("hide");
});
}
});
我已将代码更改为:
legend.append("rect")
.attr("x", width - 18)
.attr("width", 18)
.attr("height", 18)
.style("fill", color)
.on("click", function(state) {
console.log(state)
if (d3.selectAll(state).classed('hide')) {
d3.selectAll(state).classed('hide', false);
} else {
d3.selectAll("circle:not(" + state + ")")
.classed("hide", function(d, i) {
console.log(d)
return !d3.select(this).classed("hide");
});
}
});
我一直收到错误消息
d3.js:685 Uncaught TypeError:无法读取null
的属性'classList'
状态显示但代码中的“else”部分出现了问题。
下面是代码片段。
<!DOCTYPE html>
<meta charset="utf-8">
<style>
body {
font: 10px sans-serif;
}
.axis path,
.axis line {
fill: none;
stroke: #000;
shape-rendering: crispEdges;
}
.dot {
stroke: #000;
}
div.tooltip {
position: absolute;
text-align: center;
padding: 2px;
font: 12px sans-serif;
background: lightsteelblue;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
.hide {
display: none !important;
}
</style>
<body>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.17/d3.js"></script>
<script>
var margin = {top: 20, right: 20, bottom: 30, left: 80},
width = 1500 - margin.left - margin.right,
height = 900 - 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 div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);
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.csv("data/fiveMilesRadius.csv", function(error, data) {
if (error) throw error;
data.forEach(function(d) {
d.Total_Pop = +d.Total_Pop;
d.Med_HHD_Inc = +d.Med_HHD_Inc;
});
x.domain(d3.extent(data, function(d) { return d.Med_HHD_Inc; })).nice();
y.domain(d3.extent(data, function(d) { return d.Total_Pop; })).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("Median Household Income");
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("Total Population")
svg.selectAll(".dot")
.data(data)
.enter().append("circle")
.attr('class', function(d) {
return d.State;
})
.attr("r", 3.5)
.attr("cx", function(d) { return x(d.Med_HHD_Inc); })
.attr("cy", function(d) { return y(d.Total_Pop); })
.style("fill", function(d) { return color(d.State); })
.on("mouseover", function(d) {
div.transition()
.duration(200)
.style("opacity", .9);
div .html(d.City + ", " + d.State)
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d) {
div.transition()
.duration(500)
.style("opacity", 0);
});
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(state) {
console.log(state)
if (d3.selectAll(state).classed('hide')) {
d3.selectAll(state).classed('hide', false);
} else {
d3.selectAll("circle:not(" + state + ")")
.classed("hide", function(d, i) {
console.log(d)
return !d3.select(this).classed("hide");
});
}
});
legend.append("text")
.attr("x", width - 24)
.attr("y", 9)
.attr("dy", ".35em")
.style("text-anchor", "end")
.text(function(d) { return d; });
});
</script>