|
| 1 | +import { |
| 2 | + Graph, |
| 3 | + InternalEvent, |
| 4 | + Perimeter, |
| 5 | + RubberBandHandler, |
| 6 | +} from '@maxgraph/core'; |
| 7 | +import {registerCustomShapes} from "./custom-shapes"; |
| 8 | + |
| 9 | +export const initializeGraph = (container: HTMLElement) => { |
| 10 | + // Disables the built-in context menu |
| 11 | + InternalEvent.disableContextMenu(container); |
| 12 | + |
| 13 | + const graph = new Graph(container); |
| 14 | + graph.setPanning(true); // Use mouse right button for panning |
| 15 | + new RubberBandHandler(graph); // Enables rubber band selection |
| 16 | + |
| 17 | + // shapes and styles |
| 18 | + registerCustomShapes(); |
| 19 | + // create a dedicated style for "ellipse" to share properties |
| 20 | + graph.getStylesheet().putCellStyle('myEllipse', { |
| 21 | + perimeter: Perimeter.EllipsePerimeter, |
| 22 | + shape: 'ellipse', |
| 23 | + verticalAlign: 'top', |
| 24 | + verticalLabelPosition: 'bottom', |
| 25 | + }); |
| 26 | + |
| 27 | + // Gets the default parent for inserting new cells. This |
| 28 | + // is normally the first child of the root (ie. layer 0). |
| 29 | + const parent = graph.getDefaultParent(); |
| 30 | + |
| 31 | + // Adds cells to the model in a single step |
| 32 | + graph.batchUpdate(() => { |
| 33 | + // use the legacy insertVertex method |
| 34 | + const vertex01 = graph.insertVertex( |
| 35 | + parent, |
| 36 | + null, |
| 37 | + 'a regular rectangle', |
| 38 | + 10, |
| 39 | + 10, |
| 40 | + 100, |
| 41 | + 100 |
| 42 | + ); |
| 43 | + const vertex02 = graph.insertVertex( |
| 44 | + parent, |
| 45 | + null, |
| 46 | + 'a regular ellipse', |
| 47 | + 350, |
| 48 | + 90, |
| 49 | + 50, |
| 50 | + 50, |
| 51 | + { |
| 52 | + baseStyleNames: ['myEllipse'], |
| 53 | + fillColor: 'orange', |
| 54 | + } |
| 55 | + ); |
| 56 | + // use the legacy insertEdge method |
| 57 | + graph.insertEdge(parent, null, 'an orthogonal style edge', vertex01, vertex02, { |
| 58 | + // TODO cannot use constants.EDGESTYLE.ORTHOGONAL |
| 59 | + // TS2748: Cannot access ambient const enums when the '--isolatedModules' flag is provided. |
| 60 | + // See https://github.com/maxGraph/maxGraph/issues/205 |
| 61 | + edgeStyle: 'orthogonalEdgeStyle', |
| 62 | + rounded: true, |
| 63 | + }); |
| 64 | + |
| 65 | + // insert vertex using custom shapes using the new insertVertex method |
| 66 | + const vertex11 = graph.insertVertex({ |
| 67 | + parent, |
| 68 | + value: 'a custom rectangle', |
| 69 | + position: [20, 200], |
| 70 | + size: [100, 100], |
| 71 | + style: { shape: 'customRectangle' }, |
| 72 | + }); |
| 73 | + // use the new insertVertex method using position and size parameters |
| 74 | + const vertex12 = graph.insertVertex({ |
| 75 | + parent, |
| 76 | + value: 'a custom ellipse', |
| 77 | + x: 150, |
| 78 | + y: 350, |
| 79 | + width: 70, |
| 80 | + height: 70, |
| 81 | + style: { |
| 82 | + baseStyleNames: ['myEllipse'], |
| 83 | + shape: 'customEllipse', |
| 84 | + }, |
| 85 | + }); |
| 86 | + // use the new insertEdge method |
| 87 | + graph.insertEdge({ |
| 88 | + parent, |
| 89 | + value: 'another edge', |
| 90 | + source: vertex11, |
| 91 | + target: vertex12, |
| 92 | + style: { endArrow: 'block' }, |
| 93 | + }); |
| 94 | + }); |
| 95 | +} |
0 commit comments