ng new ng-mxgraph-demo --minimal
npm install mxgraph
"assets": [
"src/favicon.ico",
"src/assets",
{
"glob": "**/*",
"input": "./node_modules/mxgraph/javascript/src",
"output": "./assets/mxgraph"
}
]
Create src/config/mxgraph-config.js file to store mxGraph related configurations and set mxBasePath value.
mxBasePath = "assets/mxgraph";
"scripts": [
"src/config/mxgraph-config.js",
"node_modules/mxgraph/javascript/mxClient.js"
]
npm install mxgraph-type-definitions --save-dev
Update typeRoots section of tsconfig.json
"typeRoots": [
"node_modules/mxgraph-type-definitions",
"node_modules/@types"
]
In minimal mode, Angular CLI doesn't create tsconfig.json file. Just copy tsconfig.json to src folder from the repo.
Remove types section from tsconfig.app.json in order to skip override of typeRoots in tsconfig.json.
Generate demo component and implement "Hello World" example
ng g c demo --skipTests=true
<div id="demoGraph">
<div #graphContainer></div>
</div>
#demoGraph {
display: flex;
flex-direction: row;
flex-wrap: wrap;
justify-content: center;
align-items: center;
}
import { Component, ElementRef, ViewChild, AfterViewInit } from "@angular/core";
@Component({
selector: "demo",
templateUrl: "./demo.component.html",
styleUrls: ["./demo.component.scss"],
})
export class DemoComponent implements AfterViewInit {
@ViewChild("graphContainer") containerElementRef: ElementRef;
get container() {
return this.containerElementRef.nativeElement;
}
ngAfterViewInit(): void {
// Disables the built-in context menu
mxEvent.disableContextMenu(this.container);
// Creates the graph inside the given container
const graph = new mxGraph(this.container);
// Enables rubberband selection
new mxRubberband(graph);
// Gets the default parent for inserting new cells. This
// is normally the first child of the root (ie. layer 0).
const parent = graph.getDefaultParent();
// Adds cells to the model in a single step
graph.getModel().beginUpdate();
try {
const helloBlock = graph.insertVertex(parent, null, "Hello,", 20, 150, 80, 30);
const worldBlock = graph.insertVertex(parent, null, "World!", 200, 150, 80, 30);
graph.insertEdge(parent, null, "", helloBlock, worldBlock);
} finally {
// Updates the display
graph.getModel().endUpdate();
}
}
}
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';
import { DemoComponent } from './demo/demo.component';
const routes: Routes = [
{
path: 'demo',
component: DemoComponent
}
];
@NgModule({
imports: [RouterModule.forRoot(routes)],
exports: [RouterModule]
})
export class AppRoutingModule { }