Skip to content

About

It's a simple and direct organization chart plugin. Anytime you want a tree-like chart, you can turn to OrgChart.

Resources

Stars

151 stars

Watchers

9 watching

Forks

Repository files navigation

React OrgChart

A React 19 organization chart based on the layout and interaction contracts of OrgChart 8. React renders the hierarchy, including compact branches and grouped families. The package does not depend on orgchart, jQuery or RxJS. React and React DOM are peers; the PNG and PDF renderers load on demand.

import { useRef } from 'react';
import OrgChart, { type ChartHandle } from '@dabeng/react-orgchart';
import '@dabeng/react-orgchart/style.css';

const team = { id: 'manager', name: 'Lao Lao', title: 'Director', children: [
  { id: 'engineering', name: 'Su Miao', title: 'Engineering' }
] };

export function Directory() {
  const chart = useRef<ChartHandle>(null);
  return <>
    <OrgChart ref={chart} data={team} nodeContent="title" pan zoom minimap />
    <button onClick={() => console.log(chart.current?.getFlatDatasource())}>Get records</button>
  </>;
}

Data and rendering

data accepts a nested object, unordered flat parent_id rows, grouped family arrays, or an HTML <ul>. Flat rows require one root, unique IDs and existing parents. Numeric zero is valid; string and numeric forms of an ID match. Empty rows render an empty chart. Nested records and child arrays are copied: editing one chart does not change the supplied tree or another chart. Keep the data reference stable to retain edits. Replacing data explicitly starts a new tree.

The root is level 1. visibleLevel={false} initially shows one level, true uses the default (999), and 0 hides the initial nodes. collapsed hides a node's child layer. hybrid and verticalLevel take precedence over compact. Family members share a household, but each subtree belongs to its individual parent. getHierarchy() preserves groups, while getFlatDatasource() returns parent_id and generated family_id fields.

Default title and content fields render as text. Prefer JSX for templates, e.g. nodeTemplate={person => <strong>{String(person.name)}</strong>}. For source compatibility, a string returned by nodeTemplate is trusted HTML, not escaped text. Never construct it from unsanitized user input. createNode(element, data) runs on mount before hover controls are created. initCompleted(chart) runs after mounting. Hover exposes controls; focus alone does not. Controls accept Enter and Space, and selection survives pointer leave.

Options

Area Props
Data and template data, nodeId (default id), nodeTitle (name), nodeContent, nodeTemplate, createNode, initCompleted, icons
Layout direction (t2b, b2t, l2r, r2l), visibleLevel, verticalLevel, compact (boolean or predicate), chartClass, toggleSiblingsResp
Navigation pan, zoom, minimap, zoominLimit (7), zoomoutLimit (0.5)
Editing draggable, drag (alias; draggable wins), dropCriteria(dragged, oldParent, target)
Export exportButton, exportButtonName (Export), exportFilename (OrgChart), exportFileextension (png or pdf), exportScale (2)

Use className and style on the React component for the container; chartClass applies to the chart. Override --oc-line-color, --oc-hover-color, --oc-control-color, --oc-control-hover-color, --oc-node-width, --oc-row-height and --oc-duration on .orgchart to customize the blue theme. The stylesheet respects reduced motion.

Ref API

chart and chartContainer expose native elements. Use elements belonging to this chart as method arguments. Reacquire elements after init, structural configuration changes, or moving a node to a different parent.

Area Methods
Lifecycle init(partialOptions?), setOptions(partialOptions) or setOptions(name, value), destroy(); each returns the handle
Query getHierarchy(includeNodeData?), getFlatDatasource(), getNodeState(node, relation?), getRelatedNodes(node, relation), getParent(node), getChildren(node), getSiblings(node)
Edit addAncestors(data, parentId), addDescendants(data, parent), addParent(node, data), addChildren(node, data), addSiblings(node, data), removeNodes(node)
Visibility hideParent(node), showParent(node), hideChildren(node), showChildren(node), hideSiblings(node, side?), showSiblings(node, side?)
Viewport setChartScale(chart, multiplier, anchor?), attachMinimap(), detachMinimap(), updateMinimap(forceRefresh?)
Export export(filename?, extension?), exportPNG(canvas, filename), exportPDF(canvas, filename, scale?)

Relations are parent, children or siblings; getNodeState returns { exist, visible }. Related elements are native nodes (or null for a missing parent) and arrays for children/siblings. Exporting hierarchy without IDs returns the original diagnostic string. export() returns a promise for the canvas, or false when busy/unavailable. Failure dispatches export-error.orgchart and rejects; exportPDF also returns a promise because jsPDF is loaded on demand. Listen on chart for native init.orgchart, show-*.orgchart, hide-*.orgchart and cancellable nodedrop.orgchart events. React unmount releases listeners, observers and pending transitions automatically.

Developing

Use Node 24 or later. No adjacent reference checkout is needed.

npm ci
npm start
npm run build          # publishable ESM, CSS and TypeScript declarations in dist/
npm run build:demo     # production site in build/
npm run typecheck
npm run test:unit
npm run test:integration
npx playwright install chromium
npm run test:e2e       # Playwright: 46 demos and the React entry at 1200px and 390px
npm run test:compat    # upstream Cypress behavior specs against the React implementation
npm test               # all four suites

The homepage is a JSX example. /demo/index.html lists the 46 English reference examples, including editing, on-demand requests, maps, export and the 10,000-node chart. Both browser suites start their own Vite server. Cypress uses installed Chrome by default; set CYPRESS_BROWSER to another installed Cypress browser if necessary. Map tiles require a network connection; graph rendering and local portraits do not.

Migrating from 1.x

This is a new major version. Import the package root and the stylesheet instead of the old Babel dist/ChartContainer path. Replace datasource with data, containerClass with className, and NodeTemplate with nodeTemplate={data => <YourNode nodeData={data} />}. Use React event handlers on your template or createNode for application-specific selection. The old RxJS selection service and React 16/CRA demo application are not part of this package.

About

It's a simple and direct organization chart plugin. Anytime you want a tree-like chart, you can turn to OrgChart.

Resources

Stars

151 stars

Watchers

9 watching

Forks

Releases

Packages

Used by

Contributors

Languages