Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java drawing graphs or trees in console

Does anyone know a mean to "render" plots or at least trees in console mode (draw it in the console)?

I would be able to render small plots at end of a very long process, by drawing some figures in ASCII mode, in order to have a geeky & fun view of some stats collected into the process.

I would be pleased to discover a library which does that, and I would keep the process 100% java, no shell-hack or third-party software.

-- EDIT

@lbalazscs and @Fortega made interesting answers, but the background of my question is to know if it exists such a library, and I will add some details I missed the first time :

The output should be able to display trees, binary trees (linked by @lbalazscs here), but also simple graphs such as bargraphs or so.

I will let this question "unanswered" for a while, and if there is no probant answer, @lbalazscs will get the point ;)

like image 585
Benj Avatar asked Jan 30 '13 09:01

Benj


People also ask

Can we draw graphs in Java?

In Java, plotting of graph is done by using several topics of core Java. For plotting, we use swing, awt, and awt. geom. We use Swing package to use Jlabel, JButtons, and JPanel in our program.

Does Java have a graph class?

The Java Graph class relies on an underlying Vertex class with the following behaviors: a constructor that sets data to the passed in inputData and sets edges to an empty ArrayList. an . addEdge() method that takes a vertex and weight and adds an edge to edges.

How do you represent a graph in Java?

Usually, we implement graphs in Java using HashMap collection. HashMap elements are in the form of key-value pairs. We can represent the graph adjacency list in a HashMap. A most common way to create a graph is by using one of the representations of graphs like adjacency matrix or adjacency list.


2 Answers

You can print ascii trees with minimal code. See the second answer to this question: How to print binary tree diagram?

(the second answer because this one is not only for binary trees)

like image 168
lbalazscs Avatar answered Oct 12 '22 23:10

lbalazscs


For people coming here looking for a pure Java tree drawing library: I recommend text-tree, which draws trees like this (and other styles, a lot of possible config if you need it):

some text
├─── more text
├─── and more
│    ├─── still more
│    ╰─── more
╰─── the end

Full disclosure: I am the author of text-tree.

like image 36
barfuin Avatar answered Oct 12 '22 23:10

barfuin