简体   繁体   English

Javascript D3.js将图表从其svg元素中拖出

[英]Javascript D3.js drag a chart out of its svg Element

I want to make chart, that you can not drag out of its svg element. 我想制作图表,你不能拖出它的svg元素。 I'm doing this at the moment like this jsfiddle 我正在这样做,就像这个jsfiddle

As you can see, you can zoom and drag this freely. 如您所见,您可以自由缩放和拖动它。 What i want is this: If you drag it for example to the right and the y axis hits the edge of your screen on the left it should stop and not be able to be dragged anymore to the right. 我想要的是:如果你将它拖到右边,y轴击中左边的屏幕边缘,它应该停止,不能再向右拖动了。

Which also means, that you can't drag it around while not zoomed in, because it already fills its svg area. 这也意味着,你不能在没有放大的情况下拖动它,因为它已经填满了它的svg区域。

I guess i have to somehow restrict my redraw method. 我想我必须以某种方式限制我的重绘方法。 At the moment it's just this 目前就是这样

function redraw() {
    plotChart.attr("transform",
        "translate(" + d3.event.translate + ")"
        + " scale(" + d3.event.scale + ")");
};

It probably has to check if for example the left edge of the chart hits coordinate [0][x] and then somehow stop drawing it any further out. 它可能必须检查例如图表的左边缘是否达到坐标[0] [x],然后以某种方式再次停止绘制它。

To know the axis point on scaling and on translation you need to get the CTM and then transform to get the real points. 要知道缩放和平移时的轴点,您需要获得CTM,然后进行变换以获得真实点。

    //gets CTM on the first y line that is the group
    var ctm = d3.select(".x").select(".tick")[0][0].getCTM();
    //the first y line 
    var k = d3.select("svg")[0][0].createSVGPoint();
    //current point without transform
    k.x = d3.select(".x").select("line").attr("x2");
    k.y = d3.select(".x").select("line").attr("y2")

    j = k.matrixTransform(ctm)
    //j is the real point of the line post transform.
    //your code to zoom or pan depending on the value of j

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

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