To create a basic interactive family tree, import the family-chart package and its CSS, define your data structure, and initialize the chart using createChart. You can then configure the card display (how information is shown on nodes) and the editing interface (how users modify data).
Data Format
Your data should be an array of objects containing:
id: A unique identifier.data: An object containing person-specific attributes (e.g., first name, last name).rels: An object defining relationships such as spouses, children, or parents using the IDs of related members.
Implementation Steps
- Import
family-chart and the required CSS. - Call
f3.createChart(selector, data) to initialize the visualization. - Use
.setCardHtml() to configure node card layouts. - Use
.editTree() to configure the editing interface. - Call
.updateTree({initial: true}) to render the chart.
import * as f3 from 'family-chart';
import 'family-chart/dist/styles/family-chart.css';
// Your family tree data
const data = [
{
"id": "1",
"data": {"first name": "John", "last name": "Doe", "birthday": "1980", "gender": "M"},
"rels": {"spouses": ["2"], "children": ["3"]}
},
{
"id": "2",
"data": {"first name": "Jane", "last name": "Doe", "birthday": "1982", "gender": "F"},
"rels": {"spouses": ["1"], "children": ["3"]}
},
{
"id": "3",
"data": {"first name": "Bob", "last name": "Doe", "birthday": "2005", "gender": "M"},
"rels": {"parents": ["1", "2"]}
}
];
// Create the chart
const f3Chart = f3.createChart('#FamilyChart', data)
const f3Card = f3Chart.setCardHtml()
.setCardDisplay([["first name","last name"],["birthday"]]);
const f3EditTree = f3Chart.editTree()
.setFields(["first name","last name","birthday"]);
f3Chart.updateTree({initial: true});