예제 #1
0
    protected bool DrawGraph()
    {
        List <Branch> branchList = new List <Branch>();
        ArrayList     nodes      = new ArrayList();
        ArrayList     edges      = new ArrayList();

        if (!bd.SelectByWarehouseID(wid, ref branchList))
        {
            //error
            return(false);
        }

        //find master
        int masterBranchID = -1;
        int masterStart    = -1;

        foreach (Branch b in branchList)
        {
            if (b.branch_name == "master")
            {
                masterBranchID = b.branch_id;
                masterStart    = b.start_id;
                break;
            }
        }

        if (masterBranchID == -1)
        {
            //error
            return(false);
        }

        int curX   = 0;
        int curY   = 0;
        int edgeID = 0;

        //draw master
        Version v = new Version();

        v.version_id = masterStart;
        bool isFirst = true;

        while (v.version_id != 0)
        {
            if (!vd.SelectByID(ref v))
            {
                break;
            }
            Node n = new Node();
            n.vid   = v.version_id;
            n.lable = "master:" + n.vid.ToString();
            n.x     = curX;
            n.y     = curY;
            curX   += 10;
            nodes.Add(n);
            if (!isFirst)
            {
                Edge e = new Edge();
                e.src    = v.prev_id;
                e.dest   = v.version_id;
                e.edgeId = edgeID;
                edgeID++;
                edges.Add(e);
            }
            else
            {
                isFirst = false;
            }
            v.version_id = v.next_id;
        }

        //draw branches
        foreach (Branch b in branchList)
        {
            if (b.branch_id == masterBranchID)
            {
                continue;
            }
            for (int i = 0; i < nodes.Count; ++i)
            {
            }
            curX         = 0;
            curY        += 10;
            isFirst      = true;
            v.version_id = b.start_id;
            while (v.version_id != 0)
            {
                if (!vd.SelectByID(ref v))
                {
                    break;
                }
                if (isFirst)
                {
                    int i = 0;
                    for (; i < nodes.Count; ++i)
                    {
                        if (v.prev_id == ((Node)nodes[i]).vid)
                        {
                            curX = 10 * i;
                        }
                    }
                    isFirst = false;
                }

                Node n = new Node();
                n.vid   = v.version_id;
                n.lable = b.branch_name + ":" + n.vid.ToString();
                n.x     = curX;
                n.y     = curY;
                curX   += 10;
                nodes.Add(n);

                Edge e = new Edge();
                e.src    = v.prev_id;
                e.dest   = v.version_id;
                e.edgeId = edgeID;
                edgeID++;
                edges.Add(e);

                v.version_id = v.next_id;
            }
        }

        //js
        string js = "<script>var g = {nodes: [],edges: []};";

        foreach (Node n in nodes)
        {
            js += "g.nodes.push({id:'" + n.vid.ToString() + "',label:'" + n.lable + "',x:" + n.x.ToString() + ",y:" + n.y.ToString() + ",size:10,color:'#666'});";
        }

        foreach (Edge e in edges)
        {
            js += "g.edges.push({id:'" + e.edgeId.ToString() + "',source:'" + e.src.ToString() + "',target:'" + e.dest.ToString() + "',size:10,color: '#ccc',hover_color:'#000'}); ";
        }

        js += "s = new sigma({graph: g,renderer:{container: document.getElementById('graph-container'),type: 'canvas'},settings:{doubleClickEnabled: false,minEdgeSize: 0.5,maxEdgeSize: 4,enableEdgeHovering: true,edgeHoverColor: 'edge',defaultEdgeHoverColor: '#000',edgeHoverSizeRatio: 1,edgeHoverExtremities: true,}});";

        js += "s.bind('clickNode doubleClickNode rightClickNode', function (e) {var str = '' + e.data.node.label;var arr = str.split(':', 2);document.getElementById('selectedBranch').innerText = '选中分支:' + arr[0];document.getElementById('selectedVersion').innerText= arr[1];document.getElementById('hidValue').value = arr[1];});</script>";

        graph_script.InnerHtml = js;
        return(true);
    }