javascript - 使用 D3.js 绘制 map 上的点

标签 javascript d3.js point

我有一个邮政编码质心点的 geoJSON,我正在 D3.js map 上绘制它。我可以让它们显示,但我无法调整点的大小。我假设 .attr("r", 1) 会这样做,但我一定错过了一些东西。

        d3.json("ZipPoints.json", function (zipPoints) {
            svg.selectAll("g")
               .data(zipPoints.features)
               .enter()
               .append("path")
               .attr("d", path)
               .attr("r", 1)
               .style("fill", "red");
         });

编辑:

        d3.json("ZipPoints.json", function (zipPoints) {
            points.selectAll("circle")
               .data(zipPoints.features)
               .enter()
               .append("circle")
               .attr("r", 1.5)
               .attr("transform", function(d, i) {
                   return "translate(" + projection(zipPoints.features[i].geometry.coordinates) + ")";
                })
               .style("fill", "red")
               .classed("point", true);     
        });

最佳答案

您可以尝试以下操作。

var pins = svg.append("g");
d3.json("ZipPoints.json", function(zipPoints) {
    pins.selectAll("circle")
        .data(zipPoints.features)
        .enter()
        .append("circle")
        .attr("r", 5)
        .style("fill", "red")
        .classed("pin", true);
});

您可能需要对这些点进行转换才能正确渲染它们(我猜)。 在这种情况下,您可以使用以下代码。 (我使用的转换函数需要在使用特定投影构建的 map 上绘制具有纬度、经度信息的数据)。

.attr("transform", function(d) {
    /*whatever transformation that needs to be done*/
    return "translate(" + projection([ d.lon, d.lat ]) + ")";
})

关于javascript - 使用 D3.js 绘制 map 上的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22515208/

相关文章:

javascript - 带区域颜色填充和 y 轴的负色

javascript - “mat-form-field”不是已知元素

javascript - 使用 Jquery 通过 Javascript if/else 语句设置 CSS 属性

javascript - 在时间序列图表 C3.js 中使用数组作为数据源

javascript - DC 条形图中的条形重叠

javascript - 如何更改默认工具提示的位置和标题

javascript - d3.js悬停在 map 上不显示文本框,css问题

c# - 如何从2个List<Point>中获取最近的2个点

r - 将形状文件从多边形转换为点?

来自 Point2D.setLocation() 的 Java NullPointerException