我对 java 脚本和 D3 很陌生。我从网上选择了 d3.geo.mercator 代码,并使用单个 .csv 文件根据纬度和经度显示员工和客户。我的老板想要一个选项来分别选择员工或客户。我制作了如下的 html 以重定向到具有相同代码但不同 .csv 文件的不同 html 文件,但是当单击员工选项时,我收到错误“属性 cx:预期长度,“NaN”。”
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>MyCompany</Title>
</head>
<body>
<form action="">
<h2>Select Your Choice..</h2>
<input type="button" value="Customers" onclick="window.location.href='Customers.html';">
<input type="button" value="Employees" onclick="window.location.href='Employees.html';">
</form>
</body>
</html>
由于两者的 D3 代码相同,而不是使用两个 .html 文件,我希望根据所选选项选择 .csv 文件,我需要帮助才能做到这一点。感谢并感谢您的帮助。
<script>
var width = 960,
height = 960;
var projection = d3.geo.mercator()
.center([0, 5 ])
.scale(200)
.rotate([-180,0]);
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var path = d3.geo.path()
.projection(projection);
var g = svg.append("g");
// load and display the World
d3.json("world-110m2.json", function(error, topology) {
// load and display the cities
d3.csv("Customers.csv", function(error, data) {
g.selectAll("circle")
.data(data)
.enter()
.append("a")
.attr("xlink:href", function(d) {
return "https://www.google.com/search?q="+d.city;}
)
.append("circle")
.attr("cx", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("cy", function(d) {
return projection([d.lon, d.lat])[1];
})
.attr("r", 5)
.style("fill", function(d) {
if (d.category == "Employee") {return "red"}
else if (d.category == "Office" ) {return "lawngreen"} // <== Right here
else { return "blue" }
;})
g.selectAll("text")
.data(data)
.enter()
.append("text") // append text
.attr("x", function(d) {
return projection([d.lon, d.lat])[0];
})
.attr("y", function(d) {
return projection([d.lon, d.lat])[1];
})
.attr("dy", -7) // set y position of bottom of text
.style("fill", "black") // fill the text with the colour black
.attr("text-anchor", "middle") // set anchor y justification
.text(function(d) {return d.city;}); // define the text to display
});
g.selectAll("path")
.data(topojson.object(topology, topology.objects.countries)
.geometries)
.enter()
.append("path")
.attr("d", path)
});
// zoom and pan
var zoom = d3.behavior.zoom()
.on("zoom",function() {
g.attr("transform","translate("+
d3.event.translate.join(",")+")scale("+d3.event.scale+")");
g.selectAll("circle")
.attr("d", path.projection(projection));
g.selectAll("path")
.attr("d", path.projection(projection));
});
svg.call(zoom)
</script>