Pin (set as root) a node in an OrgChart

Pin (set as root) a node in an OrgChart

Pin (set as root) a node in an OrgChart: How to Pin a Node in an OrgChart with BALKAN OrgChart JS

How to Pin a Node in an OrgChart with BALKAN OrgChart JS

If you're building interactive organizational charts using BALKAN OrgChart JS, you may want to "pin" or fix a node as a root, so it stays visible or becomes the center of the visualization. This is especially helpful in large structures where users may want to focus on a specific department, person, or unit.

In this article, we’ll walk you through how to implement a “Set As Root” feature that pins a node at the top level of your org chart dynamically.

Code example

🧩 Demo Setup

Let’s start with a basic chart. Include the OrgChart JS library:

<script src="https://balkan.app/js/OrgChart.js"></script>
<div id="tree"></div>

Now we initialize the chart with a simple dataset:

let chart = new OrgChart(document.getElementById("tree"), {
    template: 'olivia',
    mode: 'dark',
    layout: OrgChart.tree,
    roots: [2, 3], // initially pinned nodes
    nodeBinding: {
        field_0: 'id',
        field_1: 'pid'
    },
    nodes: [
        { id: 1 },
        { id: 2, pid: 1 },
        { id: 3, pid: 1 },
        { id: 4, pid: 1 },
        { id: 5, pid: 2 },
        { id: 6, pid: 2 },
        { id: 7, pid: 2 },
        { id: 8, pid: 3 },
        { id: 9, pid: 3 },
        { id: 10, pid: 3 },
        { id: 11, pid: 3 },
        { id: 12, pid: 3 },
        { id: 13, pid: 3 },
        { id: 14, pid: 4 },
        { id: 15, pid: 4 },
        { id: 16, pid: 4 }
    ]
});


📌 Adding "Set As Root" Node Menu

To allow users to pin a node as a root, we use the built-in nodeMenu configuration. Here's how we add a custom option:

nodeMenu: {
    root: {
        icon: '', // you can add a custom icon here
        text: 'Set As Root',
        onClick: function (nodeId) {
            // Avoid duplicates
            if (chart.config.roots.indexOf(nodeId) === -1) {
                chart.config.roots.push(nodeId);
                chart.draw(); // Re-render the chart
            }
        }
    }
}

🛠️ What This Does:

  • When a user right-clicks a node, they see the "Set As Root" option.

  • On click, the node’s ID is added to the roots array (if not already present).

  • The chart is redrawn, making this node a new visible root.


🖼️ Visual Behavior

Let’s say you have a top-level executive (ID 1) and want users to focus on department heads (ID 2 and ID 3). This setup allows you to keep those visible at the top, regardless of their original hierarchy.

Users can dynamically promote any other node (like ID 4) as a root by right-clicking and selecting “Set As Root”.


✅ Conclusion

With just a few lines of code, you can make your org charts more dynamic by allowing users to pin any node as a root. This small UX feature makes a big difference in navigating large hierarchies.

Give it a try in your OrgChart app and make your organizational data more interactive!

Get roots and set as root - OrgChart JS

WordPress OrgChart Plugin

Family Tree in WordPress

Organizational Charts with Multiple Parents

Introducing new PDF and PNG Previews in OrgChart JS

Building an Org Chart MVP

Introducing PowerPoint Preview in OrgChart JS

Change The Order in the OrgChart by Drag and Drop (Simple Example)

Highlight to the root on search in OrgChart JS

OrgChart JS Now Supports Export to PowerPoint

OrgChart JS Now Supports Export to PowerPoint

Dynamically changing an orgchart field

Project Timeline Chart

OrgChart Conditional Node Size

Add CSS on Export in OrgChart JS

OrgChart JS Angular Templates

Org Chart Web App with Node.js and Express

Loading on Demand with Family Tree JS 2

Dynamic Colors Organizational Chart

Charlie Chaplin Family Tree

Introducing Family Tree JS 2: The Next Generation is Here!

React OrgChart

Create and run an OrgChart Single Page Application with Node.js – beginner's guide.

Export and Import Org Chart to Excel

Adding Custom HTML Elements into OrgChart JS container

Еasily Create a Template in an OrgChart

Node Swapping in an Organizational Chart

Vladimir Putin Family Tree

Add Field Data in Expand button with OrgChart JS

Bill Gates Family Tree

Create a Tooltip for a ForeignObject Element Hover in OrgChart JS

Custom Aligning Nodes in a JavaScript Organizational Chart

Center and Zoom on Search Click in OrgChart JS

Create Multiline Group Titles in OrgChart JS

Adding Arrows in OrgChart JS Links

Show a Custom Edit Form for a node in OrgChart JS

Jeff Bezos Family Tree

How to Add Material Icons in OrgChart JS

Leonardo DiCaprio Family Tree

Visualizing Dual Reporting Structures with OrgChart JS

Exporting OrgChart JS to A4 PDF: A Step-by-Step Guide

Genghis Khan Family Tree: The Lineage of the Great Khan

OrgChart JS now support Export to Visio

New Export Option: childLevels in OrgChart JS

Elon Musk Family Tree

AI for OrgChart JS: Revolutionizing Organization Charts

Family Tree JS 2 (Preview)

d3 org chart

Our Family Tree App is Now on Android!

Why BALKAN OrgChart JS is the Best Organizational Chart Library

How to Create a JavaScript Flow Chart

How to Add an Organizational Chart to a Mobile Application

How to Create a JavaScript Organizational Chart

OrgChart JS Now Supports PDF Export Per Team

Introducing Family Tree App

Donald Trump Family Tree

Discover Your Roots: Build Your Family Tree with BALKAN App

JavaScript Organization Chart

Instantly create interactive, intuitive flowcharts in seconds

JavaScript Hierarchy Chart

Mermaid js alternative

Create Flowcharts in Seconds with Flow Chart JS: Your New Favorite JavaScript Library

Customizing Filter UI - Code of The Week

Zoom Slider - Code of The Week

Bookmarks - Code of The Week

Customizing Search Results - Code of The Week

Introducing Undo Redo - Code Of The Week

Price adjustment announcement

The price will increase for new customers

Buttons for adding family members to a family tree - Code of The Week

Levels - Code of The Week

Change node color from a button in the Edit Form - Code of The Week

Filter and Highlight Nodes - Code of The Week

Up Expanding - Code Of The Week

Programmatically move the chart - Code of The Week

Siblings in Family Tree JS - Code of The Week

Purple template - Code of The Week

Assistant With Children - Code of The Week

Conditional Layout - Code of The Week

Conditional Color - Code of the Week

Sriniz Family Tree Template - Code of The Week

Color Picker - Code of The Week

Add company logo as watermark in exported PDF documents - Code of The Week

Mind map - Code of The Week

Create relationship with circle menu - Code of The Week

Animated Photos - Code of the Week

Grouped Dotted Lines - Code Of The Week

Dotted Lines - Code Of The Week

Department Dynamic Title - Code Of The Week

Add expand button for partners - Code of the Week

Change the sub levels with Drag and Drop - Code of The Week

Custom Edit From using Popover - Code of the Week

Sub levels tag option - Code of the Week

Highlights search results directly on Org Chart nodes - Code of the Week

Code of the Week/Relationship maps and Business process diagrams

Search using field name abbreviations - Code of the Week

Dynamic Template - Code of the Week

Read and Write local CSV file using File API and Org Chart JS - Code of The Week

Organizational Chart Path Highlighting and Selection

Modern Template - Org Chart JS

Performance - Org Chart JS

Single parent is supported in Family Tree JS

How To Develop a Flowchart Maker with Org Chart JS

How to upload a photo to Family Tree JS in .NET core

How to change the color of selected node - Family Tree JS

Family Tree with live tiles

Error

BALKAN Blog

The Latest BALKAN App News and Releases. Latest information on Org Chart JS and Family Tree JS.