Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#292 Improve Population Diagram styling #302

Open
wants to merge 8 commits into
base: develop
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 45 additions & 6 deletions frontend/src/components/ProofingTab/GraphDiagram/GraphDiagram.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React, {useEffect, useRef, useState} from "react";
import React, {useEffect, useLayoutEffect, useRef, useState} from "react";
import InfoMenu from "./InfoMenu";
import NavigationMenu from "./NavigationMenu";
import createEngine, {
BasePositionModelOptions,
DiagramModel,
} from '@projectstorm/react-diagrams';
BasePositionModelOptions, DagreEngine,
DiagramModel, PathFindingLinkFactory, DiagramEngine } from '@projectstorm/react-diagrams';
import {CanvasWidget} from '@projectstorm/react-canvas-core';
import {CustomNodeFactory} from "./Factories/CustomNodeFactory";
import {
Expand Down Expand Up @@ -39,6 +38,34 @@ interface GraphDiagramProps {
}


function genDagreEngine() {
return new DagreEngine({
graph: {
rankdir: 'TB',
ranksep: 300,
nodesep: 250,
marginx: 50,
marginy: 50
},
});
}
function reroute(engine: DiagramEngine) {
engine.getLinkFactories().getFactory<PathFindingLinkFactory>(PathFindingLinkFactory.NAME).calculateRoutingMatrix();
}
function autoDistribute(engine: DiagramEngine) {
const model = engine.getModel();

if (!model || model.getNodes().length === 0) {
return;
}

const dagreEngine = genDagreEngine();
dagreEngine.redistribute(model);

reroute(engine);
engine.repaintCanvas();
}

const GraphDiagram: React.FC<GraphDiagramProps> = ({
origins,
vias,
Expand Down Expand Up @@ -105,8 +132,20 @@ const GraphDiagram: React.FC<GraphDiagramProps> = ({
setModelFitted(true);
}
}, [modelUpdated, modelFitted, engine]);

return (

useLayoutEffect(() => {
autoDistribute(engine);
}, [engine, modelUpdated]);

useEffect(() => {
const currentContainer = containerRef.current;

if (modelUpdated && currentContainer) {
autoDistribute(engine);
}
}, [engine, modelUpdated, destinations, vias, origins]);

return (
modelUpdated ? (
<div ref={containerRef} className={"graphContainer"}>
<NavigationMenu engine={engine} statementId={statementId || "-1"}/>
Expand Down