How to create and run a Single Page Application that displays an org chart using a local JSON file as the data source.
Here's a step-by-step guide using vanilla JavaScript and a simple local development server.
✅ 1. Project Structure
orgchart-spa/
├── index.html
├── script.js
└── data.json
✅ 2. Contents of Each File
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Org Chart SPA</title>
<script src="https://cdn.balkan.app/orgchart.js"></script>
</head>
<body>
<div id="tree" style="width: 100%; height: 100vh;"></div>
<script src="script.js"></script>
</body>
</html>
script.js
fetch('data.json')
.then(response => response.json())
.then(data => {
var chart = new OrgChart(document.getElementById("tree"), {
mode: 'dark',
mouseScrool: OrgChart.action.scroll,
nodeBinding: {
field_0: "name"
}
});
chart.load(data);
})
.catch(error => console.error("Failed to load JSON:", error));
data.json
[
{ "id": 1, "name": "Denny Curtis" },
{ "id": 2, "pid": 1, "name": "Ashley Barnett" },
{ "id": 3, "pid": 1, "name": "Caden Ellison" }
]
✅ 3. Run Locally
Since you’re loading a local JSON file, you need a web server (due to CORS restrictions in browsers when opening HTML files directly).
Install it globally if you haven't already:
npm install -g http-server
Then:
http-server .
Happy charting! :)