gpt4 book ai didi

javascript - 根据下拉菜单选择重绘 d3 map

转载 作者:太空宇宙 更新时间:2023-11-04 16:22:53 25 4
gpt4 key购买 nike

我创建了this map

enter image description here

map 将会改变一次,从它开始的地方,到下拉菜单中的其他版本 - 如果你选择它。

但我想让你可以来回更改 - 现在它是单向且粘性的 - 它应该能够来回切换。

如何做到这一点?

这是我的代码:

<!DOCTYPE html>
<meta charset="utf-8">
<style>

.border {
stroke: #000;
fill: none;

}
.graticule {
fill: none;
stroke: #777;
stroke-width: .5px;
stroke-opacity: .5;
}

div.tooltip {
position: absolute;
text-align: center;
width: 84px;
height: 64px;
padding: 2px;
font: 12px sans-serif;
background: lightgrey;
border: 0px;
border-radius: 8px;
pointer-events: none;
}
</style>
<body>

<h1>Administrative Sub-Regions of Europe</h1>

<select id="json_sources" name="json_sources">
<option value ="nuts1" selected>Source 1</option>
<option value ="nuts2">Source 2</option>
<!-- <option value ="source3.json">Source 3</option> -->
</select>​


<script src="http://d3js.org/d3.v3.min.js"></script>
<script src="http://d3js.org/topojson.v1.min.js"></script>
<script src="http://d3js.org/colorbrewer.v1.min.js"></script>
<script src="https://cdn.rawgit.com/rveciana/d3-composite-projections/v0.2.0/composite-projections.min.js"></script>
<script>

var div = d3.select("body").append("div")
.attr("class", "tooltip")
.style("opacity", 0);

var width = 600,
height = 500;

var projection = d3.geo.conicConformalEurope();
var graticule = d3.geo.graticule();

var path = d3.geo.path()
.projection(projection);


// Find new colours here: http://colorbrewer2.org/
var scale = d3.scale.quantize().domain([10,60]).range(colorbrewer.PuRd[3]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);


svg.append("path")
.datum(graticule)
.attr("class", "graticule")
.attr("d", path);



var dropdown = d3.select("#json_sources")
var change = function() {
var source = dropdown.node().options[dropdown.node().selectedIndex].value;


var str1 = source;
var str2 = ".json";
var file = str1.concat(str2);
console.log(file);


d3.json(file, function(error, europe) {

d3.csv("povertry_rate.csv", function(error, povrate) {


//change the map to apadpt to the nuts file
if (source == "nuts1") {

var land = topojson.feature(europe, europe.objects.nuts1);

} else if (source == "nuts2") {

var land = topojson.feature(europe, europe.objects.nuts2);

}


data = {};
povrate.forEach(function(d) {
data[d.GEO] = d['2013'];
});

console.info(data);
svg.selectAll("path")
.data(land.features)
.enter()
.append("path")
.attr("d", path)
.style("stroke","#000")
.style("stroke-width",".5px")
.style("fill",function(d){
var value = data[d.id];
if (isNaN(value)){
value = data[d.id.substring(0,2)];
}
if (isNaN(value)){
return "#fff";
}

return scale(value);
})
.on("mouseover", function(d,i) {
var value = data[d.id];
if (isNaN(value)){
value = data[d.id.substring(0,2)];
}
div.transition()
.duration(200)
.style("opacity", 0.9);
div.html("<b>"+d.properties.name+"</b><br/>" + value + "%")
.style("left", (d3.event.pageX) + "px")
.style("top", (d3.event.pageY - 28) + "px");
})
.on("mouseout", function(d,i) {
div.transition()
.duration(500)
.style("opacity", 0);
});

svg
.append("path")
.style("fill","none")
.style("stroke","#000")
.attr("d", projection.getCompositionBorders());


});




})
}

dropdown.on("change", change)
change(); //trigger json on load



</script>


</body>

数据为here on my GitHub如果你想尝试一下。

最佳答案

你必须添加这个

    //clear way for the regeneration
d3.selectAll("path").remove();

每次绘制 map 之前

关于javascript - 根据下拉菜单选择重绘 d3 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40455443/

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