未显示在数据生成的d3形状顶部的套印格式
Overlays not showing up on top of data-generated d3 shapes
我是d3.js的新手。我使用topoJSON数据来渲染地图,到目前为止效果很好。现在,我想在每个国家/地区的顶部覆盖一些数据,如文本或圆圈,我遇到了麻烦。我有类似的代码:
var countries = g.append("g")
.attr("id", "countries")
.selectAll("path")
.data(topojson.feature(collection, collection.objects.countries).features)
.enter().append("path")
.attr("d", path)
.style("fill", colorize)
.attr("class", "country")
.on("click", clicked)
它正确地渲染了我的地图。为了在上面覆盖一些圆圈,我做了以下操作:
countries
.append("circle")
.attr("r", function(d, i, j) {
return 10; // for now
})
// getCentroid below is a function that returns the
// center of the poligon/path bounding box
.attr("cy", function(d, i, j) { return getCentroid(countries[0][j])[0]})
.attr("cx", function(d, i, j) { return getCentroid(countries[0][j])[1]})
.style("fill", "red")
这相当麻烦(特别是访问countries数组的方式),但它成功地为代表一个国家的每条路径添加了一个圆圈。问题是这个圆圈存在于SVG标记中,但根本没有显示在文档中。我显然做错了什么,但我不知道是什么。
问题是将circle
元素附加到path
元素上,这在SVG中是做不到的。您需要将它们附加到父g
元素中。代码看起来像这样。
var countries = g.selectAll("g.countries")
.data(topojson.feature(collection, collection.objects.countries).features)
.enter().append("g")
.attr("id", "countries");
countries.append("path")
.datum(function(d) { return d; })
.attr("d", path)
// etc
countries.append("circles")
// etc
相关文章:
- 如何使用javascript或html下载PDF格式的填写表单
- 货币代码为欧元-金额的格式不应包含小数
- 单击顶部导航时如何进行向下滚动效果(向下滑动).
- Json数据包含日期和时间格式
- Javascript日期格式类似于ISO,但本地
- jQuery粘性插件可变顶部间距
- 更改angularjs中的日期-时间格式
- 将日期时间从json转换为可读格式
- NodeJS日期格式不起作用
- 使用jQuery更改元素的顶部位置
- 我能得到正确的格式吗
- PHP:显示sqlite数据库中的html格式数据,使用tinymce保存
- 使用正则表达式将输入格式设置为单字符逗号、单字符逗号等
- 将日期和时间转换为UTC格式的日期-Javascript
- Javascript格式UTC日期
- 元素的内容必须由格式正确的字符数据或标记组成
- 背景图像顶部的滚动图像不移动
- 滚动然后捕捉到顶部而不是取消捕捉
- 未显示在数据生成的d3形状顶部的套印格式
- 什么是“;格式寄存器”;JavaScript文件顶部的字符串