简体   繁体   English

D3:如何处理单个图表中的缩放和工具提示?

[英]D3: How to handle zooming and tooltips in a single chart?

I have a visualization that is essentially a series of stacked bar charts, each of which contains several panels. 我有一个可视化,基本上是一系列堆叠条形图,每个条形图包含几个面板。 For example, here are three such bar charts, each with four panels. 例如, 这里有三个这样的条形图,每个条形图有四个面板。

I have managed to implement pan/zoom functionality that is coordinated across the charts. 我已经设法实现了跨图表协调的平移/缩放功能。 This is what it looks like if I zoom into the third panel from the last image, for example. 是什么样子,如果我放大到第三面板在最后的影像,例如。 The zoom behavior is called from an invisible rectangle that is placed over each chart. 从放置在每个图表上的不可见矩形调用缩放行为。

My problem is that I want to enable tooltip functionality based on the location of the user's cursor within a plot. 我的问题是我想根据用户光标在图中的位置启用工具提示功能。 Since the zoom-rectangles are placed on top of the charts, however, no mouse events are registered for any SVG elements in the actual charts themselves. 但是,由于缩放矩形放置在图表的顶部,因此实际图表本身中的任何SVG元素都没有注册鼠标事件。

Des anyone know a way around this? 有谁知道解决这个问题吗?

I was following Mike Bostock's example , and like you placing a rect across my whole chart and then calling the zoom behaviour on that, and like you found that it was consuming all the pointer events. 我正在关注Mike Bostock的例子 ,就像你在我的整个图表中放置一个rect ,然后在其上调用缩放行为,就像你发现它消耗了所有的指针事件一样。

I found an example here that seemed to be achieving what I wanted, and I found that if I scrap the rect and just call the zoom behaviour on the svg element directly , I still get pointer events for the child elements. 我在这里找到了一个似乎实现了我想要的例子 ,我发现如果我废弃rect并直接调用svg元素上的缩放行为 ,我仍然会得到子元素的指针事件。

I'm a noob here, I don't really understand why this works. 我在这里是个菜鸟,我真的不明白为什么会这样。 I also guess this might have its own ramifications eg I guess this stops you limiting the area of your graphic in which mouse events cause a zoom. 我也猜测这可能有它自己的后果,例如我猜这会阻止你限制鼠标事件导致缩放的图形区域。 You may notice that the example I linked creates a sub - svg ; 您可能会注意到我链接的示例创建了一个 svg ; I don't know, but perhaps this is to solve that problem. 我不知道,但也许这就是解决这个问题。

You can probably put the mouseevent on the same rectangle you are using for detecting your zoom. 您可以将mouseevent放在用于检测缩放的同一矩形上。 Hard to say for sure without a code sample, but I would expect you can do something along these lines: 没有代码示例很难说肯定,但我希望你可以按照以下方式做点什么:

 svg.select("rect.overlay")
     .on("mouseover.tooltip", function() { tooltip.style("display", null); })
     .on("mouseout.tooltip", function() { tooltip.style("display", "none"); })
     .on("mousemove.tooltip", mousemoveFunc);

Adding the ".tooltip" to the event adds a namespace to the event, so if you end up having any collision with your zoom listeners, you can add a namespace to them too. 将“.tooltip”添加到事件会为事件添加命名空间,因此如果最终与缩放侦听器发生冲突,您也可以向它们添加命名空间。

In your css put the style ponter-events:none for your zooming rectangles. 在你的css中为你的缩放矩形放置样式ponter-events:none。 That way the cursor events will be sensed by the elements blow. 这样,光标事件将被元素打击感知。

I know this is way too late, but I've just figured out a workaround. 我知道这太晚了,但我刚刚想出了一个解决方法。 For me the crucial thing is order of adding bits to the svg. 对我来说,关键的是向svg添加位的顺序。

svg1.append("rect")//put the rectangle for zoom events on the bottom
.attr("width", width)
.attr("height", height)
.style("fill", "none")
.style("pointer-events", "all")
.call(d3.zoom()
    .scaleExtent([0.5, 10])
    .on("zoom", zoomed));

var g = svg1.append("g"); //add g element for visualisation (above the rectangle).

function zoomed() { //zoom around the g's (g has to be before this, but after the rectangle)
g.attr("transform", d3.event.transform)  
}

Then a little bit later on add my force elements to the g 然后稍后将我的力元素添加到g

 var nodes = g.append("g")
    .attr("class", "nodes")
    .selectAll("circles")
    .attr('id', function(d) {
        return d.n_id
    })

etc. Slight issue here is I can't actually zoom with my mouse over the circles, but I have loads of white space. 这里的轻微问题是我实际上无法用鼠标在圆圈上进行缩放,但是我有很多空白区域。 I'm going to try and allow propagation of zoom events, or call zoom events from the circles 我将尝试允许传播缩放事件,或从圆圈调用缩放事件

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

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