Getting started with MJS Diagram
MJS Diagram is a set of web components for creating and dislplaying interactive diagrams, such as Flowcharts, Mind Maps, Network Diagrams, Org Charts and other.
MJS Diagram is extensible and enables you to add your own diagram types.
Installation
npm
Using a modern JavaScript/TypeScript environment? Just run:
npm install @markerjs/mjs-diagram
yarn add @markerjs/mjs-diagram
to add MJS Diagram to your project.
From CDN or local file
Just add a script tags referencing the core.js
, editor.js
and/or viewer.js
libraries.
<script src="https://unpkg.com/@markerjs/mjs-diagram/core.js"></script>
<script src="https://unpkg.com/@markerjs/mjs-diagram/editor.js"></script>
<script src="https://unpkg.com/@markerjs/mjs-diagram/viewer.js"></script>
In this case, we are referencing the library through the UNPKG CDN. For other options, please see the Downloads section.
Usage
To add a diagram editor to your web app follow these steps:
- Add a diagram editor web component to your page.
- Assign a stencil set (diagram type).
- Setup an event hander for the
saveclick
event to process the results.
Here's a simple example:
On the page:
<mjs-diagram-editor id="mjsDia"></mjs-diagram-editor>
Import MJS Diagram modules:
import * as mjsdcore from "@markerjs/mjs-diagram/core";
import * as mjsde from "@markerjs/mjs-diagram/editor";
import * as flowchart from "@markerjs/mjs-diagram/stencilsets/flowchart/flowchart";
When the page loads:
let editor = document.getElementById('mjsDia');
// assign imported Flowchart stencil set
editor.stencilEditorSet = flowchart.flowchartStencilEditorSet;
editor.addEventListener('saveclick', (ev) => {
// process state (represents the created diagram)
console.log(ev.detail.state);
});
To add a diagram viewer to your web app follow these steps:
- Add diagram viewer web component to your page.
- Assign a stencil set (diagram type).
- Load diagram configuration via the
show()
method.
Here's a simple example:
On the page:
<mjs-diagram-viewer id="mjsDiaView"></mjs-diagram-viewer>
Import MJS Diagram modules:
import * as mjsdcore from "@markerjs/mjs-diagram/core";
import * as mjsdv from "@markerjs/mjs-diagram/viewer";
import * as flowchart from "@markerjs/mjs-diagram/stencilsets/flowchart/flowchart";
When the page loads:
let viewer = document.getElementById('mjsDiaView');
// assign imported Flowchart stencil set
viewer.stencilSet = flowchart.flowchartStencilSet;
// load diagram (state)
viewer.show(savedState);
See also
Check out the "getting started" demo for the simplest implementation of MJS Diagram in action.