oss-bandb / GraphView

Android GraphView is used to display data in graph structures.
Apache License 2.0
1.04k stars 129 forks source link

Display a family tree structure #76

Closed M144-Coder closed 3 years ago

M144-Coder commented 4 years ago

I am trying to display a family tree structure using this great library. But I am stuck at looping through the tree and adding it to the graph.

This is the tree class which holds the data of the family tree Inspired from this answer.

public class PersonTree implements Serializable {

    private Person data;

    private PersonTree parent;

    private ArrayList<PersonTree> children;

    public PersonTree() {
    }

    public Person getData() {
        return data;
    }

    public PersonTree getParent() {
        return parent;
    }

    public ArrayList<PersonTree> getChildren() {
        return children;
    }

    public boolean isRoot() {
        return parent == null;
    }

    public boolean isLeaf() {
        return children == null || children.isEmpty();
    }

    public int getLevel() {
        return isRoot() ? 0 : parent.getLevel() + 1;
    }
}

And this is the Person class which holds the person data:

public class Person implements Serializable {

    private String id;

    private String name;

    private Date birthdate;

    private String fatherId;

    private String fatherName;

    public Person() {
    }

    public String getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public Date getBirthdate() {
        return birthdate;
    }

    public String getFatherId() {
    return fatherId;
    }

    public String getFatherName() {
        return fatherName;
    }
}

I have constructed the graph view in the same way in the library example, but I am stuck at adding the nodes to the graph, here is my code:

public class GraphActivity extends AppCompatActivity {

    PersonTree familyTree;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        GraphView graphView = findViewById(R.id.graph);

        final Graph graph = new Graph();

        //How to loop through the tree and add children to the graph

        final Node node1 = new Node(parent.getName());
        final Node node2 = new Node(child1.getName());
        final Node node3 = new Node(child2.getName());

        graph.addEdge(node1, node2);
        graph.addEdge(node1, node3);

        GraphAdapter adapter = new GraphAdapter<GraphView.ViewHolder>(graph) {

            @NonNull
            @Override
            public GraphView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                final View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.node, parent, false);
                return new SimpleViewHolder(view);
            }

            @Override
            public void onBindViewHolder(GraphView.ViewHolder viewHolder, Object data, int position) {
                ((SimpleViewHolder) viewHolder).textView.setText((String)data);
            }
        };
        graphView.setAdapter(adapter);

        final BuchheimWalkerConfiguration configuration = new BuchheimWalkerConfiguration.Builder()
                .setSiblingSeparation(100)
                .setLevelSeparation(300)
                .setSubtreeSeparation(300)
                .setOrientation(BuchheimWalkerConfiguration.ORIENTATION_TOP_BOTTOM)
                .build();

        graphView.setLayout(new BuchheimWalkerAlgorithm(configuration));
    }
}

Thanks for help.

DennisBlock commented 3 years ago

You need a recursive function, where you iterate through the current children and create a new edge between its parent und the child itself. Just google "recursive function" and you find many examples.

GregorBlock commented 3 years ago

I'm closing this issue due to inactivity. If you have further input on the issue, don't hesitate to reopen this issue or post a new one.