d3.js Map (<svg>) Auto Fit into Parent Container and Resize with Window

前端 未结 5 2206
时光取名叫无心
时光取名叫无心 2020-12-23 16:54

UPDATE: I have posted and accepted a fully working solution in the answers section. Any code in this section is to be used as reference for comparison to your own NO

5条回答
  •  梦毁少年i
    2020-12-23 17:33

    The best choice is to have a combined use of aspect ratio on normal definition of d3 graph's width and height. This has helped me in lot of my graph works.
    Step 1 : Dynamically get the height of the div to which the graph has to be appended.
    Step 2 : Declare width as aspect ratio with respect to the dynamically caught height.

    var graph_div = document.getElementById(graph.divId);
    graph.height = graph_div.clientHeight;
    graph.width = (960/1200)*graph.height;
    

提交回复
热议问题