javascript - 尝试使用 Promise 将 d3 v3 地图迁移到 d3 v5 地图
问题描述
我正在尝试将使用 d3.js 第 3 版创建的地图迁移到第 5 版。但是,第 5 版使用承诺,我不确定如何更改 d3.json 方法以使用承诺。如何更改现有的 v3 代码以使用 v5 承诺?谢谢你。
我正在使用的原始 d3.json 方法:
d3.json("ne_50m_admin_0_countries_simplified.json", function(json) {
//Bind data and create one path per GeoJSON feature
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "rgba(8, 81, 156, 0.2)")
.attr("fill", "rgba(8, 81, 156, 0.6)");
});
尝试更改它以利用调用 d3.json 的新承诺方式。不生成地图。
d3.json("ne_50m_admin_0_countries_simplified.json").then(data => {
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "rgba(8, 81, 156, 0.2)")
.attr("fill", "rgba(8, 81, 156, 0.6)");
});
解决方案
您似乎在回调函数中使用“数据”作为变量名而不是“json”。
尝试
d3.json("ne_50m_admin_0_countries_simplified.json").then(json => {
svg.selectAll("path")
.data(json.features)
.enter()
.append("path")
.attr("d", path)
.attr("stroke", "rgba(8, 81, 156, 0.2)")
.attr("fill", "rgba(8, 81, 156, 0.6)");
});
推荐阅读
- java - 如何使用intent settype搜索自定义文件扩展名的文本文件
- ios - 上下文菜单块拖放到新窗口
- json - {this.state.object} 对象作为 React 子对象无效
- mysql - 无法再次连接到 AWS RDS
- reactjs - ant Design Carousel:如何访问 goto()、next() 和 prev() 方法
- matlab - Simulink 中查找表的断点是什么?
- html - 用瓷砖包围的侧边栏
- php - PHP 计算 AgrregateRating 满分 5 星
- html - CSS 在桌面和 android 上的 chrome 上运行良好,但在 safari 上不起作用
- javascript - 如何通过javascript更改HTML输入字段/复选框的边框颜色?