通过 d3 更新传单上的 json
Update json on leaflet through d3
我正在使用leaflet
,并在地图上显示带有d3 (vers3)
的json
。基本上我在这里遵循本教程。
这是我的代码。第一个callbackHandler
描述了一种接收另一种语言发送给JavaScript
的信息的方法,基于用户与网站的交互。 pathToFile
是一个文件(json
)的链接,然后由d3.json(...)
加载。
var svg = d3.select(map.getPanes().overlayPane).append("svg"),
g = svg.append("g").attr("class", "leaflet-zoom-hide");
someMethod("myName", function(pathToFile) {
console.log(pathToFile);
d3.json(pathToFile, function(error, collection) {
console.log(collection);
if (error) throw error;
// Use Leaflet to implement a D3 geometric transformation.
function projectPoint(x, y) {
var point = map.latLngToLayerPoint(new L.LatLng(y, x));
this.stream.point(point.x, point.y);
}
var transform = d3.geo.transform({point: projectPoint}),
path = d3.geo.path().projection(transform);
var feature = g1.selectAll("path")
.data(collection.features)
.enter().append("path")
.attr("stroke-width", 0.5)
.attr("fill-opacity", 0.7)
.attr("stroke", "white")
map.on("viewreset", reset);
reset();
// Reposition the SVG to cover the features.
function reset() {
var bounds = path.bounds(collection),
topLeft = bounds[0],
bottomRight = bounds[1];
svg1 .attr("width", bottomRight[0] - topLeft[0])
.attr("height", bottomRight[1] - topLeft[1])
.style("left", topLeft[0] + "px")
.style("top", topLeft[1] + "px");
g.attr("transform", "translate(" + -topLeft[0] + "," + -topLeft[1] + ")");
feature.attr("d", path);
}
console.log("1");
});
console.log("2");
});
有趣的是:第一次执行代码时,它工作正常。我的json
会按原样显示在地图上。但是,当第一个callbackHandler
(someMethod
)第二次执行时(通过某些用户与网站交互时),新json
不会显示在传单上。
这是我尝试更新地图后包含的console.log
的输出:
// on startup of website, the callbackHandler "someMethod" gets
./some/path/toFile
Object {crs: Object, type: "FeatureCollection", features: Array[20]}
2
1
// after interaction with the website and execution of the callbackHandler "someMethod"
./some/other/path/toFile
Object {crs: Object, type: "FeatureCollection", features: Array[9]}
2
1
但是,不会显示新json
。相反,旧的留下来了。为什么?
因为我没有代码可以玩。
我的预感是:
当您第一次调用someMethod
文件时,文件就会上传,一切正常。
var feature = g1.selectAll("path")
.data(collection.features)
.enter().append("path")
.attr("stroke-width", 0.5)
.attr("fill-opacity", 0.7)
.attr("stroke", "white")
原因:
第一次运行g1.selectAll("path")
。选择为空,您可以根据collection.features
中的数据附加路径,这将起作用。
第二次当你这样做时g1.selectAll("path")
它将返回你绑定数据的路径,但追加将不起作用。
所以问题是你需要删除旧的collection.features
或需要更新它。
为此
选项1
var paths = g1.selectAll("path").data()
paths.exit().remove();//remove old data paths
var feature = paths
.enter().append("path")
.attr("stroke-width", 0.5)
.attr("fill-opacity", 0.7)
.attr("stroke", "white")
选项 2 删除所有路径
var paths = g1.selectAll("path").data()
g1.selectAll("path").remove();//remove all paths
var feature = paths
.enter().append("path")
.attr("stroke-width", 0.5)
.attr("fill-opacity", 0.7)
.attr("stroke", "white")
希望这能解决您的问题!
在此处阅读更新/输入/退出
相关文章:
- D3从嵌套的JSON中绘制第二个圆环图
- 如何获取d3.js中json文件中具有特定值的总行数
- 通过内部数组将JSON数据导入D3.js+Google Map
- 修改d3.js圆环图以读取json数组
- 如何使用D3.js根据json中给出的数据在Heatmap中创建可变大小的矩形
- D3动态json密钥名称
- Neo4j结果(JSON格式)创建D3树图
- 在d3.json中使用d3.csv组合多个csv文件数据输入的最佳方法是什么
- 点击浏览器的“后退”按钮显示的是JSON而不是HTML(使用Rails和d3.js)
- 在 d3.js 中导入 json 文件
- D3.js根据 JSON 值将圆圈附加到组
- D3.js:通过Force Layout从JSON文件加载多个网络
- 从D3中的JSON中获取最大值
- D3.js可以't加载json文件
- 如何在d3之外调用d3.json()
- 如何动态地决定并告诉d3.json()函数何时使用哪个json
- 从d3.js中的文件读取json数据不起作用
- 通过flask服务器上的队列访问d3.json对象时出现问题
- 如何加载CSV和JSON文件,并使用d3将两个数据集组合到一个数据集.Json, d3.csv和d3.zip
- Javascript, JSON,D3和访问嵌套变量来创建饼状图