我正在创建一个小例子,通过按钮点击显示动态轴更新。我能够更新条形和轴线,但是y轴间距和条形对齐不合适,并且某些条形似乎重叠以供选择" R"。我已经尝试了很多东西,但无法确定问题。请帮我指出问题。
这是我迄今为止所做的以及link我的虚拟数据:
<!DOCTYPE html>
<meta charset="utf-8">
<style> /* set the CSS */
path {
stroke: steelblue;
stroke-width: 2;
fill: none;
}
.bar {
fill: skyblue;
}
.axis path,
.axis line {
fill: none;
stroke: grey;
stroke-width: 1;
shape-rendering: crispEdges;
}
.buttons-container {
width: 520px;
margin-bottom: 25px;
}
.button {
float: left;
margin-left: 10px;
font-weight: lighter;
cursor: pointer;
}
.selected {
font-weight: bold;
}
.top-label {
font-size: 13px;
font-style: italic;
text-transform: uppercase;
float: left;
}
.axis-label {
text-anchor: middle;
font-size: 13px;
}
.x.axis line {
fill: none;
stroke: #000;
}
.x.axis text {
font-size: 13px;
}
.axis path {
display:none;
}
.y.axis line {
display:none;
}
</style>
<body>
<!-- load the d3.js library -->
<script src="http://d3js.org/d3.v4.min.js"></script>
<script>
// Set the dimensions of the canvas / graph
var margin = {top: 50, right: 50, bottom: 40, left: 50, padding: 5},
width = 960 - margin.left - margin.right,
height = 700 - margin.top - margin.bottom;
var area = "P";
var areas = ["P","Q", "R"];
var x = d3.scaleLinear()
.range([0, width]);
var y = d3.scaleBand()
.rangeRound([0, height], 0.1);
var xAxis = d3.axisTop()
.scale(x);
var yAxis = d3.axisLeft()
.scale(y);
var body = d3.select("body");
var buttons = body.append("div")
.attr("class", "buttons-container")
.selectAll("div").data(areas)
.enter().append("div")
.text(function(d) { return d; })
.attr("class", function(d) {
if(d == area)
return "button selected";
else
return "button";
});
function keys(d) {
return d.subzone;
}
function render(){
// Get the data
d3.csv("data/Dummy.csv", function(error, data) {
data.forEach(function(d) {
d.subzone = d.subzone;
d.zone = d.zone;
d.pop = +d.pop;
});
data2= data.filter(function(element) {return element.zone == area});
console.log(data2);
// Scale the range of the data
y.domain(data2.map(function(d){return d.subzone;}));
x.domain([0, d3.max(data2, function(d) { return d.pop; })]);
// Adds the svg canvas
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 barGroup = svg.append("g").attr("class", "bar")
.attr("width",width)
.attr("height", height);
var bars = barGroup.selectAll("rect").data(data2, keys)
.enter().append("rect")
.attr("x", 0)
.attr("y",function(d){return y(d.subzone);})
.attr("width", function(d){return x(d.pop);})
.attr("height",y.bandwidth()-20 )
// Add the X Axis
svg.append("g")
.attr("class", "x axis")
.attr("transform", "translate(0," + height + ")")
.call(xAxis);
// Add the Y Axis
svg.append("g")
.attr("class", "y axis")
.call(yAxis);
buttons.on("click", function(zone) {
d3.select(".selected")
.classed("selected", false);
d3.select(this)
.classed("selected", true);
area = zone;
popData = data.filter(function(element) {return element.zone == area});
y.domain(popData.map(function(d){return d.subzone;}));
x.domain([0, d3.max(popData, function(d) { return d.pop; })])
console.log(popData);
var ba = barGroup.selectAll("rect").data(popData);
ba.transition()
.duration(500)
.attr("width", function(d) { return x(d.pop); });
ba.enter().append("rect") .attr("x", 0)
.attr("y",function(d){return y(d.subzone);})
.attr("width", function(d){return x(d.pop);})
.attr("height",y.bandwidth()-20 );
ba.exit().remove();
svg.select(".y.axis").transition().duration(1500).call(yAxis);
svg.select(".x.axis").transition().duration(1500).call(xAxis);
});
});
};
render();
</script>
</body>
答案 0 :(得分:3)
由于您使用的是d3.js版本4,magic of implicitly merging the enter() and update selections no longer applies。您需要更改此行:
ba.enter().append("rect") .attr("x", 0)
......对此:
ba.enter().append("rect").merge(ba).attr("x", 0)
请参阅我为调试/测试创建的this plunker。我还修复了#34;条形转换 - 您在转换中更改宽度,然后在没有转换的情况下覆盖它 - 但请注意条形之间没有object constancy,转换非常漂亮。最好淡出现有的条形图并淡入输入条形图,并使用数据键确保转换条实际上代表相同的数据。