abego TreeLayout JavaFX Demo

Looking for API to help me play around with data,and restricting myself to java/javafx am not usually disappointed .
I came a cross the abego TreeLayout library a layout that displays hierarchical structure, checked the examples and i realized it was built in Swings  had to try this in javafx.

Kenya 2014-2015 Goverment Project Summary Funding Sources data here.

Kenya 2014-2015 Goverment Project Summary Funding Sources By Continent/Country 
The classes needed for this build were

DefaultConfiguration//sets the gap between levels and node,position of the root node
NodeExtentProvider//sets the size of the node height/width
TreeLayout//produces the results
DefaultTreeForTreeLayout//the tree

Building the Tree.

public TreeForTreeLayout<Text> createTree(){
     
        Text root=new Text("root");
     
        Text t1=new Text("t1");
        Text t2=new Text("t2");
        Text  t3=new Text("t3");
        Text t4=new Text("t4");

        DefaultTreeForTreeLayout<Text> tree = new DefaultTreeForTreeLayout<>(root);
        tree.addChild(root, t1);
        tree.addChild(root, t2);
        tree.addChild(t2, t3);
        tree.addChild(t3, t4);
     
        return tree;
    }
Configure Layout

 DefaultConfiguration<Text> configuration = new DefaultConfiguration<>(100, 45,Location.Bottom);

Set Node Width/Height

 NodeExtentProvider<Text> nodeExtentProvider = new NodeExtentProvider<Text>() {

              @Override
              public double getWidth(Text tn) {
                  return 25;
              }

              @Override
              public double getHeight(Text tn) {
                return 25;
              }
          };
     TreeLayout<Text> treeLayout = new TreeLayout<>(createTree(),nodeExtentProvider, configuration);

Drawing the Nodes

    for(Text t:treeLayout.getNodeBounds().keySet()){

          if(!treeLayout.getTree().isLeaf(t)){
           
              java.awt.geom.Rectangle2D.Double bounds=treeLayout.getNodeBounds().get(t);
              double x1 = bounds.getCenterX();
     double y1 = bounds.getCenterY();
           
              for(Text tx:treeLayout.getTree().getChildren(t)){

                 java.awt.geom.Rectangle2D.Double bound=treeLayout.getNodeBounds().get(tx);
                 Line line =new Line();
                 line.setStroke(Color.RED);
                 line.setStartX(x1);
                 line.setStartY(y1);
                 line.setEndX(bound.getCenterX());
                 line.setEndY(bound.getCenterY());
                 roots.getChildren().add(line);
               
              }
          }
          java.awt.geom.Rectangle2D.Double bounds=treeLayout.getNodeBounds().get(t);
          Rectangle rect=new Rectangle();
          rect.setWidth(30);
          rect.setHeight(30);
          rect.setFill(Color.color(Math.random(), Math.random(), Math.random()));
          rect.setEffect(dropShadow2);
          Text name=new Text(t.getText());
          name.setFill(rect.getFill());
          rect.setLayoutX(bounds.x);
          rect.setLayoutY(bounds.y);
          name.setLayoutX(bounds.x);
          name.setLayoutY(bounds.y);
          roots.getChildren().addAll(rect,name);
      }

Results


Comments

Popular Posts