A lightweight Angular Library for building drag and drop flow charts. Chart behavior and steps are customizable. Data can be exported or uploaded in json format.
Demo | Npm | Getting started | Wiki
A lightweight Angular Library for building drag and drop flow charts. Chart behavior and steps are customizable. Data can be exported or uploaded in json format.
Inspired by Alyssa X Flowy
1.0.0-beta.30
{ provide: REMOVE_STYLES_ON_COMPONENT_DESTROY, useValue: false }
@import '@joelwenzel/ng-flowchart/assets/styles.scss';
REMOVE_STYLES_ON_COMPONENT_DESTROY: true
1.0.2-beta
1.0.0-beta
npm i --save @joelwenzel/ng-flowchart
NgFlowchartModule
.import { NgFlowchartModule } from '@joelwenzel/ng-flowchart';
@NgModule({
imports: [
NgFlowchartModule
]
})
export class AppModule { }
<div class="some-container-with-large-dimensions" ngFlowchartCanvas>
</div>
Add the step directives to any elements that you want to drag into your canvas.
The directive requires an input, an object containing the templateRef, stepType and optional data. See the wiki for more information.
NOTE: The steps do not need to be in the same component as the canvas.
<div class="palette">
<div *ngFor="let item of items" [ngFlowchartStep]="{
template: stepContent, //templateRef or Type<NgFlowchartStepComponent>
type: item.type, //any unique string. used to classify the step in json
data: item.data //optional config data
}">
<span>{{item.name}}</span>
</div>
</div>
<ng-template #stepContent>
<div class="step-container">
<div class="step-heading">
heading
</div>
<div class="step-content">
content
</div>
</div>
</ng-template>
The template field on the ngFlowchartStep directive can contain a TemplateRef, as seen above, or a component type extending from NgFlowchartStepComponent.
For more complex steps that may need to have specific rules or add their own children, you should create a custom step component.
For more features and examples checkout the official documentation
Please star the repo if you liked the library. Your support means everything to me and helps me focus on delivering new features
The entire chart content and functionality is available via the NgFlowchart.Flow class.
//In your component.ts containing the chart
@ViewChild(NgFlowchartCanvasDirective)
canvasElement: NgFlowchartCanvasDirective;
ngOnInit() {
let flow: NgFlowchart.Flow = this.canvasElement.getFlow();
// manual canvas zoom methods reside on the canvas directive directly
this.canvasElement.scaleDown()
this.canvasElement.scaleUp()
this.canvasElement.setScale(1) //resets back to default scale
}
The node structure resembles a linked tree with each node having at most 1 parent and n number of children
let flow: NgFlowchart.Flow = this.canvasElement.getFlow();
// returns an array of all direct children of the root
flow.getRoot().children
// the following two lines return the same node
flow.getRoot()
flow.getRoot().children[0].parent
Returns the root node/step of this flowchart.
Returns the JSON representation of this flowchart. Optionally specify an indent factor
Re-renders the flowchart. This should only be needed in rare circumstances.
Pretty will attempt to re-center the flow in the canvas
Returns a step in the flowchart with the given id. Steps are created with the id of the html element id.
Clears the current flow, reseting the canvas. canDeleteStep callbacks will not be called from this method.
Clears the existing flow and uploads a new flow from the json string representation.
See the wiki for the full list and descriptions
The optional config data for the step, passed in the ngFlowchartStep directive
The array of direct children of this step
The parent step of this step
The step type as passed in the ngFlowchartStep directive
Creates and adds a child to this step
Destroys this step component and updates all necessary child and parent relationships
Remove a child from this step. Returns the index at which the child was found or -1 if not found.
The flowchart can be exported in json format via the Flow object or Step object.
//In your component.ts containing the chart
@ViewChild(NgFlowchartCanvasDirective)
canvasElement: NgFlowchartCanvasDirective;
onButtonClicked() {
const json = this.canvasElement.getFlow().toJSON(2);
console.log(json);
}
Here is sample json output for a very basic 3 node chart
{
"root": {
"id": "s1608918280530",
"type": "sample-step",
"data": {
"name": "Do Action",
"inputs": [
{
"name": "ACTION",
"value": "TRANSLATE"
}
]
},
"children": [
{
"id": "s1608918283650",
"type": "do-action",
"data": {
"name": "Do Action",
"inputs": [
{
"name": "ACTION",
"value": null
}
]
},
"children": []
},
{
"id": "s1608918285174",
"data": {
"name": "Notification",
"type": "notification",
"inputs": [
{
"name": "Address",
"value": "[email protected]"
}
]
},
"children": []
}
]
}
}
The chart flow can be initialized from the same json represenation seen in the Generating Output JSON section.
However, due to the customizable nature of the steps, step types must be registered with the NgFlowchartStepRegistry service so they can be created correctly.
constructor(private stepRegistry: NgFlowchartStepRegistry) {
}
ngAfterViewInit() {
//created from standared ng-template refs
this.stepRegistry.registerStep('rest-get', this.normalStepTemplate);
this.stepRegistry.registerStep('filter', this.normalStepTemplate);
//created from custom component
this.stepRegistry.registerStep('router', CustomRouterStepComponent);
}
After registering it is as simple as calling the public method on the flow object with your json.
@ViewChild(NgFlowchartCanvasDirective)
canvas: NgFlowchartCanvasDirective;
showUpload(json: string) {
this.canvas.getFlow().upload(json);
}
In addition to the Chart API above there are also several hooks and options to make the chart behave the way you want it to.
Options are passed via the ngFlowchartOptions input on the ngFlowchartCanvas directive.
<div
id="canvas"
ngFlowchartCanvas
[ngFlowchartOptions]="{
stepGap: 40
}"
></div>
The gap (in pixels) between chart steps. Default is 40.
An inner deadzone radius (in pixels) that will not register the hover icon. The default is 20.
Is the flow sequential? If true, then you will not be able to drag parallel steps.
Set the default root position of the chart.
When a canvas resize is detected, should the flow be re-centered? Default is true
Canvas zoom options. Defaults to mouse WHEEL zoom with a step of .1 (10%)
Callbacks are passed via the ngFlowchartCallbacks input on the ngFlowchartCanvas directive.
<div
id="canvas"
ngFlowchartCanvas
[ngFlowchartOptions]="{
stepGap: 40
}"
[ngFlowchartCallbacks]="callbacks"
></div>
//in your component.ts
callbacks: NgFlowchart.Callbacks = {};
constructor() {
//be sure to bind this to the callback if you want to access this classes context
this.callbacks.onDropError = this.onDropError;
this.callbacks.onDropStep = this.onDropStep.bind(this);
this.callbacks.onMoveError = this.onMoveError;
}
onDropError(error: NgFlowchart.DropError) {
console.log(error);
//show some popup
}
onMoveError(error: NgFlowchart.MoveError) {
console.log(error);
//show some popup
}
onDropStep(dropEvent: NgFlowchart.DropEvent) {
this.actions.push(dropEvent);
}
Called whenever a new step fails to drop on the canvas
Called whenever an existing canvas step fails to move
Called when the delete method has been called on the step
Called after the delete method has run on the step. If you need to access
step children or parents, use beforeDeleteStep
Called whenever a new step or existing step is successfully dropped on the canvas
Called before the canvas is about to re-render
Called after the canvas completes a re-render
Called after the canvas has been scaled
Custom steps can be created if you need any kind of complex logic for specific steps. The example below is a custom step for a router which can be seen elsewhere on this page.
@Component({
selector: 'app-custom-router-step',
template: `
<div class="router-step" #canvasContent>
<span>{{ data.name }}</span>
<button class="btn" (click)="onAddRoute()">Add Route</button>
</div>
`,
styleUrls: ['./custom-router-step.component.scss']
})
export class CustomRouterStepComponent extends NgFlowchartStepComponent {
routes = [];
canDrop(dropEvent: NgFlowchart.DropTarget): boolean {
return true;
}
getDropPositionsForStep(pendingStep: NgFlowchart.PendingStep): NgFlowchart.DropPosition[] {
if (pendingStep.template !== RouteStepComponent) {
return ['ABOVE', 'LEFT', 'RIGHT'];
}
else {
return ['BELOW'];
}
}
onAddRoute() {
let route = {
name: 'New Route',
condition: '',
sequence: null
}
let index = this.routes.push(route);
route.sequence = index;
this.addChild({
template: RouteStepComponent, //another custom step
type: 'route-step',
data: route
}, {
sibling: true
});
}
}
For the most part, the theme is left to the user given they have complete control over the canvas content via the step templates. However, connectors and drop icons can be styled by overriding a few css classes.
These styles should be placed in your root styles.scss or prefix with ::ng-deep
/** The drop icon outer circle */
.ngflowchart-step-wrapper[ngflowchart-drop-hover]::after {
background: #a3e798 !important;
}
/** The drop icon inner circle */
.ngflowchart-step-wrapper[ngflowchart-drop-hover]::before {
background: #4cd137 !important;
}
/** The arrow paths and arrow head */
.ngflowchart-arrow {
& #arrowhead {
fill: darkgrey;
}
& #arrowpath {
stroke: darkgrey;
}
}
When creating a new step, a data object can be passed to the step. This data object is completely optional but allows you to store/edit configuration data for the step. See Getting Started for passing the data.
This can even be a nested chart
export class AppComponent {
title = 'workspace';
@ViewChild(NgFlowchartCanvasDirective)
canvas: NgFlowchartCanvasDirective;
async onStepEdit(id: string) {
let step = this.canvas.getFlow().getStep(id);
//if data is an object then step.data will return a reference
//returning and setting an updated version of the data may not be needed
let updated = await this.showConfigPopup(step.data);
step.data = updated;
}
}
Depending on the type of data and complexity, it may be beneficial to create a component for it.
@Component({
selector: 'app-editable-step',
template: `
<div class="editable-step" #canvasContent>
<span>{{ data.name }}</span>
<button class="btn" (click)="edit()">Edit</button>
</div>
`,
styleUrls: ['./editable-step.component.scss']
})
export class EditableStepComponent extends NgFlowchartStepComponent {
edit() {
let updated = await this.showConfigPopup(this.data);
this.data = updated;
}
}
The canvas directive binds directly to the html disabled attribute.
//component.ts
disabled = false;
//component.html
<main>
<div
id="canvas"
ngFlowchartCanvas
[ngFlowchartOptions]="options"
[disabled]="disabled"
></div>
</main>
When the chart is disabled no styling is applied but easily can be by override the following styles:
/** Your base selector may be different */
div#canvas[disabled="true"] {
opacity: .7;
}
/** Ng deep is only needed if not in the root styles sheet*/
div#canvas[disabled="true"] ::ng-deep.ngflowchart-step-wrapper {
opacity: .7;
}
If you are trying to access your component variables and functions inside a callback be sure that you bound the “this” object when assigning the callback.
this.callbacks.onDropStep = this.onDropStep.bind(this);