D3.js OpenLayers3 交互式地图上的 SVG

D3.js SVG on OpenLayers3 interactive map

我想知道将 D3.js 与 OpenLayers 3 集成以创建漂亮的交互式地图有多难。

我正在查看 Mike 的示例,D3 + Leaflet: http://bost.ocks.org/mike/leaflet/

然后在 Mike`s example d3.geo.path + Canvas 处失去所有交互性和 SVG 的 css 样式支持。

而在 OpenLayers-3 example site 上,有这张交互式地图,它将 Mike 的 d3.geo.path + Canvas 示例与 OpenLayers 集成以创建交互式地图:

所以我想知道,OpenLayers3 中缺少什么来允许创建类似于 D3 + Leaflet 示例的东西,或者甚至可以考虑 OL3 设计?

您不能在 openlayers 上使用 leaflet 使用的 css 方法,D3 + openlayer 基本上使用 d3 在用作图像层的 canvas 上绘制数据。

你需要使用 openlayer 方法:layers + style ,你可以得到与 openlayers "imagevector" 层类似的性能。

我用 style + imagevector 编辑了你的 jsfiddle:

http://jsfiddle.net/6r8rat8n/5/

var vector = new ol.layer.Image({
    source: new ol.source.ImageVector({
        source: vectorSource,
        style: new ol.style.Style({
            fill: new ol.style.Stroke({
                color: '#efefef',
                width: 1
            }),
            stroke: new ol.style.Stroke({
                color: '#fff',
                width: 1
            })
        })
    })
});


// select interaction working on "mousemove"
var selectMouseMove = new ol.interaction.Select({
  condition: ol.events.condition.mouseMove,
    style: new ol.style.Style({
                    fill: new ol.style.Stroke({
                        color: 'red',
                        width: 1
                    }),
                    stroke: new ol.style.Stroke({
                        color: '#fff',
                        width: 1
                    })
                })
});

openlayers.v4 和 d3.v4 是可能的。查看这些要点和 fiddle.

https://gist.github.com/grabantot/64ff2daafdb85c7ffd187fe391755494/ https://jsfiddle.net/grabantot/3gq5wbqz/

调用 init_ol_d3(map),它 returns 你是一个在 longlat 和屏幕之间缩放的函数,然后你可以像往常一样使用 d3 和 css:

drawRoute = d3.line()
    .x(function(lonlat) { return s(lonlat)[0] })
    .y(function(lonlat) { return s(lonlat)[1] })

将鼠标悬停在线条上,它会根据 css 中的设置更改颜色。 抱歉解释不当,这是我一天工作的结束。有关更多信息,请参阅要点。 旋转(alt+shift+鼠标)虽然不起作用(我还不需要)。也许有人会修复...