I am working on Angular 4 and I want to integrate mxGraph in my project. I have googled for it but I am not getting the full working example.
I have tried following
I also couldn't find any resources that can solve this problem. So I made my own npm packages that others can also use. You can try to use one of these packages for your application.
ts-mxgraph typescript wrapped version of mxgraph library v4.03.
ts-mxgraph-factory typescript wrapper
ts-mxgraph-typings
I used the following method in my project.
Step 1:
create mxgraph.overrides.ts file inside the project
Step 2
Import mxgraph prototype methods. Can also extend the original methods of the library itself.
import '../../assets/deflate/base64.js'
import '../../assets/deflate/pako.min.js';
import { mxgraph, mxgraphFactory } from "ts-mxgraph";
const mx = mxgraphFactory({
mxBasePath: 'mxgraph',
mxLoadResources: false,
mxLoadStylesheets: false,
});
declare const Base64: any;
declare const pako: any;
let mxActor: any = mx.mxActor;
let mxArrow: any = mx.mxArrow;
let mxArrowConnector: any = mx.mxArrowConnector;
let mxGraph: any = mx.mxGraph;
let mxClient: any = mx.mxClient;
let mxClipboard: any = mx.mxClipboard;
let mxCellMarker: any = mx.mxCellMarker;
let mxCodecRegistry: any = mx.mxCodecRegistry;
let mxDoubleEllipse: any = mx.mxDoubleEllipse;
let mxUtils: any = mx.mxUtils;
...
// extends mxgraph prototypes
mxGraph.prototype.updatePageBreaks = function(visible, width, height) {
const useCssTranforms = this.useCssTransforms, scale = this.view.scale,
translate = this.view.translate;
if (useCssTranforms) {
this.view.scale = 1;
this.view.translate = new mxPoint(0, 0);
this.useCssTransforms = false;
}
graphUpdatePageBreaks.apply(this, arguments);
if (useCssTranforms) {
this.view.scale = scale;
this.view.translate = translate;
this.useCssTransforms = true;
}
}
// Adds panning for the grid with no page view and disabled scrollbars
const mxGraphPanGraph = mxGraph.prototype.panGraph;
mxGraph.prototype.panGraph = function(dx, dy) {
mxGraphPanGraph.apply(this, arguments);
if (this.shiftPreview1 != null) {
let canvas = this.view.canvas;
if (canvas.ownerSVGElement != null) {
canvas = canvas.ownerSVGElement;
}
const phase = this.gridSize * this.view.scale * this.view.gridSteps;
const position = -Math.round(phase - mxUtils.mod(this.view.translate.x * this.view.scale + dx, phase)) + 'px ' +
-Math.round(phase - mxUtils.mod(this.view.translate.y * this.view.scale + dy, phase)) + 'px';
canvas.style.backgroundPosition = position;
}
}
...
export {
mxClient,
mxUtils,
mxRubberband,
mxEventObject,
mxEdgeHandler,
mxEvent,
mxGraph,
mxGraphModel,
mxGeometry,
mxConstants,
...
}
// and then import these where you want to use them
import {
mxClient,
mxUtils,
mxEvent,
mxGraph,
mxGraphModel,
mxGeometry,
mxConstants,
mxCell,
mxDictionary,
mxCellEditor,
mxStyleRegistry
} from './mxgraph.overrides';
import { IMAGE_PATH, STYLE_PATH, STENCIL_PATH, urlParams } from '../config';
declare var Base64: any;
declare var pako: any;
export class Graph extends mxGraph {
...
}