views:

129

answers:

3

In the following picture:

alt text

I want to connect the boxes in the above with below, Let us call the bottom edge of the top boxes as A and top edge of the below boxes as B

Now, I have two arrays containing the points in the line A nd B say

A = [ {Ax1, Ay1},{Ax2, Ay2},.... ] and B = [ {Bx1, By1},{Bx2, By2},.... ]

In real world it can be like A = [ {100, 100},{120, 100},{140, 100},{160, 100}] and B=[ {120, 200},{140, 200},{160, 200},{180, 200},{200, 200},]

Please look at the black dots in the picture above

How can get the connectiong poins as shown in the pictures? Connecting point must be a close to the center of the line as possible.

Here is what I'm trying to get, but below functions draw line between the two matching points from the starting from the left of the both lines, Any suggessions

drawConnection : function(componentOut, componentIn, connectionKey) {
        var outDim     = $(componentOut).data('dim');
        var inDim      = $(componentIn).data('dim');
        var outPorts   = $(componentOut).data('ports');
        var inPorts    = $(componentIn).data('ports');
        var abovePorts = {}; 
        var belowPorts = {}; 
        var i = 0; 
        if(outDim.bottomLeft.y < inDim.topLeft.y){
            // Now proceed only if they can be connect with a single line
            if(outDim.bottomLeft.x < inDim.topRight.x && outDim.bottomRight.x>inDim.topLeft.x) {
                // Now get a proper connecting point
                abovePorts = outPorts.bottom; 
                belowPorts = inPorts.top;
                for(i=0; i<abovePorts.length; i++) {
                    for(j=0; j<belowPorts.length; j++) {
                        if(!abovePorts[i].inUse && !belowPorts[j].inUse && (abovePorts[i].x == belowPorts[j].x)){
                            console.debug("Drawing vertical lines between points ("+abovePorts[i].x+","+abovePorts[i].y+") and ("+abovePorts[i].x+","+belowPorts[j].y+")");
                            return true;
                        }
                    }
                }
            }
        }
        return false;
    },

-- Update

I'm exactly trying to get something similar to this http://raphaeljs.com/graffle.html, but the connections should be made with straight lines as shown below

alt text

+4  A: 

Have you tried Raphael.js : http://raphaeljs.com/ ?

Roki
never heard of this one, and it's awesome.
Peter Perháč
A: 

Another approach is to use the HTML+CSS engine of the browser, instead of using JS.

You can use a table.
One cell row for each box and a 2 cells row for the connector.
You color one of the border for the connector and use margin, float and width styles, to position the boxes.

I've already used this technique to draw org charts a long time ago... when IE6 was considered the best browser!

Mic
My boxes have to support DnD too!
Mithun P
You can make the original invisible while dragging, and redraw the new situation. But without the context it is rather difficult to imagine your need. Good luck !-)
Mic
A: 

Another worth looking at is Processing.js if you want a bit more power. I've used Raphael.js before and that was pretty easy to pickup and use. Just be aware that both utilize the Canvas element which to my knowledge isn't supported in all browsers yet.

Jason B-H
Diffrence is that From the documentation, Raphael uses SVG and is supported in IE6 too! The Processing.js runs in Firefox, Safari, Opera and Chrome but will not be supported in Internet Explorer until Mircosoft catch up with ISSUE 15.
Mithun P