简体   繁体   English

在同一网页上调整多个SVG图的大小

[英]Resizing more than one svg graph on the same web page

I have written functions using d3.js to create SVG graphs.The graphs get resized when the window is resized. 我已经使用d3.js编写了创建SVG图的函数。当调整窗口大小时,图的大小也会调整 The (simplified) software pattern is like this: (简化的)软件模式如下所示:

function drawGraph(DIV, data){
// Draw the initial graph, which fills the supplied DIV

    function resize {
    // Read the new width and height of the SVG, redraw the graph
    }

    d3.select(window).on('resize', resize);
    resize();
}

An excellent example of this pattern can for example be seen here: https://blog.safaribooksonline.com/2014/02/17/building-responsible-visualizations-d3-js/ 例如,可以在以下示例中找到这种模式的一个很好的例子: https : //blog.safaribooksonline.com/2014/02/17/building-responsible-visualizations-d3-js/

This is my problem: I call the routine twice and create two SVG graphs on the same web page. 这是我的问题:我两次调用例程,并在同一网页上创建两个SVG图形。 When the window is resized, ONLY the last SVG drawn gets resized. 调整窗口大小时,仅最后绘制的SVG会调整大小。 How can I keep the first one "alive", so it will listen to the resize event? 我如何保持第一个“活动”,以便它监听resize事件?

See the documentation here : 请参阅此处的文档:

If an event listener was already registered for the same type on the selected element, the existing listener is removed before the new listener is added. 如果已在选定元素上为同一类型注册了事件侦听器,则在添加新侦听器之前,将删除现有侦听器。 To register multiple listeners for the same event type, the type may be followed by an optional namespace, such as "click.foo" and "click.bar". 要为同一事件类型注册多个侦听器,该类型后可以跟一个可选的名称空间,例如“ click.foo”和“ click.bar”。 To remove a listener, pass null as the listener. 要删除侦听器,请将null用作侦听器。 To remove all listeners for a particular event type, pass null as the listener, and .type as the type, eg selection.on(".foo", null). 要删除特定事件类型的所有侦听器,请传递null作为侦听器,并传递.type作为类型,例如selection.on(“。foo”,null)。

So assuming DIV is a string identifier: 因此,假设DIV是字符串标识符:

d3.select(window).on('resize.'+DIV, resize);

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

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