d3.js Map(<svg>)自动适应父容器并使用Window调整大小

Jon*_*Jon 30 css svg canvas d3.js

更新:我已经在答案部分发布并接受了一个完整的解决方案.本节中的任何代码都将用作与您自己的非工作代码进行比较的参考,但不能用作解决方案.


我正在构建一个仪表板并使用d3.js添加一个世界地图,该地图将根据地理位置实时绘制推文.

d3.json()行中引用的world.json文件可以在这里下载(它叫做world-countries.json).

在此输入图像描述

该地图作为SVG容器在页面上,并使用d3呈现.

以下是相关的代码片.

<div id="mapContainer">
    <svg xmlns="http://www.w3.org/2000/svg" width="100%" height="500"></svg>
</div>
Run Code Online (Sandbox Code Playgroud)
#mapContainer svg {
    display:block;
    margin:0 auto;
}
#mapContainer path {
  fill:#DDD;
  stroke:#FFF;
}
Run Code Online (Sandbox Code Playgroud)
// generate US plot
function draw() {
    var map = d3.select("svg");
    var width = $("svg").parent().width();
    var height = $("svg").parent().height();

    var projection = d3.geo.equirectangular().scale(185).translate([width/2, height/2]);
    var path = d3.geo.path().projection(projection);
    d3.json('plugins/maps/world.json', function(collection) {
        map.selectAll('path').data(collection.features).enter()
            .append('path')
            .attr('d', path)
            .attr("width", width)
            .attr("height", height);
    });
}
draw();
latestLoop();
Run Code Online (Sandbox Code Playgroud)
$(window).resize(function() {
    draw();
});
Run Code Online (Sandbox Code Playgroud)

更新:我已将地图缩放到可接受的大小(对于我的特定浏览器大小),但是当我更改窗口大小时,它仍然不会缩放和居中.但是,如果我调整窗口大小,然后点击刷新,则重新加载页面后地图将居中.但是,由于比例是静态的,因此不能正确缩放.

在此输入图像描述

Jon*_*Jon 28

完整的解决方案:

这是解决方案,它将在用户释放窗口边缘以调整其大小之后调整地图大小,并将其居中在父容器中.

<div id="mapContainer"></div>
Run Code Online (Sandbox Code Playgroud)
function draw(ht) {
    $("#mapContainer").html("<svg id='map' xmlns='http://www.w3.org/2000/svg' width='100%' height='" + ht + "'></svg>");
    map = d3.select("svg");
    var width = $("svg").parent().width();
    var height = ht;

    // I discovered that the unscaled equirectangular map is 640x360. Thus, we
    // should scale our map accordingly. Depending on the width ratio of the new
    // container, the scale will be this ratio * 100. You could also use the height 
    // instead. The aspect ratio of an equirectangular map is 2:1, so that's why
    // our height is half of our width.

    projection = d3.geo.equirectangular().scale((width/640)*100).translate([width/2, height/2]);
    var path = d3.geo.path().projection(projection);
    d3.json('plugins/maps/world.json', function(collection) {
        map.selectAll('path').data(collection.features).enter()
            .append('path')
            .attr('d', path)
            .attr("width", width)
            .attr("height", width/2);
    });
}
draw($("#mapContainer").width()/2);
Run Code Online (Sandbox Code Playgroud)
$(window).resize(function() {
    if(this.resizeTO) clearTimeout(this.resizeTO);
    this.resizeTO = setTimeout(function() {
        $(this).trigger('resizeEnd');
    }, 500);
});

$(window).bind('resizeEnd', function() {
    var height = $("#mapContainer").width()/2;
    $("#mapContainer svg").css("height", height);
    draw(height);
});
Run Code Online (Sandbox Code Playgroud)

  • 这不是重写一切吗?我怎么能避免这个? (2认同)

Spi*_*ams 8

选择对象是一个多维数组,尽管在大多数情况下它可能只有一个对象.该对象有一个"clientWidth"字段,告诉您其父级的宽度.

所以你可以这样做:

var selection = d3.select("#chart"); 
width = selection[0][0].clientWidth;
Run Code Online (Sandbox Code Playgroud)

  • 由于我有一段时间没有看过它,我会接受你的话.如果你有时间,小提琴可能对未来的人有所帮助,但谢谢你的回答! (2认同)

Eli*_*jah 6

这应该工作:

<svg 
    xmlns="http://www.w3.org/2000/svg"
    width="860"
    height="500"
    viewBox="0 0 860 500"
    preserveAspectRatio="xMinYMin meet">
Run Code Online (Sandbox Code Playgroud)