Adding Custom HTML Elements into OrgChart JS container

Adding Custom HTML Elements into OrgChart JS container

Adding Custom HTML Elements into OrgChart JS container: Enhance Your OrgChart.js Adding Custom HTML Elements - Titles, Legends, and More!

Enhance Your OrgChart.js Adding Custom HTML Elements - Titles, Legends, and More!

Code example

When working with the OrgChart JS library, it's not uncommon to want to personalize the chart beyond node customization. Maybe you want a title, a legend, or even a bit of branding tucked neatly into the chart container. Luckily, OrgChart provides enough flexibility to make this easy.

In this guide, we'll walk through how to dynamically inject custom HTML (like a title and a color-coded legend) into your chart container and also include that same HTML in exported outputs like PDF.


🛠️ The Goal

We want to:

  • Add a title to the top of the chart.

  • Add a legend to the bottom right that explains node categories.

  • Ensure both are included when exporting to PDF.


📦 Full Working Code Example

Here's the full example (HTML + JavaScript + CSS), and we’ll break it down step by step below:

✅ HTML Structure

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

<!-- Hidden Legend Template -->
<div style="display: none;">
    <div id="legent-content">
        <div>
            <div style="width: 16px; height: 16px; background-color: #039BE5; display: inline-block; border-radius: 15px;"></div> Manager
        </div>
        <div>
            <div style="width: 16px; height: 16px; background-color: #F57C00; display: inline-block; border-radius: 15px;"></div> Sales
        </div>
        <div>
            <div style="width: 16px; height: 16px; background-color: #FFCA28; display: inline-block; border-radius: 15px;"></div> IT
        </div>
    </div>
</div>

<!-- Chart Container -->
<div id="tree"></div>

<!-- Custom Styles for Nodes -->
<style id="myStyle">
    .manager rect {
        stroke: #039BE5;
    }
    .sales rect {
        stroke: #F57C00;
    }
    .it rect {
        stroke: #FFCA28;
    }
</style>


🧠 JavaScript Logic

let initialized = false;
let legentHTML;
let titleHTML;

let chart = new OrgChart(document.getElementById("tree"), {
    template: 'olivia',
    enableSearch: false,
    orientation: OrgChart.orientation.bottom_left,
    mode: 'dark',
    menu: {
        pdf: { text: "Export PDF" },
    }
});

// Add custom HTML when the chart is drawn
chart.on('redraw', function () {
    if (!initialized) {
        // Add a title
        let title = document.createElement("h1");
        title.style.position = 'fixed';
        title.style.top = '0';
        title.style.width = '85%';
        title.style.textAlign = 'center';
        title.style.color = '#757575';
        title.innerHTML = 'My Title';
        chart.element.appendChild(title);
        titleHTML = title.outerHTML;

        // Add a legend
        let legent = document.createElement("div");
        legent.style.position = 'absolute';
        legent.style.bottom = '10px';
        legent.style.right = '10px';
        legent.style.color = '#757575';
        legent.innerHTML = document.querySelector('#legent-content').innerHTML;
        chart.element.appendChild(legent);
        legentHTML = legent.outerHTML;

        initialized = true;
    }
});

// Ensure the HTML is included in PDF exports
chart.on('exportstart', function (sender, args) {
    args.content += titleHTML + legentHTML;
    args.styles += document.getElementById('myStyle').outerHTML;
});

// Load nodes
chart.load([
    { id: 0, tags: ['manager'] },
    { id: 1, pid: 0, tags: ['sales'] },
    { id: 2, pid: 0, tags: ['it'] }
]);


📝 How It Works

chart.on('redraw', ...)

  • The redraw event fires every time the chart is rendered.

  • We use an initialized flag to make sure we only add our HTML once.

  • A title (<h1>) is added at the top using fixed positioning.

  • A legend is created by pulling inner HTML from a hidden <div> (#legent-content) and placing it in the corner.

  • We store outerHTML of both elements to reuse later.

chart.on('exportstart', ...)

  • When the user triggers an export (like PDF), OrgChart allows us to inject custom content.

  • We add the same title and legend HTML to args.content.

  • We also inject our custom CSS (myStyle) to preserve node styling in the exported file.


🎯 Result

You get a beautiful chart that:

  • Displays extra contextual UI elements (title + legend)

  • Includes those extras in the exported version

  • Uses modular and readable code


✅ Pro Tips

  • Make sure your legend uses the same classes or tags that your node styling does.

  • If you want the title to scroll with the chart, use absolute instead of fixed.

  • You can extend this method to add logos, footers, or even dropdown filters.


📌 Conclusion

Adding HTML to your OrgChart container is super flexible and useful for enriching your chart. Whether you're building a dashboard, an org structure viewer, or a visual directory, adding contextual elements like titles and legends can make a huge difference.


HTML elements inside the chart div - OrgChart JS

React OrgChart

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

Export and Import Org Chart to Excel

Е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.