Subtree Not Generating

Help
sumit
2012-08-15
2013-05-29
  • sumit
    sumit
    2012-08-15

    Hi… Well i am a novice to prefuse and facing lots of difficulty in TreeView… i m able to generate a treeview with my xml which is  for network marketing company purpose. they dont want to show every agent the complete tree but just the subtree where they are the root. my xml is in simple format like:

    <?xml version="1.0" encoding="UTF-8"?>
    <tree>
    <declarations id="dec">
    <attributeDecl name="name" type="String" />
    </declarations>
    <branch id="A">
    <attribute name="name" value="A" />
    <branch id="b">
    <attribute name="name" value="b" />
    <branch id="d">
    <attribute name="name" value="d" />
    </branch>
    <branch id="e">
    <attribute name="name" value="e" />
    </branch>
    </branch>
    <branch id="c">
    <attribute name="name" value="c" />
    <branch id="f">
    <attribute name="name" value="f" />
    </branch>
    <branch id="g">
    <attribute name="name" value="g" />
    </branch>
    </branch>
    </branch>
    </tree>
    

    pls. help me out to generate subtree.. i tried with spanningtree but it is showing complete tree only with different directions.. Another problem is they want nodes in star shape and not rectangle which is again a trouble.. Hope to get reply soon… thanks

     
  • I built this function that gives you a subTree out of a node

         // @SuppressWarnings("unchecked")
         public static Tree getSubTree(Node root) {
             Tree subtree=new Tree();
             subtree.getNodeTable().addColumns(root.getSchema());
             //One queue for the new nodes, one for the old ones
             LinkedList<Node> qNew = new LinkedList<Node>();
             LinkedList<Node> qOld = new LinkedList<Node>();
             Node newFather=subtree.addRoot();
             copyNodeData(root,newFather);
             qOld.add(root);
             qNew.add(newFather);
             while ( !qNew.isEmpty() ) {
                 Node currentOld=qOld.removeFirst();
                 Node currentNewFather=qNew.removeFirst();
                 for (Iterator<Node> iter=currentOld.children(); iter.hasNext(); ) {
                     Node oldChild=iter.next();
                     Node n=subtree.addChild(currentNewFather);
                     copyNodeData(oldChild, n);
                     qNew.add(n);
                     qOld.add(oldChild);
                 }
             }
             return subtree;
         }
    
     
  • sumit
    sumit
    2012-08-16

    Hiii.. gr8 to hear from you. thanks for the code.. but now from where
    copyNodeData(oldChild, n) function came.. Can u give me the implementation of it..?

     
  • sumit
    sumit
    2012-08-16

    Is it possible to give me entire program for my requirement…?
    Thanks in anticipation of your positive response…

     
  • The copyNodeData function? sure it's $500, hehehe  just kidding here it is?

        public static void copyNodeData(Node orig, Node dest) {
            for (int i=0;i<orig.getColumnCount();i++) {         
                dest.set(i, orig.get(i));
            }
        }
    

    The entire program for my requirement????? jajajaja no comments

     
  • sumit
    sumit
    2012-08-16

    hahaha… Thanx john for the code… i scared after seeing $500.. Heyyyyyyy!!!!!!! Hurrah it worked for me…. John now just a small favor can u tell me how to bring star shap instead of rectangle in my graph.. i tried with this but its not working:

            ShapeAction shape = new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ItemAction shapetype= new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ActionList draw = new ActionList();
            draw.add(shape);
            m_vis.putAction("draw", draw);
            filter.add(shapetype);
            m_vis.putAction("filter", filter);
            m_vis.run("filter");
    

    Any Idea on it?

     
  • sumit
    sumit
    2012-08-16

    sorry to trouble again..
    can i use predicate to find the nodes.. say i want to show graph of 'b' so,

            Graph graph = (Graph)m_vis.getGroup(tree);
            Predicate p = ExpressionParser.predicate("value='A'");
    

    it is showing it cant find any node with this value….

     
  • Björn Kruse
    Björn Kruse
    2012-08-16

    Hi,

    regarding the stars, do you use a ShapeRenderer?

    For the expression, the last example of this page may help?
    http://home.arcor.de/gutn8/prefuse/

    best regards, Björn

     
  • sumit
    sumit
    2012-08-17

    this is my code:

    package prefuse.demos;
    import java.awt.BorderLayout;
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.Font;
    import java.awt.event.ActionEvent;
    import java.awt.event.MouseEvent;
    import java.awt.geom.Point2D;
    import javax.swing.AbstractAction;
    import javax.swing.BorderFactory;
    import javax.swing.Box;
    import javax.swing.BoxLayout;
    import javax.swing.JComponent;
    import javax.swing.JFrame;
    import javax.swing.JPanel;
    import javax.swing.KeyStroke;
    import javax.swing.SwingConstants;
    import prefuse.Constants;
    import prefuse.Display;
    import prefuse.Visualization;
    import prefuse.action.Action;
    import prefuse.action.ActionList;
    import prefuse.action.ItemAction;
    import prefuse.action.RepaintAction;
    import prefuse.action.animate.ColorAnimator;
    import prefuse.action.animate.LocationAnimator;
    import prefuse.action.animate.QualityControlAnimator;
    import prefuse.action.animate.VisibilityAnimator;
    import prefuse.action.assignment.ColorAction;
    import prefuse.action.assignment.FontAction;
    import prefuse.action.filter.FisheyeTreeFilter;
    import prefuse.action.layout.CollapsedSubtreeLayout;
    import prefuse.action.layout.graph.NodeLinkTreeLayout;
    //import prefuse.action.layout.graph.TreeLayout;
    import prefuse.activity.SlowInSlowOutPacer;
    import prefuse.controls.ControlAdapter;
    import prefuse.controls.FocusControl;
    import prefuse.controls.PanControl;
    import prefuse.controls.WheelZoomControl;
    import prefuse.controls.ZoomControl;
    import prefuse.controls.ZoomToFitControl;
    import prefuse.data.Tree;
    import prefuse.data.Tuple;
    import prefuse.data.Node;
    import prefuse.data.event.TupleSetListener;
    import prefuse.data.io.TreeMLReader;
    import prefuse.data.search.PrefixSearchTupleSet;
    import prefuse.data.tuple.TupleSet;
    import prefuse.data.expression.Predicate;
    import prefuse.data.Graph;
    import prefuse.render.DefaultRendererFactory;
    import prefuse.render.EdgeRenderer;
    import prefuse.render.AbstractShapeRenderer;
    import prefuse.render.LabelRenderer;
    import prefuse.util.ColorLib;
    import prefuse.util.FontLib;
    import prefuse.util.ui.JFastLabel;
    import prefuse.util.ui.JSearchPanel;
    import prefuse.visual.VisualItem;
    import prefuse.visual.NodeItem;
    import prefuse.visual.expression.InGroupPredicate;
    import prefuse.visual.sort.TreeDepthItemSorter;
    import prefuse.action.assignment.ShapeAction;
    import prefuse.data.expression.parser.ExpressionParser;
    import java.util.Iterator;
    import java.util.LinkedList;
    /**
     * Demonstration of a node-link tree viewer
     *
     * @version 1.0
     * @author <a href="http://jheer.org">jeffrey heer</a>
     */
    public class TreeView1 extends Display {
        public static final String TREE_CHI = "/dummy_data.xml";
        
        private static final String tree = "tree";
        private static final String treeNodes = "tree.nodes";
        private static final String treeEdges = "tree.edges";
        
        private LabelRenderer m_nodeRenderer;
        private EdgeRenderer m_edgeRenderer;
        
        private String m_label = "label";
        private int m_orientation = Constants.ORIENT_TOP_BOTTOM;
        
        static Node m_root;
        
        public TreeView1(Tree t, String label) {
            super(new Visualization());
            m_label = label;
            m_vis.add(tree, t);
            
            m_nodeRenderer = new LabelRenderer(m_label);
            m_nodeRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_FILL);
            m_nodeRenderer.setHorizontalAlignment(Constants.LEFT);
            m_nodeRenderer.setRoundedCorner(8,8);
          //  m_nodeRenderer.
            m_edgeRenderer = new EdgeRenderer(Constants.EDGE_TYPE_CURVE);
            
            DefaultRendererFactory rf = new DefaultRendererFactory(m_nodeRenderer);
            rf.add(new InGroupPredicate(treeEdges), m_edgeRenderer);
           m_vis.setRendererFactory(rf);
                   
            // colors
            ItemAction nodeColor = new NodeColorAction(treeNodes);
            ItemAction textColor = new ColorAction(treeNodes,
                    VisualItem.TEXTCOLOR, ColorLib.rgb(0,0,0));
            m_vis.putAction("textColor", textColor);
            
            ItemAction edgeColor = new ColorAction(treeEdges,
                    VisualItem.STROKECOLOR, ColorLib.rgb(200,200,200));
            
            // quick repaint
            ActionList repaint = new ActionList();
            repaint.add(nodeColor);
            repaint.add(new RepaintAction());
            m_vis.putAction("repaint", repaint);
            
            // full paint
            ActionList fullPaint = new ActionList();
            fullPaint.add(nodeColor);
            m_vis.putAction("fullPaint", fullPaint);
            ShapeAction shape = new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ItemAction shapetype= new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ActionList draw = new ActionList();
            draw.add(shape);
            m_vis.putAction("draw", draw);
            m_vis.run("filter");
            // animate paint change
            ActionList animatePaint = new ActionList(400);
            animatePaint.add(new ColorAnimator(treeNodes));
            animatePaint.add(new RepaintAction());
            
            m_vis.putAction("animatePaint", animatePaint);
            
            
            // create the tree layout action
            NodeLinkTreeLayout treeLayout = new NodeLinkTreeLayout(tree,
                    m_orientation, 50, 0, 8);
            treeLayout.setLayoutAnchor(new Point2D.Double(25,300));
            
            //m_root=t.getNode(1);
           // NodeItem ni=(NodeItem)t.getNode(1);
           // treeLayout.setLayoutRoot(ni);
            
            
    /*        
            Graph graph = (Graph)m_vis.getGroup(tree);
            Predicate p = ExpressionParser.predicate("value='A'");
            Iterator items = m_vis.items("graph.nodes", p);
            VisualItem item = (VisualItem) items.next();
          //now calculate the new spanningtree and run the actionList afterwards
            graph.getSpanningTree((Node) item);
            m_vis.run("filter");
    */
            
            
            
                    
            m_vis.putAction("treeLayout", treeLayout);
            
            CollapsedSubtreeLayout subLayout = 
                new CollapsedSubtreeLayout(tree, m_orientation);
            m_vis.putAction("subLayout", subLayout);
            
            AutoPanAction autoPan = new AutoPanAction();
            
            // create the filtering and layout
            ActionList filter = new ActionList();
            filter.add(new FisheyeTreeFilter(tree, 10));
            filter.add(new FontAction(treeNodes, FontLib.getFont("Tahoma", 16)));
            filter.add(treeLayout);
            filter.add(subLayout);
            filter.add(textColor);
            filter.add(nodeColor);
            filter.add(edgeColor);
            filter.add(shapetype);
            m_vis.putAction("filter", filter);
            m_vis.run("filter");
            // animated transition
            ActionList animate = new ActionList(1000);
            animate.setPacingFunction(new SlowInSlowOutPacer());
            animate.add(autoPan);
            animate.add(new QualityControlAnimator());
            animate.add(new VisibilityAnimator(tree));
            animate.add(new LocationAnimator(treeNodes));
            animate.add(new ColorAnimator(treeNodes));
            animate.add(new RepaintAction());
            m_vis.putAction("animate", animate);
            m_vis.alwaysRunAfter("filter", "animate");
            
            // create animator for orientation changes
            ActionList orient = new ActionList(2000);
            orient.setPacingFunction(new SlowInSlowOutPacer());
            orient.add(autoPan);
            orient.add(new QualityControlAnimator());
            orient.add(new LocationAnimator(treeNodes));
            orient.add(new RepaintAction());
            m_vis.putAction("orient", orient);
            
            // ------------------------------------------------
            
            // initialize the display
            setSize(700,600);
            setItemSorter(new TreeDepthItemSorter());
            addControlListener(new ZoomToFitControl());
            addControlListener(new ZoomControl());
            addControlListener(new WheelZoomControl());
            addControlListener(new PanControl());
            addControlListener(new FocusControl(1, "filter"));
            
            registerKeyboardAction(
                new OrientAction(Constants.ORIENT_LEFT_RIGHT),
                "left-to-right", KeyStroke.getKeyStroke("ctrl 1"), WHEN_FOCUSED);
            registerKeyboardAction(
                new OrientAction(Constants.ORIENT_TOP_BOTTOM),
                "top-to-bottom", KeyStroke.getKeyStroke("ctrl 2"), WHEN_FOCUSED);
            registerKeyboardAction(
                new OrientAction(Constants.ORIENT_RIGHT_LEFT),
                "right-to-left", KeyStroke.getKeyStroke("ctrl 3"), WHEN_FOCUSED);
            registerKeyboardAction(
                new OrientAction(Constants.ORIENT_BOTTOM_TOP),
                "bottom-to-top", KeyStroke.getKeyStroke("ctrl 4"), WHEN_FOCUSED);
            
            // ------------------------------------------------
            
            // filter graph and perform layout
            setOrientation(m_orientation);
            m_vis.run("filter");
            
            
            
            TupleSet search = new PrefixSearchTupleSet(); 
            m_vis.addFocusGroup(Visualization.SEARCH_ITEMS, search);
            search.addTupleSetListener(new TupleSetListener() {
                public void tupleSetChanged(TupleSet t, Tuple[] add, Tuple[] rem) {
                    m_vis.cancel("animatePaint");
                    m_vis.run("fullPaint");
                    m_vis.run("animatePaint");
                }
            });
        }
        
        // ------------------------------------------------------------------------
        
        public void setOrientation(int orientation) {
            NodeLinkTreeLayout rtl 
                = (NodeLinkTreeLayout)m_vis.getAction("treeLayout");
            CollapsedSubtreeLayout stl
                = (CollapsedSubtreeLayout)m_vis.getAction("subLayout");
            switch ( orientation ) {
            case Constants.ORIENT_LEFT_RIGHT:
                m_nodeRenderer.setHorizontalAlignment(Constants.LEFT);
                m_edgeRenderer.setHorizontalAlignment1(Constants.RIGHT);
                m_edgeRenderer.setHorizontalAlignment2(Constants.LEFT);
                m_edgeRenderer.setVerticalAlignment1(Constants.CENTER);
                m_edgeRenderer.setVerticalAlignment2(Constants.CENTER);
                break;
            case Constants.ORIENT_RIGHT_LEFT:
                m_nodeRenderer.setHorizontalAlignment(Constants.RIGHT);
                m_edgeRenderer.setHorizontalAlignment1(Constants.LEFT);
                m_edgeRenderer.setHorizontalAlignment2(Constants.RIGHT);
                m_edgeRenderer.setVerticalAlignment1(Constants.CENTER);
                m_edgeRenderer.setVerticalAlignment2(Constants.CENTER);
                break;
            case Constants.ORIENT_TOP_BOTTOM:
                m_nodeRenderer.setHorizontalAlignment(Constants.CENTER);
                m_edgeRenderer.setHorizontalAlignment1(Constants.CENTER);
                m_edgeRenderer.setHorizontalAlignment2(Constants.CENTER);
                m_edgeRenderer.setVerticalAlignment1(Constants.BOTTOM);
                m_edgeRenderer.setVerticalAlignment2(Constants.TOP);
                break;
            case Constants.ORIENT_BOTTOM_TOP:
                m_nodeRenderer.setHorizontalAlignment(Constants.CENTER);
                m_edgeRenderer.setHorizontalAlignment1(Constants.CENTER);
                m_edgeRenderer.setHorizontalAlignment2(Constants.CENTER);
                m_edgeRenderer.setVerticalAlignment1(Constants.TOP);
                m_edgeRenderer.setVerticalAlignment2(Constants.BOTTOM);
                break;
            default:
                throw new IllegalArgumentException(
                    "Unrecognized orientation value: "+orientation);
            }
            m_orientation = orientation;
            rtl.setOrientation(orientation);
            stl.setOrientation(orientation);
        }
        
        public int getOrientation() {
            return m_orientation;
        }
        
        //-----------Copynodedata----------
        
        public static void copyNodeData(Node orig, Node dest) {
            for (int i=0;i<orig.getColumnCount();i++) {         
                dest.set(i, orig.get(i));
            }
        }
        
        //----------- Subtree Code ------------
        
         // @SuppressWarnings("unchecked")
         public static Tree getSubTree(Node root) {
             Tree subtree=new Tree();
             subtree.getNodeTable().addColumns(root.getSchema());
             //One queue for the new nodes, one for the old ones
             LinkedList<Node> qNew = new LinkedList<Node>();
             LinkedList<Node> qOld = new LinkedList<Node>();
             Node newFather=subtree.addRoot();
             copyNodeData(root,newFather);
             qOld.add(root);
             qNew.add(newFather);
             while ( !qNew.isEmpty() ) {
                 Node currentOld=qOld.removeFirst();
                 Node currentNewFather=qNew.removeFirst();
                 for (Iterator<Node> iter=currentOld.children(); iter.hasNext(); ) {
                     Node oldChild=iter.next();
                     Node n=subtree.addChild(currentNewFather);
                     copyNodeData(oldChild, n);
                     qNew.add(n);
                     qOld.add(oldChild);
                 }
             }
             return subtree;
         }
        
        
        
        
        // ------------------------------------------------------------------------
        
        public static void main(String argv[]) {
            String infile = TREE_CHI;
            String label = "name";
            if ( argv.length > 1 ) {
                infile = argv[0];
                label = argv[1];
            }
            JComponent treeview = demo(infile, label);
            
            JFrame frame = new JFrame("p r e f u s e  |  t r e e v i e w");
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setContentPane(treeview);
            frame.pack();
            frame.setVisible(true);
        }
        
        public static JComponent demo() {
            return demo(TREE_CHI, "name");
        }
        
        public static JComponent demo(String datafile, final String label) {
            Color BACKGROUND = Color.WHITE;
            Color FOREGROUND = Color.BLACK;
            
            Tree t = null;
           
            try {
                t = (Tree)new TreeMLReader().readGraph(datafile);
                m_root=t.getNode(4);
                t = getSubTree(m_root);         
    //            t = (Tree)new TreeMLReader().readGraph(datafile);
            } catch ( Exception e ) {
                e.printStackTrace();
                System.exit(1);
            }
            
            // create a new treemap
            
            TreeView1 tview = new TreeView1(t, label);
    //        tview.m_root=t.getNode(1);
     //      System.out.println(t.getNode(1).toString());
     //       
     //       tview=new TreeView1(t.getSpanningTree(tview.m_root),label);
            tview.setBackground(BACKGROUND);
            tview.setForeground(FOREGROUND);
            
            ShapeAction shape = new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ItemAction shapetype= new ShapeAction("canUrban", Constants.SHAPE_STAR);
            ActionList draw = new ActionList();
            draw.add(shape);
            tview.m_vis.putAction("draw", draw);
            tview.m_vis.run("draw");
                  
            // create a search panel for the tree map
            JSearchPanel search = new JSearchPanel(tview.getVisualization(),
                treeNodes, Visualization.SEARCH_ITEMS, label, true, true);
            search.setShowResultCount(true);
            search.setBorder(BorderFactory.createEmptyBorder(5,5,4,0));
            search.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 11));
            search.setBackground(BACKGROUND);
            search.setForeground(FOREGROUND);
            
            final JFastLabel title = new JFastLabel("                 ");
            title.setPreferredSize(new Dimension(350, 20));
            title.setVerticalAlignment(SwingConstants.BOTTOM);
            title.setBorder(BorderFactory.createEmptyBorder(3,0,0,0));
            title.setFont(FontLib.getFont("Tahoma", Font.PLAIN, 16));
            title.setBackground(BACKGROUND);
            title.setForeground(FOREGROUND);
            
            tview.addControlListener(new ControlAdapter() {
                public void itemEntered(VisualItem item, MouseEvent e) {
                    if ( item.canGetString(label) )
                        title.setText(item.getString(label));
                }
                public void itemExited(VisualItem item, MouseEvent e) {
                    title.setText(null);
                }
            });
            
            
            Box box = new Box(BoxLayout.X_AXIS);
            box.add(Box.createHorizontalStrut(10));
            box.add(title);
            box.add(Box.createHorizontalGlue());
            box.add(search);
            box.add(Box.createHorizontalStrut(3));
            box.setBackground(BACKGROUND);
            
            JPanel panel = new JPanel(new BorderLayout());
            panel.setBackground(BACKGROUND);
            panel.setForeground(FOREGROUND);
            panel.add(tview, BorderLayout.CENTER);
            panel.add(box, BorderLayout.SOUTH);
            return panel;
        }
        
        // ------------------------------------------------------------------------
       
        public class OrientAction extends AbstractAction {
            private int orientation;
            
            public OrientAction(int orientation) {
                this.orientation = orientation;
            }
            public void actionPerformed(ActionEvent evt) {
                setOrientation(orientation);
                getVisualization().cancel("orient");
                getVisualization().run("treeLayout");
                getVisualization().run("orient");
            }
        }
        
        public class AutoPanAction extends Action {
            private Point2D m_start = new Point2D.Double();
            private Point2D m_end   = new Point2D.Double();
            private Point2D m_cur   = new Point2D.Double();
            private int     m_bias  = 150;
            
            public void run(double frac) {
                TupleSet ts = m_vis.getFocusGroup(Visualization.FOCUS_ITEMS);
                if ( ts.getTupleCount() == 0 )
                    return;
                
                if ( frac == 0.0 ) {
                    int xbias=0, ybias=0;
                    switch ( m_orientation ) {
                    case Constants.ORIENT_LEFT_RIGHT:
                        xbias = m_bias;
                        break;
                    case Constants.ORIENT_RIGHT_LEFT:
                        xbias = -m_bias;
                        break;
                    case Constants.ORIENT_TOP_BOTTOM:
                        ybias = m_bias;
                        break;
                    case Constants.ORIENT_BOTTOM_TOP:
                        ybias = -m_bias;
                        break;
                    }
                    VisualItem vi = (VisualItem)ts.tuples().next();
                    m_cur.setLocation(getWidth()/2, getHeight()/2);
                    getAbsoluteCoordinate(m_cur, m_start);
                    m_end.setLocation(vi.getX()+xbias, vi.getY()+ybias);
                } else {
                    m_cur.setLocation(m_start.getX() + frac*(m_end.getX()-m_start.getX()),
                                      m_start.getY() + frac*(m_end.getY()-m_start.getY()));
                    panToAbs(m_cur);
                }
            }
        }
        
        public static class NodeColorAction extends ColorAction {
            
            public NodeColorAction(String group) {
                super(group, VisualItem.FILLCOLOR);
            }
            
            public int getColor(VisualItem item) {
                if ( m_vis.isInGroup(item, Visualization.SEARCH_ITEMS) )
                    return ColorLib.rgb(255,190,190);
                else if ( m_vis.isInGroup(item, Visualization.FOCUS_ITEMS) )
                    return ColorLib.rgb(198,229,229);
                else if ( item.getDOI() > -1 )
                    return ColorLib.rgb(164,193,193);
                else
                    return ColorLib.rgba(255,255,255,0);
            }
            
        } // end of inner class TreeMapColorAction
        
        
    } // end of class TreeMap