Force Tree Chart of Service Dependencies
If you are on a platform team, you are the one asked what breaks when a service goes down. The answer is in a table of services and what each depends on, but a table does not show which services are the hubs, or how far a failure would travel.
A force tree chart answers that. The gateway is pinned at the centre, every service it reaches settles around it, and a service with many dependents of its own pulls a cluster out behind it. The chart above is bound to the grid beneath it, one row per service, so filtering the rows redraws the chart. Filter the Depends on column to billing and only the billing branch is left.
Why a force tree and not a plain tree
A plain tree lays every level out in tidy rows, which is right for an org chart but wastes the space on a dependency map and hides the shape. A force tree lets the links pull the services into place, so the hubs sit in the middle with their clusters round them, and you can drag a node aside to follow one path.
The chart over a grid
Each row names the service it depends on, and the chart reads that column as the row’s parent. The id option says which column holds the service name and parentId says which holds its parent. The gateway has an empty parent, so it becomes the root. Force trees are an opt-in chart type, so import the module once after the charts module. Here is the example in plain JavaScript, with sample rows inline in place of your own.
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';
import '@toclocoinc/lattice-grid/modules/chart-forcetree';
const rows = [
{ id: 1, service: 'gateway', dependsOn: '' },
{ id: 2, service: 'auth', dependsOn: 'gateway' },
{ id: 3, service: 'orders', dependsOn: 'gateway' },
{ id: 4, service: 'billing', dependsOn: 'gateway' },
{ id: 5, service: 'sessions', dependsOn: 'auth' },
{ id: 6, service: 'cart', dependsOn: 'orders' },
{ id: 7, service: 'payments', dependsOn: 'billing' },
{ id: 8, service: 'card-vault', dependsOn: 'payments' },
];
const gridEl = document.createElement('div');
const chartEl = document.createElement('div');
gridEl.style.height = '240px';
chartEl.style.height = '440px';
document.body.append(gridEl, chartEl);
const grid = createGrid(gridEl, {
rowKey: 'id', rows,
columns: [
{ field: 'service', title: 'Service' },
{ field: 'dependsOn', title: 'Depends on', filter: { type: 'set' } },
],
});
createChart({ grid, container: chartEl, type: 'forceTree',
id: 'service', parentId: 'dependsOn', title: 'Services and what they depend on' });
The same thing in React
The React adapter is a factory, so you build the component once at module scope. The ref’s grid is the real grid, which is what the chart binds to.
import React, { useEffect, useRef } from 'react';
import { createGrid } from '@toclocoinc/lattice-grid';
import '@toclocoinc/lattice-grid/css';
import { createLatticeGrid } from '@toclocoinc/lattice-grid/modules/react';
import { createChart } from '@toclocoinc/lattice-grid/modules/charts';
import '@toclocoinc/lattice-grid/modules/chart-forcetree';
const LatticeGrid = createLatticeGrid({ React, createGrid });
const rows = [
{ id: 1, service: 'gateway', dependsOn: '' },
{ id: 2, service: 'auth', dependsOn: 'gateway' },
{ id: 3, service: 'orders', dependsOn: 'gateway' },
{ id: 4, service: 'cart', dependsOn: 'orders' },
];
const columns = [
{ field: 'service', title: 'Service' },
{ field: 'dependsOn', title: 'Depends on', filter: { type: 'set' } },
];
export function ServiceMap() {
const gridRef = useRef(null);
const chartRef = useRef(null);
useEffect(() => {
const chart = createChart({ grid: gridRef.current.grid, container: chartRef.current,
type: 'forceTree', id: 'service', parentId: 'dependsOn' });
return () => chart.destroy();
}, []);
return (<>
<LatticeGrid ref={gridRef} rowKey="id" columns={columns} rows={rows}
style={{ height: 200 }} />
<div ref={chartRef} style={{ height: 360 }} />
</>);
}
The options used
These are the rows of the charts reference for the options in the listings above.
| Option | Type | What it does |
|---|---|---|
grid |
Grid | Required. The grid to read. |
container |
Element or string | Required. Where to draw. |
type |
string | 'forceTree' here, a force-directed graph whose only links run from parent to child. |
id |
string | The column a parentId refers to: the row’s own identity. Defaults to the row key. |
parentId |
string | The column that names each row’s parent. A row with no parent, an unknown parent or a parent that leads back to itself hangs off the root. |
title |
string | Drawn above the plot. |
Two more options are worth knowing: expandDepth sets how many levels start open, so a large map can begin folded, and iterations sets how many ticks the layout settles for. The chart stops moving once it has settled.
Reading the map
Look for the nodes with the most links: those are the hubs, and a failure there reaches everything in their cluster. A leaf at the edge, such as the card vault in the example, is reached only through the services above it. A click folds a branch away or opens it again, and a node you drag stays where you drop it. The chart follows the grid’s filter, so a hidden row leaves the picture, and a service whose parent is filtered out hangs off the root until you clear the filter.
Next
See force trees beside the other hierarchy charts in the hierarchy charts family demo, read the full list of chart types on the charts page, or follow the how-to for a chart that follows the grid.