简体   繁体   English

使D3图形响应

[英]Make D3 graphs responsive

I have a couple of graphs made with D3 but I have problems when I want to display them on small devices. 我有一些用D3制作的图形,但是当我想在小型设备上显示它们时遇到了问题。 They are cut off on left and right sides and I cannot see them properly. 它们在左右两侧被切断,我看不到它们。

I have no idea how to fix it, I have tried to change the dimensions of the canvas through the CSS when the screen size is small but I could not. 我不知道如何解决它,当屏幕尺寸很小但我无法通过CSS更改画布的尺寸时,我却无法解决。 I hope you can help me. 我希望你能帮助我。 I guess I have to introduce some change in the CSS and in the bit of code where I set the size of the graph. 我想我必须在CSS和设置图大小的一些代码中引入一些更改。

var margin = {top: 100, right: 111, bottom: 50, left: 120},
    width = 960 - margin.left - margin.right,
    height = 560 - margin.top - margin.bottom;


var svgbar = d3.select("#barforest").append("svg")
    .attr("width", width + margin.left + margin.right)
    .attr("height", height + margin.top + margin.bottom)
    .append("g")
    .attr("transform", "translate(" + margin.left + "," + margin.top + ")")

Thanks 谢谢

In your code, you have set the width, height and margins fixed. 在代码中,您已设置了宽度,高度和边距为固定值。 If you want your graph responsive, you have to register a resize event and redraw with appropriate width and height. 如果要让图形响应,则必须注册一个调整大小事件并以适当的宽度和高度重绘。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM