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:
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
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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;
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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..?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
packageprefuse.demos;importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.Dimension;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.MouseEvent;importjava.awt.geom.Point2D;importjavax.swing.AbstractAction;importjavax.swing.BorderFactory;importjavax.swing.Box;importjavax.swing.BoxLayout;importjavax.swing.JComponent;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.KeyStroke;importjavax.swing.SwingConstants;importprefuse.Constants;importprefuse.Display;importprefuse.Visualization;importprefuse.action.Action;importprefuse.action.ActionList;importprefuse.action.ItemAction;importprefuse.action.RepaintAction;importprefuse.action.animate.ColorAnimator;importprefuse.action.animate.LocationAnimator;importprefuse.action.animate.QualityControlAnimator;importprefuse.action.animate.VisibilityAnimator;importprefuse.action.assignment.ColorAction;importprefuse.action.assignment.FontAction;importprefuse.action.filter.FisheyeTreeFilter;importprefuse.action.layout.CollapsedSubtreeLayout;importprefuse.action.layout.graph.NodeLinkTreeLayout;//importprefuse.action.layout.graph.TreeLayout;importprefuse.activity.SlowInSlowOutPacer;importprefuse.controls.ControlAdapter;importprefuse.controls.FocusControl;importprefuse.controls.PanControl;importprefuse.controls.WheelZoomControl;importprefuse.controls.ZoomControl;importprefuse.controls.ZoomToFitControl;importprefuse.data.Tree;importprefuse.data.Tuple;importprefuse.data.Node;importprefuse.data.event.TupleSetListener;importprefuse.data.io.TreeMLReader;importprefuse.data.search.PrefixSearchTupleSet;importprefuse.data.tuple.TupleSet;importprefuse.data.expression.Predicate;importprefuse.data.Graph;importprefuse.render.DefaultRendererFactory;importprefuse.render.EdgeRenderer;importprefuse.render.AbstractShapeRenderer;importprefuse.render.LabelRenderer;importprefuse.util.ColorLib;importprefuse.util.FontLib;importprefuse.util.ui.JFastLabel;importprefuse.util.ui.JSearchPanel;importprefuse.visual.VisualItem;importprefuse.visual.NodeItem;importprefuse.visual.expression.InGroupPredicate;importprefuse.visual.sort.TreeDepthItemSorter;importprefuse.action.assignment.ShapeAction;importprefuse.data.expression.parser.ExpressionParser;importjava.util.Iterator;importjava.util.LinkedList;/***Demonstrationofanode-linktreeviewer**@version1.0*@author<ahref="http://jheer.org">jeffreyheer</a>*/publicclassTreeView1extendsDisplay{publicstaticfinalStringTREE_CHI="/dummy_data.xml";privatestaticfinalStringtree="tree";privatestaticfinalStringtreeNodes="tree.nodes";privatestaticfinalStringtreeEdges="tree.edges";privateLabelRendererm_nodeRenderer;privateEdgeRendererm_edgeRenderer;privateStringm_label="label";privateintm_orientation=Constants.ORIENT_TOP_BOTTOM;staticNodem_root;publicTreeView1(Treet,Stringlabel){super(newVisualization());m_label=label;m_vis.add(tree,t);m_nodeRenderer=newLabelRenderer(m_label);m_nodeRenderer.setRenderType(AbstractShapeRenderer.RENDER_TYPE_FILL);m_nodeRenderer.setHorizontalAlignment(Constants.LEFT);m_nodeRenderer.setRoundedCorner(8,8);//m_nodeRenderer.m_edgeRenderer=newEdgeRenderer(Constants.EDGE_TYPE_CURVE);DefaultRendererFactoryrf=newDefaultRendererFactory(m_nodeRenderer);rf.add(newInGroupPredicate(treeEdges),m_edgeRenderer);m_vis.setRendererFactory(rf);//colorsItemActionnodeColor=newNodeColorAction(treeNodes);ItemActiontextColor=newColorAction(treeNodes,VisualItem.TEXTCOLOR,ColorLib.rgb(0,0,0));m_vis.putAction("textColor",textColor);ItemActionedgeColor=newColorAction(treeEdges,VisualItem.STROKECOLOR,ColorLib.rgb(200,200,200));//quickrepaintActionListrepaint=newActionList();repaint.add(nodeColor);repaint.add(newRepaintAction());m_vis.putAction("repaint",repaint);//fullpaintActionListfullPaint=newActionList();fullPaint.add(nodeColor);m_vis.putAction("fullPaint",fullPaint);ShapeActionshape=newShapeAction("canUrban",Constants.SHAPE_STAR);ItemActionshapetype=newShapeAction("canUrban",Constants.SHAPE_STAR);ActionListdraw=newActionList();draw.add(shape);m_vis.putAction("draw",draw);m_vis.run("filter");//animatepaintchangeActionListanimatePaint=newActionList(400);animatePaint.add(newColorAnimator(treeNodes));animatePaint.add(newRepaintAction());m_vis.putAction("animatePaint",animatePaint);//createthetreelayoutactionNodeLinkTreeLayouttreeLayout=newNodeLinkTreeLayout(tree,m_orientation,50,0,8);treeLayout.setLayoutAnchor(newPoint2D.Double(25,300));//m_root=t.getNode(1);//NodeItemni=(NodeItem)t.getNode(1);//treeLayout.setLayoutRoot(ni);/*Graphgraph=(Graph)m_vis.getGroup(tree);Predicatep=ExpressionParser.predicate("value='A'");Iteratoritems=m_vis.items("graph.nodes",p);VisualItemitem=(VisualItem)items.next();//nowcalculatethenewspanningtreeandruntheactionListafterwardsgraph.getSpanningTree((Node)item);m_vis.run("filter");*/m_vis.putAction("treeLayout",treeLayout);CollapsedSubtreeLayoutsubLayout=newCollapsedSubtreeLayout(tree,m_orientation);m_vis.putAction("subLayout",subLayout);AutoPanActionautoPan=newAutoPanAction();//createthefilteringandlayoutActionListfilter=newActionList();filter.add(newFisheyeTreeFilter(tree,10));filter.add(newFontAction(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");//animatedtransitionActionListanimate=newActionList(1000);animate.setPacingFunction(newSlowInSlowOutPacer());animate.add(autoPan);animate.add(newQualityControlAnimator());animate.add(newVisibilityAnimator(tree));animate.add(newLocationAnimator(treeNodes));animate.add(newColorAnimator(treeNodes));animate.add(newRepaintAction());m_vis.putAction("animate",animate);m_vis.alwaysRunAfter("filter","animate");//createanimatorfororientationchangesActionListorient=newActionList(2000);orient.setPacingFunction(newSlowInSlowOutPacer());orient.add(autoPan);orient.add(newQualityControlAnimator());orient.add(newLocationAnimator(treeNodes));orient.add(newRepaintAction());m_vis.putAction("orient",orient);//------------------------------------------------//initializethedisplaysetSize(700,600);setItemSorter(newTreeDepthItemSorter());addControlListener(newZoomToFitControl());addControlListener(newZoomControl());addControlListener(newWheelZoomControl());addControlListener(newPanControl());addControlListener(newFocusControl(1,"filter"));registerKeyboardAction(newOrientAction(Constants.ORIENT_LEFT_RIGHT),"left-to-right",KeyStroke.getKeyStroke("ctrl 1"),WHEN_FOCUSED);registerKeyboardAction(newOrientAction(Constants.ORIENT_TOP_BOTTOM),"top-to-bottom",KeyStroke.getKeyStroke("ctrl 2"),WHEN_FOCUSED);registerKeyboardAction(newOrientAction(Constants.ORIENT_RIGHT_LEFT),"right-to-left",KeyStroke.getKeyStroke("ctrl 3"),WHEN_FOCUSED);registerKeyboardAction(newOrientAction(Constants.ORIENT_BOTTOM_TOP),"bottom-to-top",KeyStroke.getKeyStroke("ctrl 4"),WHEN_FOCUSED);//------------------------------------------------//filtergraphandperformlayoutsetOrientation(m_orientation);m_vis.run("filter");TupleSetsearch=newPrefixSearchTupleSet();m_vis.addFocusGroup(Visualization.SEARCH_ITEMS,search);search.addTupleSetListener(newTupleSetListener(){publicvoidtupleSetChanged(TupleSett,Tuple[]add,Tuple[]rem){m_vis.cancel("animatePaint");m_vis.run("fullPaint");m_vis.run("animatePaint");}});}//------------------------------------------------------------------------publicvoidsetOrientation(intorientation){NodeLinkTreeLayoutrtl=(NodeLinkTreeLayout)m_vis.getAction("treeLayout");CollapsedSubtreeLayoutstl=(CollapsedSubtreeLayout)m_vis.getAction("subLayout");switch(orientation){caseConstants.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;caseConstants.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;caseConstants.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;caseConstants.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:thrownewIllegalArgumentException("Unrecognized orientation value: "+orientation);}m_orientation=orientation;rtl.setOrientation(orientation);stl.setOrientation(orientation);}publicintgetOrientation(){returnm_orientation;}//-----------Copynodedata----------publicstaticvoidcopyNodeData(Nodeorig,Nodedest){for(inti=0;i<orig.getColumnCount();i++){dest.set(i,orig.get(i));}}//-----------SubtreeCode------------//@SuppressWarnings("unchecked")publicstaticTreegetSubTree(Noderoot){Treesubtree=newTree();subtree.getNodeTable().addColumns(root.getSchema());//Onequeueforthenewnodes,onefortheoldonesLinkedList<Node>qNew=newLinkedList<Node>();LinkedList<Node>qOld=newLinkedList<Node>();NodenewFather=subtree.addRoot();copyNodeData(root,newFather);qOld.add(root);qNew.add(newFather);while(!qNew.isEmpty()){NodecurrentOld=qOld.removeFirst();NodecurrentNewFather=qNew.removeFirst();for(Iterator<Node>iter=currentOld.children();iter.hasNext();){NodeoldChild=iter.next();Noden=subtree.addChild(currentNewFather);copyNodeData(oldChild,n);qNew.add(n);qOld.add(oldChild);}}returnsubtree;}//------------------------------------------------------------------------publicstaticvoidmain(Stringargv[]){Stringinfile=TREE_CHI;Stringlabel="name";if(argv.length>1){infile=argv[0];label=argv[1];}JComponenttreeview=demo(infile,label);JFrameframe=newJFrame("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);}publicstaticJComponentdemo(){returndemo(TREE_CHI,"name");}publicstaticJComponentdemo(Stringdatafile,finalStringlabel){ColorBACKGROUND=Color.WHITE;ColorFOREGROUND=Color.BLACK;Treet=null;try{t=(Tree)newTreeMLReader().readGraph(datafile);m_root=t.getNode(4);t=getSubTree(m_root);//t=(Tree)newTreeMLReader().readGraph(datafile);}catch(Exceptione){e.printStackTrace();System.exit(1);}//createanewtreemapTreeView1tview=newTreeView1(t,label);//tview.m_root=t.getNode(1);//System.out.println(t.getNode(1).toString());////tview=newTreeView1(t.getSpanningTree(tview.m_root),label);tview.setBackground(BACKGROUND);tview.setForeground(FOREGROUND);ShapeActionshape=newShapeAction("canUrban",Constants.SHAPE_STAR);ItemActionshapetype=newShapeAction("canUrban",Constants.SHAPE_STAR);ActionListdraw=newActionList();draw.add(shape);tview.m_vis.putAction("draw",draw);tview.m_vis.run("draw");//createasearchpanelforthetreemapJSearchPanelsearch=newJSearchPanel(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);finalJFastLabeltitle=newJFastLabel(" ");title.setPreferredSize(newDimension(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(newControlAdapter(){publicvoiditemEntered(VisualItemitem,MouseEvente){if(item.canGetString(label))title.setText(item.getString(label));}publicvoiditemExited(VisualItemitem,MouseEvente){title.setText(null);}});Boxbox=newBox(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);JPanelpanel=newJPanel(newBorderLayout());panel.setBackground(BACKGROUND);panel.setForeground(FOREGROUND);panel.add(tview,BorderLayout.CENTER);panel.add(box,BorderLayout.SOUTH);returnpanel;}//------------------------------------------------------------------------publicclassOrientActionextendsAbstractAction{privateintorientation;publicOrientAction(intorientation){this.orientation=orientation;}publicvoidactionPerformed(ActionEventevt){setOrientation(orientation);getVisualization().cancel("orient");getVisualization().run("treeLayout");getVisualization().run("orient");}}publicclassAutoPanActionextendsAction{privatePoint2Dm_start=newPoint2D.Double();privatePoint2Dm_end=newPoint2D.Double();privatePoint2Dm_cur=newPoint2D.Double();privateintm_bias=150;publicvoidrun(doublefrac){TupleSetts=m_vis.getFocusGroup(Visualization.FOCUS_ITEMS);if(ts.getTupleCount()==0)return;if(frac==0.0){intxbias=0,ybias=0;switch(m_orientation){caseConstants.ORIENT_LEFT_RIGHT:xbias=m_bias;break;caseConstants.ORIENT_RIGHT_LEFT:xbias=-m_bias;break;caseConstants.ORIENT_TOP_BOTTOM:ybias=m_bias;break;caseConstants.ORIENT_BOTTOM_TOP:ybias=-m_bias;break;}VisualItemvi=(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);}}}publicstaticclassNodeColorActionextendsColorAction{publicNodeColorAction(Stringgroup){super(group,VisualItem.FILLCOLOR);}publicintgetColor(VisualItemitem){if(m_vis.isInGroup(item,Visualization.SEARCH_ITEMS))returnColorLib.rgb(255,190,190);elseif(m_vis.isInGroup(item,Visualization.FOCUS_ITEMS))returnColorLib.rgb(198,229,229);elseif(item.getDOI()>-1)returnColorLib.rgb(164,193,193);elsereturnColorLib.rgba(255,255,255,0);}}//endofinnerclassTreeMapColorAction}//endofclassTreeMap
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
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:
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
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..?
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?
The entire program for my requirement????? jajajaja no comments
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:
Any Idea on it?
sorry to trouble again..
can i use predicate to find the nodes.. say i want to show graph of 'b' so,
it is showing it cant find any node with this value….
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
this is my code: