This document describes the view layer responsible for rendering the graph, managing cached visual state, and coordinating the validation lifecycle. The GraphView class acts as the rendering engine that translates the logical graph structure from GraphDataModel into visual representations on screen.
For information about the underlying data model and cell hierarchy, see GraphDataModel and Cell System. For details about geometry objects and coordinate calculations, see Geometry and Positioning. For rendering specifics, see CellRenderer and Shape Creation.
The GraphView class extends EventSource and serves as the central rendering coordinator. It maintains a cache of CellState objects, handles coordinate transformations, and manages the validation lifecycle that synchronizes the visual representation with the data model.
Sources: packages/core/src/view/GraphView.ts98-225
The GraphView maintains several essential properties:
| Property | Type | Purpose |
|---|---|---|
graph | AbstractGraph | Reference to the enclosing graph |
states | Map<Cell, CellState> | Cache mapping cells to their visual states |
scale | number | Current zoom level (1.0 = 100%) |
translate | Point | Current pan offset |
graphBounds | Rectangle | Bounding box of all visible content |
currentRoot | Cell | null | Root of the displayed hierarchy (for drill-down) |
Sources: packages/core/src/view/GraphView.ts163-182
The GraphView organizes rendering into four layered panes:
These panes provide z-ordering control, ensuring backgrounds render behind cells, which render behind overlays, which render behind decorators.
Sources: packages/core/src/view/GraphView.ts112-353
A CellState represents the computed visual properties of a cell at a specific point in time. It caches geometry, style resolution, absolute coordinates, and shape references to avoid redundant calculations during rendering.
Sources: packages/core/src/view/GraphView.ts34
Each CellState contains:
x, y, width, height (absolute coordinates in view space)origin, absoluteOffset (for hierarchical positioning)CellState for edgesshape (the rendered shape), text (the label)style (resolved CellStateStyle object)invalid (marks whether recomputation is needed)Sources: Inferred from GraphView usage patterns
The validation lifecycle is a three-phase process that synchronizes the visual representation with the data model:
Sources: packages/core/src/view/GraphView.ts445-869
The invalidate() method marks cells (and their descendants and connected edges) as needing recomputation. This is typically triggered by model changes.
The method sets state.invalid = true and recursively processes:
recurse is true)includeEdges is true)cell.invalidating flag to prevent infinite loopsSources: packages/core/src/view/GraphView.ts490-534
The validate() method walks the cell hierarchy and recomputes all invalid states. It performs these steps:
lastNode, lastHtmlNode, etc.validateCell() recursivelyvalidateCellState() for each cellgraphBoundsSources: packages/core/src/view/GraphView.ts536-563
For each cell state being validated, updateCellState() computes:
Sources: packages/core/src/view/GraphView.ts872-946
The convenience method revalidate() combines invalidation and validation:
This is commonly used after scale or translate changes to refresh the entire view.
Sources: packages/core/src/view/GraphView.ts455-461
The GraphView maintains a Map<Cell, CellState> that provides efficient lookups and lifecycle management.
States are created lazily through getState():
The visibility parameter controls whether a state should exist. If visible is false and a state exists, it is removed via removeState().
Sources: packages/core/src/view/GraphView.ts789-807
The removeState() method cleans up a cell state:
states mapThe clear() method removes states for an entire subtree, typically used when collapsing groups or changing the current root.
Sources: packages/core/src/view/GraphView.ts470-488
The GraphView applies two coordinate transformations: translate (pan) and scale (zoom). These convert from model coordinates to view (screen) coordinates.
For a cell with geometry position (gx, gy):
viewX = scale * (translate.x + gx)
viewY = scale * (translate.y + gy)
The inverse transformation (view to model):
modelX = (viewX / scale) - translate.x
modelY = (viewY / scale) - translate.y
Sources: packages/core/src/view/GraphView.ts928-933
Each transformation method fires events and triggers revalidation to update all cell states with new coordinates.
Sources: packages/core/src/view/GraphView.ts232-443
For cells within a parent, the coordinate computation accounts for:
origingraph.getChildOffsetForCell()geo.relative is true, position is a percentage of parent sizegeo.offset pointSources: packages/core/src/view/GraphView.ts876-945
Edge states require special handling for terminal points and routing:
Edge validation involves:
Sources: packages/core/src/view/GraphView.ts972-1008
The GraphView delegates actual shape creation and drawing to CellRenderer, but controls the rendering flow:
The stateValidated() method ensures correct z-ordering by managing DOM node insertion order based on whether cells should be kept in foreground or background.
Sources: packages/core/src/view/GraphView.ts816-1110
The GraphView manages special rendering for background images and page boundaries:
If graph.backgroundImage is set, the view creates an ImageShape in the backgroundPane and updates it during validation:
Sources: packages/core/src/view/GraphView.ts631-660
If graph.pageVisible is true, the view renders a page background shape with shadow effect:
The page bounds are computed from graph.pageFormat and graph.pageScale.
Sources: packages/core/src/view/GraphView.ts662-725
When the model changes, the typical flow is:
Sources: Inferred from GraphView design
The PrintPreview class uses GraphView with temporary cell states to render pages:
This allows rendering the same graph at different scales without affecting the main view.
Sources: packages/core/src/view/other/PrintPreview.ts879-887
Layout algorithms access cell states to read current positions and write new positions:
Sources: packages/core/src/view/layout/hierarchical/CoordinateAssignment.ts1024-1058
The validation system is designed for efficiency:
invalid is trueCellState caches bounding boxes to avoid recalculationrendering is falseThe updateStyle property can force style recalculation on every validation (default is false), which is useful during interactive style editing but expensive for normal operation.
Sources: packages/core/src/view/GraphView.ts184-189
This validation and state management system forms the foundation of maxGraph's rendering pipeline, efficiently translating the logical data model into a visual representation while supporting zoom, pan, and hierarchical drill-down operations.
Refresh this wiki