Background
The background of your diagram canvas can be easily customized in ngDiagram.
You can choose between built-in patterns (solid, grid, dotted), or provide your own custom background.
Available Background Types
Section titled “Available Background Types”Solid Color
Section titled “Solid Color”You can set a solid color background by overriding the background color of the diagram container.
To do this, simply override the CSS variable or set the background style directly on the ng-diagram component.
To use solid background, you don’t need to add the <ng-diagram-background> component.
Example:
--ngd-diagram-background-color: darkgray;or
ng-diagram { background: darkgray; /* Your desired color */}Grid Background
Section titled “Grid Background”The grid background displays a grid pattern with minor and major lines.
Grid backgrounds are commonly used in design, industry, and technical diagramming applications to provide a visual reference for layout and spacing. Our grid pattern is fully customizable, allowing you to adjust preferences.
Usage:
<ng-diagram ...props> <ng-diagram-background type="grid" /></ng-diagram>Configurable settings:
- Minor cell size: Size of the smallest grid cell (used for minor grid lines). Controlled via the diagram config BackgroundConfig.cellSize
- Major line frequency: How often a major line appears BackgroundConfig.majorLinesFrequency
- Line colors, widths, and opacity: Controlled via CSS variables.
CSS Variables:
:root { --ngd-background-line-minor-color: #c2c0c0; /* Color of minor grid lines */ --ngd-background-line-major-color: #6f7480; /* Color of major grid lines */ --ngd-background-line-minor-width: 0.5; /* Width of minor grid lines */ --ngd-background-line-major-width: 1; /* Width of major grid lines */ --ngd-background-line-minor-opacity: 0.5; /* Opacity of minor grid lines */ --ngd-background-line-major-opacity: 0.6; /* Opacity of major grid lines */}Dotted Background
Section titled “Dotted Background”The dotted background displays a repeating pattern of dots. This is the default background type.
Usage:
<ng-diagram-background type="dots" /><!-- or simply --><ng-diagram-background />Configurable settings:
- Dot spacing: Controlled via the diagram config BackgroundConfig.dotSpacing
- Dot color: Controlled via CSS variable.
CSS Variables:
:root { --ngd-background-dot-color: #6f7480; /* Dot color */}Custom Background
Section titled “Custom Background”You can provide any custom SVG, HTML, or image as the background by projecting content into the <ng-diagram-background> component.
When you apply a custom background, it will completely replace any built-in patterns.
Usage:
<ng-diagram-background> <!-- Your custom SVG, image, or HTML here --> <svg width="100%" height="100%"><!-- Your SVG content --></svg></ng-diagram-background>Custom background will be stretched to cover the entire diagram area.
Such background would not be panned or zoomed along with the diagram content. You can write a custom logic to achieve that if needed.
Example
Section titled “Example”import '@angular/compiler';
import { Component, signal } from '@angular/core';import { initializeModel, NgDiagramBackgroundComponent, NgDiagramComponent, provideNgDiagram, type NgDiagramConfig,} from 'ng-diagram';import { SidebarContainer, type BackgroundStyle,} from './sidebar/sidebar.component';
@Component({ imports: [NgDiagramComponent, SidebarContainer, NgDiagramBackgroundComponent], providers: [provideNgDiagram()], templateUrl: './diagram.component.html', styleUrls: ['./diagram.component.scss'],})export class DiagramComponent { backgroundStyle = signal<BackgroundStyle>('dot');
onBackgroundStyleChange(style: BackgroundStyle) { this.backgroundStyle.set(style); }
config = { zoom: { zoomToFit: { onInit: true, padding: [50, 285, 50, 50], }, }, // background: { // cellSize: { width: 20, height: 20 }, // Size of each minor cell in the 'grid' background // dotSpacing: 30, // Spacing between dots in the 'dot' background // majorLinesFrequency: { x: 10, y: 10 }, // Frequency of major lines in the 'grid' background // }, } satisfies NgDiagramConfig;
model = initializeModel({ nodes: [ { id: '1', position: { x: 100, y: 150 }, data: { label: 'Node 1' } }, { id: '2', position: { x: 400, y: 150 }, data: { label: 'Node 2' } }, ], edges: [ { id: '1', source: '1', sourcePort: 'port-right', targetPort: 'port-left', target: '2', data: {}, }, ], });}import '@angular/compiler';import { Component, input, output } from '@angular/core';
export type BackgroundStyle = 'solid' | 'dot' | 'grid' | 'custom';
@Component({ selector: 'sidebar-container', templateUrl: `./sidebar.component.html`, styleUrl: './sidebar.component.scss',})export class SidebarContainer { backgroundStyle = input<BackgroundStyle>(); backgroundStyleChange = output<BackgroundStyle>();
onBackgroundStyleChange(style: BackgroundStyle) { this.backgroundStyleChange.emit(style); }}<div class="not-content diagram"> <ng-diagram [model]="model" [config]="config"> @if (backgroundStyle() === 'dot') { <ng-diagram-background /> } @else if (backgroundStyle() === 'grid') { <ng-diagram-background type="grid" /> } @else if (backgroundStyle() === 'custom') { <ng-diagram-background> <svg width="100%" height="100%" style="display: block"> <defs> <pattern id="pattern-diagram-bg" width="40" height="40" patternUnits="userSpaceOnUse" > <rect width="40" height="40" fill="var(--ngd-ui-bg-primary-default)" /> <circle cx="10" cy="10" r="2.5" fill="var(--ngd-node-stroke-primary-default)" /> <circle cx="30" cy="30" r="2.5" fill="var(--ngd-background-line-major-color)" /> <path d="M0 40 L40 0" stroke="var(--ngd-focus-ring-node-active)" stroke-width="1" opacity="0.6" /> <path d="M-10 10 L10 -10 M30 50 L50 30" stroke="var(--ngd-button-gray-bg-default)" stroke-width="0.6" opacity="0.5" /> </pattern> </defs>
<rect width="100%" height="100%" opacity="0.4" fill="url(#pattern-diagram-bg)" /> </svg> </ng-diagram-background> } </ng-diagram></div><sidebar-container [backgroundStyle]="backgroundStyle()" (backgroundStyleChange)="onBackgroundStyleChange($event)"/><div class="sidebar"> <div class="group"> <fieldset> <legend>Background Style</legend>
<div class="horizontal"> <input #solidRadio type="radio" id="bgSolid" name="bgStyle" value="solid" [checked]="backgroundStyle() === 'solid'" (change)="onBackgroundStyleChange('solid')" /> <label for="bgSolid">Solid</label> </div>
<div class="horizontal"> <input #dotRadio type="radio" id="bgDot" name="bgStyle" value="dot" [checked]="backgroundStyle() === 'dot'" (change)="onBackgroundStyleChange('dot')" /> <label for="bgDot">Dot</label> </div>
<div class="horizontal"> <input #gridRadio type="radio" id="bgGrid" name="bgStyle" value="grid" [checked]="backgroundStyle() === 'grid'" (change)="onBackgroundStyleChange('grid')" /> <label for="bgGrid">Grid</label> </div>
<div class="horizontal"> <input #customRadio type="radio" id="bgCustom" name="bgStyle" value="custom" [checked]="backgroundStyle() === 'custom'" (change)="onBackgroundStyleChange('custom')" /> <label for="bgCustom">Custom</label> </div> </fieldset> </div></div>:host { position: relative; display: flex;}
.diagram { display: flex; width: 100%; height: var(--ng-diagram-height); border: var(--ng-diagram-border);}:host { position: absolute; top: 0; right: 0; height: 90%; padding: 1rem; margin-top: 0;
.sidebar { display: flex; flex-direction: column; background-color: var(--ngd-node-bg-primary-default); border: var(--ngd-node-border-size) solid var(--ngd-node-border-color); padding: 1rem; gap: 0.5rem; height: 100%; width: 200px; user-select: none;
.horizontal { display: flex; gap: 0.5rem; }
.vertical { display: flex; gap: 0.25rem; flex-direction: column; } }}