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

Add method for updating flows in NAD viewer #137

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 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
82 changes: 79 additions & 3 deletions demo/src/diagram-viewers/add-diagrams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,14 @@ import {
OnMoveTextNodeCallbackType,
OnSelectNodeCallbackType,
OnToggleNadHoverCallbackType,
FLOW,
} from '../../../src';

export const addNadToDemo = () => {
fetch(NadSvgExample)
.then((response) => response.text())
.then((svgContent) => {
new NetworkAreaDiagramViewer(
const nadViewer = new NetworkAreaDiagramViewer(
document.getElementById('svg-container-nad')!,
svgContent,
NadSvgExampleMeta,
Expand All @@ -61,6 +62,62 @@ export const addNadToDemo = () => {
.getElementById('svg-container-nad')
?.getElementsByTagName('svg')[0]
.setAttribute('style', 'border:2px; border-style:solid;');

// add range slider to update flows
const flowsSlider = document.createElement('input');
flowsSlider.type = 'range';
flowsSlider.min = '1';
flowsSlider.max = '20';
flowsSlider.value = '1';
flowsSlider.style.width = '99%';
flowsSlider.setAttribute('list', 'flowsDatalist');
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
flowsSlider.addEventListener('input', () => {
const flows =
'[{"branchId": "NGEN_NHV1", "side": 1, "p": ' +
(627 - +flowsSlider.value * 20) +
'}, {"branchId": "NGEN_NHV1", "side": 2, "p": ' +
(-626 + +flowsSlider.value * 20) +
'}, {"branchId": "NHV1_NHV2_1", "side": 1, "p": ' +
(322 - +flowsSlider.value * 20) +
'}, {"branchId": "NHV1_NHV2_1", "side": 2, "p": ' +
(-320 + +flowsSlider.value * 20) +
'}, {"branchId": "NHV1_NHV2_2", "side": 1, "p": ' +
(322 - +flowsSlider.value * 20) +
'}, {"branchId": "NHV1_NHV2_2", "side": 2, "p": ' +
(-320 + +flowsSlider.value * 20) +
'}, {"branchId": "NHV2_NLOAD", "side": 1, "p": ' +
(-620 + +flowsSlider.value * 20) +
'}, {"branchId": "NHV2_NLOAD", "side": 2, "p": ' +
(621 - +flowsSlider.value * 20) +
'}]';
nadViewer.setJsonFlows(flows);
});

const flowsDatalist = document.createElement('datalist');
flowsDatalist.id = 'flowsDatalist';
for (let index = 1; index <= 20; index++) {
const flowsOption = document.createElement('option');
flowsOption.value = index.toString();
flowsDatalist.appendChild(flowsOption);
}

const flowsDiv = document.createElement('div');
for (let index = 1; index <= 20; index++) {
const flowsSpan = document.createElement('span');
flowsSpan.innerHTML = index.toString();
flowsDiv.appendChild(flowsSpan);
}
flowsDiv.style.width = '98%';
flowsDiv.style.display = 'flex';
flowsDiv.style.justifyContent = 'space-between';
flowsDiv.style.padding = '0 5px';
flowsDiv.style.fontSize = '12px';

const updateFlowsDiv = document.createElement('div');
updateFlowsDiv.appendChild(flowsSlider);
updateFlowsDiv.appendChild(flowsDatalist);
flo-dup marked this conversation as resolved.
Show resolved Hide resolved

document.getElementById('svg-container-nad')?.appendChild(updateFlowsDiv);
});

fetch(NadSvgExample)
Expand Down Expand Up @@ -92,7 +149,7 @@ export const addNadToDemo = () => {
fetch(NadSvgMultibusVLNodesExample)
.then((response) => response.text())
.then((svgContent) => {
new NetworkAreaDiagramViewer(
const nadViewer = new NetworkAreaDiagramViewer(
document.getElementById('svg-container-nad-multibus-vlnodes')!,
svgContent,
NadSvgMultibusVLNodesExampleMeta,
Expand All @@ -113,6 +170,25 @@ export const addNadToDemo = () => {
.getElementById('svg-container-nad-multibus-vlnodes')
?.getElementsByTagName('svg')[0]
.setAttribute('style', 'border:2px; border-style:solid;');

// add button to update flows
const flows = '[{"branchId": "L7-5-0", "side": 1, "p": 609}, {"branchId": "L7-5-0", "side": 2, "p": -611}]';
const updateFlowsTextArea = document.createElement('textarea');
updateFlowsTextArea.rows = 2;
updateFlowsTextArea.cols = 65;
updateFlowsTextArea.value = flows;
const br = document.createElement('br');
const updateFlowsButton = document.createElement('button');
updateFlowsButton.innerHTML = 'Update Flows';
updateFlowsButton.addEventListener('click', () => {
const flowsArray: FLOW[] = JSON.parse(updateFlowsTextArea.value);
nadViewer.setFlows(flowsArray);
});
const updateFlowsDiv = document.createElement('div');
updateFlowsDiv.appendChild(updateFlowsTextArea);
updateFlowsDiv.appendChild(br);
updateFlowsDiv.appendChild(updateFlowsButton);
document.getElementById('svg-container-nad-multibus-vlnodes')?.appendChild(updateFlowsDiv);
});

fetch(NadSvgMultibusVLNodes14Example)
Expand Down Expand Up @@ -208,7 +284,7 @@ export const addNadToDemo = () => {
handleTextNodeMove,
handleNodeSelect,
true,
true,
false,
null,
handleToggleNadHover
);
Expand Down
16 changes: 8 additions & 8 deletions demo/src/diagram-viewers/data/nad-eurostag-tutorial-example1.svg
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { debounce } from '@mui/material';

type DIMENSIONS = { width: number; height: number; viewbox: VIEWBOX };
type VIEWBOX = { x: number; y: number; width: number; height: number };
export type FLOW = { branchId: string; side: number; p: number };
flo-dup marked this conversation as resolved.
Show resolved Hide resolved

export type OnMoveNodeCallbackType = (
equipmentId: string,
Expand Down Expand Up @@ -1390,4 +1391,43 @@ export class NetworkAreaDiagramViewer {
}
});
}

public setJsonFlows(flows: string) {
const flowsArray: FLOW[] = JSON.parse(flows);
this.setFlows(flowsArray);
}

public setFlows(flows: FLOW[]) {
flows.forEach((flow) => {
this.setFlow(flow.branchId, flow.side, flow.p);
});
}

private setFlow(branchId: string, side: number, p: number) {
const edge: EdgeMetadata | undefined = this.diagramMetadata?.edges.find((edge) => edge.equipmentId == branchId);
flo-dup marked this conversation as resolved.
Show resolved Hide resolved
if (edge !== undefined) {
const halfEdge: SVGGraphicsElement | null = this.container.querySelector(
"[id='" + edge.svgId + '.' + side + "']"
);
const arrowGElement = halfEdge?.lastElementChild?.firstElementChild;
if (arrowGElement !== null && arrowGElement !== undefined) {
arrowGElement.classList.remove('nad-state-in', 'nad-state-out');
if (p < 0) {
arrowGElement.classList.add('nad-state-in');
} else {
arrowGElement.classList.add('nad-state-out');
}
const flowElement = arrowGElement.lastElementChild;
if (flowElement !== null && flowElement !== undefined) {
flowElement.innerHTML = p.toFixed(0);
} else {
console.warn('Skipping updating branch ' + branchId + ' flow: edge not found');
}
} else {
console.warn('Skipping updating branch ' + branchId + ' flow: edge not found');
}
} else {
console.warn('Skipping updating branch ' + branchId + ' flow: branch not found');
}
}
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type {
OnMoveTextNodeCallbackType,
OnSelectNodeCallbackType,
OnToggleNadHoverCallbackType,
FLOW,
} from './components/network-area-diagram-viewer/network-area-diagram-viewer';
export type { DiagramMetadata } from './components/network-area-diagram-viewer/diagram-metadata';
export { THRESHOLD_STATUS } from './components/network-area-diagram-viewer/dynamic-css-utils';
Expand Down
Loading