gpt4 book ai didi

javascript - D3 更新 x-y 轴

转载 作者:行者123 更新时间:2023-11-28 18:25:15 29 4
gpt4 key购买 nike

我正在创建一个小示例来显示通过单击按钮进行动态轴更新。我能够更新条形图和轴,但 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>

最佳答案

由于您使用的是 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我创建它是为了调试/测试。我还“修复”了条形过渡 - 您正在更改过渡中的宽度,然后在没有过渡的情况下覆盖它 - 但请注意,没有 object constancy酒吧之间的过渡非常漂亮。最好淡出退出的条并淡入输入的条,并使用数据键来确保过渡条实际上代表相同的数据。

关于javascript - D3 更新 x-y 轴,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39283627/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com