Display a graphic tree
tree-visualization is a Go-based tool that transforms a hierarchical folder structure into a Graphviz DOT file and generates an image representation (e.g., PNG, SVG) via an API. The project is containerized using Docker for easy deployment across systems.
- Accepts a JSON input representing a folder structure with
id,display_name,parent_id,node_left, andnode_right. - Generates a Graphviz DOT representation of the folder hierarchy.
- Converts the DOT file into an image (e.g., PNG, SVG) using Graphviz.
- Provides a RESTful API endpoint for easy integration.
- Dockerized for portability—no local dependencies required.
tree-visualization/
├── Dockerfile
├── LICENSE
├── README.md
├── go.mod
├── go.sum
├── main.go
├── models
│ └── node.go
├── routers
│ └── api.go
├── test_case_1.png
├── test_case_2.png
├── test_case_3.png
└── utils
└── dot.go
- Docker (for containerized deployment)
- (Optional) Go 1.21+ and Graphviz (for local development)
- Using Docker
git clone https://github.com/henry0hai/tree-visualization.git
cd tree-visualization- Build the Docker image
docker build -t tree-visualization .- Run the container
docker run -p 8080:8080 tree-visualization- The API will be available at http://localhost:8080.
-
Install Go 1.23+ and Graphviz:
- Go: Official Go Installation
- Graphviz:
sudo apt install graphviz(Ubuntu) orbrew install graphviz(macOS)
-
Clone the repository and navigate to the project directory.
-
Install dependencies:
go mod tidy- Run the application
go run main.go- Endpoint:
POST /generate - Content-Type:
application/json - Request Body:
{
"nodes": [
{"id": 1, "display_name": "root", "parent_id": null, "node_left": 1, "node_right": 20},
{"id": 2, "display_name": "Documents", "parent_id": 1, "node_left": 2, "node_right": 3}
],
"format": "png"
}- Response: Binary image data (e.g., PNG, SVG) based on the specified format.
- Save the JSON to a file (e.g., input.json) and use curl:
curl -X POST -H "Content-Type: application/json" -d @input.json http://localhost:8080/generate --output output.pngBelow are sample inputs to test the application. Each represents a different folder structure.
{
"nodes": [
{"id": 1, "display_name": "root", "parent_id": null, "node_left": 1, "node_right": 20},
{"id": 2, "display_name": "Documents", "parent_id": 1, "node_left": 2, "node_right": 3},
{"id": 3, "display_name": "Videos", "parent_id": 1, "node_left": 4, "node_right": 5},
{"id": 4, "display_name": "Photos", "parent_id": 1, "node_left": 6, "node_right": 15},
{"id": 5, "display_name": "Workspace", "parent_id": 1, "node_left": 16, "node_right": 17},
{"id": 6, "display_name": "Personal", "parent_id": 1, "node_left": 18, "node_right": 19},
{"id": 7, "display_name": "Camera", "parent_id": 4, "node_left": 7, "node_right": 10},
{"id": 8, "display_name": "Demo_1", "parent_id": 7, "node_left": 8, "node_right": 9},
{"id": 9, "display_name": "Screenshot", "parent_id": 4, "node_left": 11, "node_right": 12}
],
"format": "svg"
}{
"nodes": [
{"id": 1, "display_name": "root", "parent_id": null, "node_left": 1, "node_right": 14},
{"id": 2, "display_name": "Projects", "parent_id": 1, "node_left": 2, "node_right": 13},
{"id": 3, "display_name": "Work", "parent_id": 2, "node_left": 3, "node_right": 12},
{"id": 4, "display_name": "2025", "parent_id": 3, "node_left": 4, "node_right": 11},
{"id": 5, "display_name": "Q1", "parent_id": 4, "node_left": 5, "node_right": 10},
{"id": 6, "display_name": "Reports", "parent_id": 5, "node_left": 6, "node_right": 7},
{"id": 7, "display_name": "Drafts", "parent_id": 5, "node_left": 8, "node_right": 9}
],
"format": "png"
}{
"nodes": [
{"id": 1, "display_name": "root", "parent_id": null, "node_left": 1, "node_right": 18},
{"id": 2, "display_name": "Home", "parent_id": 1, "node_left": 2, "node_right": 9},
{"id": 3, "display_name": "Work", "parent_id": 1, "node_left": 10, "node_right": 17},
{"id": 4, "display_name": "Photos", "parent_id": 2, "node_left": 3, "node_right": 6},
{"id": 5, "display_name": "Videos", "parent_id": 2, "node_left": 7, "node_right": 8},
{"id": 6, "display_name": "Family", "parent_id": 4, "node_left": 4, "node_right": 5},
{"id": 7, "display_name": "Tasks", "parent_id": 3, "node_left": 11, "node_right": 14},
{"id": 8, "display_name": "Meetings", "parent_id": 3, "node_left": 15, "node_right": 16},
{"id": 9, "display_name": "Urgent", "parent_id": 7, "node_left": 12, "node_right": 13}
],
"format": "png"
}- The
node_leftandnode_rightvalues follow the nested set model for hierarchical representation. - Supported image formats include
png,svg,jpg, etc., depending on Graphviz capabilities. - For local testing without Docker, ensure Graphviz is installed.
Feel free to submit issues or pull requests to enhance functionality, such as additional input formats or validation checks.
This project is licensed under the MIT License.


