React
This document shows you how to create an OrgChart JS React project.
Creating a local React project
- Create a new project:
bash
npx create-react-app orgchartGo to the project root folder:
bash
cd orgchart- Create file mytree.js in the src folder with the following content:
javascript
import React, { Component } from "react";
export default class Tree extends Component {
constructor(props) {
super(props);
this.divRef = React.createRef();
}
shouldComponentUpdate() {
return false;
}
componentDidMount() {
this.chart = new OrgChart(this.divRef.current, {
nodes: this.props.nodes,
nodeBinding: {
field_0: "name",
img_0: "img"
}
});
}
render() {
return <div id="tree" ref={this.divRef}></div>;
}
}- Install OrgChart JS.
Downloaded package installation:
- Add the extracted orgchart.js in your project src folder.
- In your mytree.js file add this reference:
javascript
import OrgChart from "./orgchart.js";Note: For TypeScript projects use orgchart.esm.js and orgchart.esm.d.ts files.
- Change app.js as follows:
javascript
import React, { Component } from "react";
import Tree from "./mytree";
export default class App extends Component {
render() {
return (
<div style={{ height: "100%" }}>
<Tree
nodes={[
{ id: 1, name: "Denny Curtis", title: "CEO", img: "https://cdn.balkan.app/shared/2.jpg" },
{ id: 2, pid: 1, name: "Ashley Barnett", title: "Sales Manager", img: "https://cdn.balkan.app/shared/3.jpg" },
{ id: 3, pid: 1, name: "Caden Ellison", title: "Dev Manager", img: "https://cdn.balkan.app/shared/4.jpg" },
{ id: 4, pid: 2, name: "Elliot Patel", title: "Sales", img: "https://cdn.balkan.app/shared/5.jpg" },
{ id: 5, pid: 2, name: "Lynn Hussain", title: "Sales", img: "https://cdn.balkan.app/shared/6.jpg" },
{ id: 6, pid: 3, name: "Tanner May", title: "Developer", img: "https://cdn.balkan.app/shared/7.jpg" },
{ id: 7, pid: 3, name: "Fran Parsons", title: "Developer", img: "https://cdn.balkan.app/shared/8.jpg" }
]}
/>
</div>
);
}
}- Start the project:
bash
npm startUsing online references
- Create an HTML file with an app element and add the online references of react, react-dom, babel and OrgChart JS:
html
<html>
<body>
<div id="app"></div>
<script src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
<script src="https://unpkg.com/@babel/standalone/babel.min.js"></script>
<script src="https://balkan.app/js/orgchart.js"></script>
</body>
</html>- Add the div element and the javascript:
jsx
<div id="tree"></div>
<script type="text/jsx">
const app = document.getElementById("app");
function OrgChartComponent(props) {
const chart = new OrgChart(document.getElementById("tree"), {
nodeBinding: props.nodeBinding,
nodes: props.nodes
});
return null;
}
const data = [
{ id: 1, name: "Denny Curtis", title: "CEO", img: "https://cdn.balkan.app/shared/2.jpg" },
{ id: 2, pid: 1, name: "Ashley Barnett", title: "Sales Manager", img: "https://cdn.balkan.app/shared/3.jpg" },
{ id: 3, pid: 1, name: "Caden Ellison", title: "Dev Manager", img: "https://cdn.balkan.app/shared/4.jpg" },
{ id: 4, pid: 2, name: "Elliot Patel", title: "Sales", img: "https://cdn.balkan.app/shared/5.jpg" },
{ id: 5, pid: 2, name: "Lynn Hussain", title: "Sales", img: "https://cdn.balkan.app/shared/6.jpg" },
{ id: 6, pid: 3, name: "Tanner May", title: "Developer", img: "https://cdn.balkan.app/shared/7.jpg" },
{ id: 7, pid: 3, name: "Fran Parsons", title: "Developer", img: "https://cdn.balkan.app/shared/8.jpg" }
];
const nodeBinding = {
field_0: "name",
img_0: "img"
};
ReactDOM.render(
<OrgChartComponent nodes={data} nodeBinding={nodeBinding} />,
app
);
</script>Download a React TypeScript Project
Next.js
- Create a new file called package.json with an empty object
{}.
json
{}- Install Next.js:
bash
npm install react react-dom nextYou will get something like this in package.json:
json
{
"dependencies": {
"next": "^12.1.0",
"react": "^17.0.2",
"react-dom": "^17.0.2"
}
}- Add a script in package.json to run the Next.js development server while you develop:
json
"scripts": {
"dev": "next dev"
}- Install the OrgChart JS NPM package:
bash
npm i balkan-orgchart-js- Create a pages folder and an index.js file in it with the following content:
javascript
import OrgChart from "balkan-orgchart-js";
function OrgChartComponent(props) {
if (typeof window === "object") {
const chart = new OrgChart(document.getElementById("tree"), {
nodeBinding: props.nodeBinding,
nodes: props.nodes
});
}
return null;
}
const data = [
{ id: 1, name: "Denny Curtis", title: "CEO", img: "https://cdn.balkan.app/shared/2.jpg" },
{ id: 2, pid: 1, name: "Ashley Barnett", title: "Sales Manager", img: "https://cdn.balkan.app/shared/3.jpg" },
{ id: 3, pid: 1, name: "Caden Ellison", title: "Dev Manager", img: "https://cdn.balkan.app/shared/4.jpg" },
{ id: 4, pid: 2, name: "Elliot Patel", title: "Sales", img: "https://cdn.balkan.app/shared/5.jpg" },
{ id: 5, pid: 2, name: "Lynn Hussain", title: "Sales", img: "https://cdn.balkan.app/shared/6.jpg" },
{ id: 6, pid: 3, name: "Tanner May", title: "Developer", img: "https://cdn.balkan.app/shared/7.jpg" },
{ id: 7, pid: 3, name: "Fran Parsons", title: "Developer", img: "https://cdn.balkan.app/shared/8.jpg" }
];
const nodeBinding = {
field_0: "name",
img_0: "img"
};
export default function HomePage() {
return (
<div>
<div id="tree"></div>
<OrgChartComponent nodes={data} nodeBinding={nodeBinding} />
</div>
);
}- Install next-transpile-modules:
bash
npm install --save next-transpile-modules- Create next.config.js in the root folder with the following content:
javascript
const withTM = require("next-transpile-modules")(["balkan-orgchart-js"]);
module.exports = withTM();- Run the project:
bash
npm run dev