Fishbone Layout
Loading example…
import { Graph, treeToGraphData } from '@antv/g6';
const data = {
id: 'Quality',
children: [
{
id: 'Machine',
children: [{ id: 'Mill' }, { id: 'Mixer' }, { id: 'Metal Lathe', children: [{ id: 'Milling' }] }],
},
{ id: 'Method' },
{
id: 'Material',
children: [
{
id: 'Masonite',
children: [
{ id: 'spearMint' },
{ id: 'pepperMint', children: [{ id: 'test3' }] },
{ id: 'test1', children: [{ id: 'test4' }] },
],
},
{
id: 'Marscapone',
children: [{ id: 'Malty' }, { id: 'Minty' }],
},
{ id: 'Meat', children: [{ id: 'Mutton' }] },
],
},
{
id: 'Man Power',
children: [
{ id: 'Manager' },
{ id: "Master's Student" },
{ id: 'Magician' },
{ id: 'Miner' },
{ id: 'Magister', children: [{ id: 'Malpractice' }] },
{
id: 'Massage Artist',
children: [{ id: 'Masseur' }, { id: 'Masseuse' }],
},
],
},
{
id: 'Measurement',
children: [{ id: 'Malleability' }],
},
{
id: 'Milieu',
children: [{ id: 'Marine' }],
},
],
};
export const layoutFishbone = async (context) => {
const graph = new Graph({
...context,
container: 'container',
autoFit: 'view',
data: treeToGraphData(data),
node: {
type: 'rect',
style: {
size: [32, 32],
// fill: () => randomColor(),
label: false,
labelFill: '#262626',
labelFontFamily: 'Gill Sans',
labelMaxLines: 2,
labelMaxWidth: '100%',
labelPlacement: 'center',
labelText: (d) => d.id,
labelWordWrap: true,
},
},
edge: {
type: 'polyline',
style: {
lineWidth: 3,
},
},
layout: {
type: 'fishbone',
vGap: 48,
hGap: 48,
direction: 'RL',
},
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
animation: false,
});
await graph.render();
layoutFishbone.form = (panel) => {
const config = {
type: 'fishbone',
direction: 'RL',
};
return [
panel
.add(config, 'direction', ['LR', 'RL'])
.name('Direction')
.onChange((value) => {
graph.setLayout((prev) => ({ ...prev, direction: value }));
graph.render();
}),
];
};
return graph;
};
layoutFishbone();
import { Graph } from '@antv/g6';
import GUI from 'lil-gui';
export function addPanel(renderPanel: (gui: GUI) => void) {
const gui = new GUI({ container: document.body });
gui.title('Control');
Object.assign(gui.domElement.style, { position: 'absolute', top: '0', right: '0', zIndex: '10' });
renderPanel(gui);
addEventListener('pagehide', () => gui.destroy(), { once: true });
}
export async function createGraph(
options: ConstructorParameters<typeof Graph>[0],
size: { width?: number; height?: number } = {},
renderPanel?: (gui: GUI, graph: Graph) => void,
) {
const container = document.createElement('div');
Object.assign(container.style, {
width: '100%',
maxWidth: `${size.width || 600}px`,
height: `${size.height || 400}px`,
});
document.getElementById('container')!.append(container);
const graph = new Graph({ ...size, ...options, width: container.clientWidth, container, autoResize: true });
addEventListener('pagehide', () => graph.destroy(), { once: true });
await graph.render();
if (renderPanel) addPanel((gui) => renderPanel(gui, graph));
return container;
}