React

This document shows you haw you can create an Family Tree JS React project.

Using NPM:

  1. Create a new project:
                    
    npx create-react-app familytree
            
  2. Go to the project root folder:
                    
    cd familytree
            
  3. Create file mytree.js in the src folder with the following content:
                    
    import React, { Component } from 'react';
    
    
    export default class Chart extends Component {
    
        constructor(props) {
            super(props);
            this.divRef = React.createRef();
        }
    
        shouldComponentUpdate() {
            return false;
        }
    
        componentDidMount() {
            this.family = new FamilyTree (this.divRef.current , {
                nodes: this.props.nodes,
    
                nodeBinding: {
                    field_0: 'name',
                    img_0: 'img'
                }
            });
        }
    
        render() {
            return (
                <div id="tree" ref={this.divRef}></div>
            );
        }
    }
            
  4. Install the Family Tree JS:
    You need to use one these two methods:

    • Downloaded package installation:
      • Add the extracted familytree.js in your project \src\ folder.
      • If you want to disable ESLint, add this at the begginig of familytree.js file:
                                        
        /* eslint-disable */
                            
      • In your mytree.js file add this reference:
                                        
        import FamilyTree from "./familytree.js";
                            
      • If you use TypeScript you need to add this in the end of both files (.js and .d.ts ):
                                        
        export default FamilyTree
                            
        and copy the .d.ts file in the same folder too

    • NPM installation:
      Warning!

      May not work for the licensed version.

      
                              
      npm i @balkangraph/familytree.js
                          
      In your mytree.js file add this reference:
                              
      import FamilyTree from "@balkangraph/familytree.js";
                          

  5. Change app.js as follows:
                    
    import React, { Component } from 'react';
    import FamilyTree from './mytree';
    
    export default class App extends Component {
        render() {
            return (
                <div style={{height: '100%'}}>
    
                    <FamilyTree nodes={[
                    { id: 1, pids: [2], name: 'Amber McKenzie', gender: 'female', img: 'https://cdn.balkan.app/shared/2.jpg'  },
                    { id: 2, pids: [1], name: 'Ava Field', gender: 'male', img: 'https://cdn.balkan.app/shared/m30/5.jpg' },
                    { id: 3, mid: 1, fid: 2, name: 'Peter Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/2.jpg' },
                    { id: 4, mid: 1, fid: 2, name: 'Savin Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/1.jpg'  },
                    { id: 5, mid: 1, fid: 2, name: 'Emma Stevens', gender: 'female', img: 'https://cdn.balkan.app/shared/w10/3.jpg' }
                    ]} />
                </div>
            );
        }
    }
            
  6. Start the project:
                    
    npm start
            

Using online references:

  1. Create an HTML file with an app element and add the online references of react, react-dom, babel and Family Tree JS:
                                                                                                                                                     
    <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/FamilyTree.js"></script>
        </body>
    </html>
            
  2. Add the div element and the javascript:
                                                       
    <div id="tree"></div>
    <script type="text/jsx">
        const app = document.getElementById('app')
    
        function Familytree(props) {
            var chart = new FamilyTree(document.getElementById("tree"), {
                nodeBinding: props.nodeBinding,
                nodes: props.nodes
            });
            return (null)
        }
    
        var data = [
                    { id: 1, pids: [2], name: 'Amber McKenzie', gender: 'female', img: 'https://cdn.balkan.app/shared/2.jpg'  },
                    { id: 2, pids: [1], name: 'Ava Field', gender: 'male', img: 'https://cdn.balkan.app/shared/m30/5.jpg' },
                    { id: 3, mid: 1, fid: 2, name: 'Peter Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/2.jpg' },
                    { id: 4, mid: 1, fid: 2, name: 'Savin Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/1.jpg'  },
                    { id: 5, mid: 1, fid: 2, name: 'Emma Stevens', gender: 'female', img: 'https://cdn.balkan.app/shared/w10/3.jpg' }
        ]
    
        var nodeBinding = {
            field_0: "name", 
            img_0: "img"
        }
    
        ReactDOM.render(<Familytree 
            nodes = {data} 
            nodeBinding = {nodeBinding}
        />, app)
    
    </script>
            

Next.js:

  1. Create a new file called package.json with an empty object {}.
                    
    {
    }
                
  2. Install Next.js:
                    
    npm install react react-dom next
                    
    You'll get something like this in package.json:
                    
    {
        "dependencies": {
            "next": "^12.1.0",
            "react": "^17.0.2",
            "react-dom": "^17.0.2"
        }
    }
                
  3. Add a script in package.json to run the Next.js development server while you develop:
                    
    "scripts": {
        "dev": "next dev"
    },
                
  4. Install the Family Tree JS NPM package:
                    
    npm i @balkangraph/familytree.js
            
  5. Create a pages folder and an index.js file in it with the following content:
                                                        
    import FamilyTree from '@balkangraph/familytree.js';
    function Familytree(props) {
        if (typeof window === 'object') {
        var chart = new FamilyTree(document.getElementById("tree"), {
            nodeBinding: props.nodeBinding,
            nodes: props.nodes
        });
        }
        return (null)
    }
    
    var data = [
                    { id: 1, pids: [2], name: 'Amber McKenzie', gender: 'female', img: 'https://cdn.balkan.app/shared/2.jpg'  },
                    { id: 2, pids: [1], name: 'Ava Field', gender: 'male', img: 'https://cdn.balkan.app/shared/m30/5.jpg' },
                    { id: 3, mid: 1, fid: 2, name: 'Peter Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/2.jpg' },
                    { id: 4, mid: 1, fid: 2, name: 'Savin Stevens', gender: 'male', img: 'https://cdn.balkan.app/shared/m10/1.jpg'  },
                    { id: 5, mid: 1, fid: 2, name: 'Emma Stevens', gender: 'female', img: 'https://cdn.balkan.app/shared/w10/3.jpg' }
    ]
    
    var nodeBinding = {
        field_0: "name",
        img_0: "img"
    }
    
    export default function HomePage() {
    
        return (
                <div>
                <div id="tree"></div>
                <Familytree nodes={data}
                nodeBinding={nodeBinding} />
        </div>
        )
    }
                
  6. Install next-transpile-modules
                    
    npm install --save next-transpile-modules
                
  7. Create next.config.js file in the root folder with the following content:
                           
    const withTM = require('next-transpile-modules')(['@balkangraph/familytree.js']); // pass the modules you would like to see transpiled
    
    module.exports = withTM();
                
  8. run the project
                    
    npm run dev