Ruler in Angular Diagram
The ruler provides horizontal and vertical guides for measuring in the Angular Diagram component. It can be used to measure diagram objects, indicate positions, and align diagram elements, making it especially useful for creating scale models. The ruler also includes a position indicator that displays the precise location of the mouse cursor on the diagram canvas, with the default color of the position indicator marker being red.
The diagram ruler consists of two components: a horizontal ruler displayed along the top edge and a vertical ruler along the left edge of the diagram canvas. Both rulers work together to provide comprehensive positioning and measurement capabilities.
Define rulers
The rulerSettings property of diagram controls the visibility and appearance of the ruler in the diagram.
The showRulers property shows or hides the rulers in the diagram.
The following code shows how to add a ruler to the diagram.
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent ,RulerSettingsModel} from '@syncfusion/ej2-angular-diagrams';
@Component({
imports: [
DiagramModule
],
providers: [ ],
standalone: true,
selector: "app-container",
template: `<div><ejs-diagram #diagram id="diagram" width="100%" height="600px" [rulerSettings]='rulerSettings'></ejs-diagram></div>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild('diagram')
public diagram?: DiagramComponent;
public rulerSettings: RulerSettingsModel = { showRulers: true}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Customizing the Ruler
The horizontalRuler and verticalRuler properties of rulerSettings customize the rulers appearance in the diagram.
By default, the ruler segments are arranged based on pixel values, with each segment representing a unit of measurement on the diagram canvas.
Ruler spacing and dimensions
The interval property defines the spacing between ruler segments, while the segmentWidth property sets the width of each segment. These properties apply to both horizontal and vertical rulers.
Tick alignment
The tickAlignment property controls the positioning of ruler tick marks. For the horizontal ruler, ticks can be aligned to the left or right side, while for the vertical ruler, they can be aligned to the top or bottom.
Ruler thickness
The thickness property sets the thickness of the ruler display area for both horizontal and vertical rulers.
The following code shows how the diagram ruler can be customized.
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent ,RulerSettingsModel} from '@syncfusion/ej2-angular-diagrams';
@Component({
imports: [
DiagramModule
],
providers: [ ],
standalone: true,
selector: "app-container",
template: `<div><ejs-diagram #diagram id="diagram" width="100%" height="600px" [rulerSettings]='rulerSettings'></ejs-diagram></div>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild('diagram')
public diagram?: DiagramComponent;
public rulerSettings: RulerSettingsModel = { showRulers: true,
horizontalRuler:{
interval:8,
segmentWidth:100,
thickness:25,
//Align horizontal ruler tick to the bottom side.
tickAlignment:"RightOrBottom"
},
verticalRuler:{
interval:10,
segmentWidth:200,
thickness:35,
//Align vertical ruler tick to the left side.
tickAlignment:"LeftOrTop"
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Arrange tick
The arrangeTick function allows customization of ruler tick appearance for both horizontal and vertical rulers. This function is called during the rendering of each tick mark, providing control over tick properties such as length and style.
The following code demonstrates how to use the arrangeTick function to customize the tick length.
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent ,RulerSettingsModel} from '@syncfusion/ej2-angular-diagrams';
@Component({
imports: [
DiagramModule
],
providers: [ ],
standalone: true,
selector: "app-container",
template: `<div><ejs-diagram #diagram id="diagram" width="100%" height="600px" [rulerSettings]='rulerSettings'></ejs-diagram></div>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild('diagram')
public diagram?: DiagramComponent;
public rulerSettings: RulerSettingsModel = { showRulers: true, horizontalRuler:{interval:10, segmentWidth:50, thickness:50, arrangeTick: this.arrangeTicks },verticalRuler:{interval:20, segmentWidth:200, thickness:20, tickAlignment:"LeftOrTop", markerColor: 'blue' } };
// To arrange ruler tick.
private arrangeTicks(args: any) {
if (args.tickInterval % 10 === 0) {
// Set the tick length to 25 when the interval is divisible by 10
args.tickLength = 45;
}
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Measurement units
The measurementUnit property of rulerSettings specifies the unit used to display the ruler values. The ruler supports the following measurement units:
-
Pixel- Displays ruler values in pixels. This is the default value. -
Point- Displays ruler values in points (1 pt = 1/72 inch). -
Inch- Displays ruler values in inches (1 in = 96 px). -
Millimeter- Displays ruler values in millimeters (1 mm = 3.7795 px). -
Centimeter- Displays ruler values in centimeters (1 cm = 37.7953 px).
When interacting with diagram elements such as dragging or resizing, the tooltip also displays measurements in the configured unit.
The following code demonstrates how to configure the ruler measurement unit initially. The measurementUnit is set to Centimeter, and it applies to both the horizontal and vertical rulers.
import { Component } from '@angular/core';
import { DiagramModule, RulerSettingsModel } from '@syncfusion/ej2-angular-diagrams';
@Component({
imports: [DiagramModule],
standalone: true,
selector: "app-container",
template: `<ejs-diagram id="diagram" width="100%" height="600px" [rulerSettings]='rulerSettings'></ejs-diagram>`
})
export class AppComponent {
public rulerSettings: RulerSettingsModel = {
showRulers: true,
// Display the ruler values in centimeters.
measurementUnit: 'Centimeter'
}
}The measurementUnit property supports runtime updates, instantly refreshing the ruler display when the measurement unit is changed.
The following code shows how to change the measurement unit at runtime. The ruler values are initially displayed in inches, and selecting another unit from the drop-down updates the measurementUnit property and refreshes the rulers.
import { Component, OnInit, ViewEncapsulation, ViewChild } from '@angular/core';
import { DiagramModule, DiagramComponent ,RulerSettingsModel, MeasurementUnit} from '@syncfusion/ej2-angular-diagrams';
@Component({
imports: [
DiagramModule
],
providers: [ ],
standalone: true,
selector: "app-container",
template: `<div>
<div style="margin-bottom: 10px;">
<label for="unit">Select ruler unit: </label>
<select id="unit" (change)="onUnitChange($event)">
<option value="Pixel">Pixel</option>
<option value="Point">Point</option>
<option value="Inch" selected>Inch</option>
<option value="Millimeter">Millimeter</option>
<option value="Centimeter">Centimeter</option>
</select>
</div>
<ejs-diagram #diagram id="diagram" width="100%" height="600px" [rulerSettings]='rulerSettings'></ejs-diagram>
</div>`,
encapsulation: ViewEncapsulation.None
})
export class AppComponent {
@ViewChild('diagram')
public diagram?: DiagramComponent;
public rulerSettings: RulerSettingsModel = {
showRulers: true,
// Display the ruler values in inches.
measurementUnit: 'Inch'
}
// Change the ruler measurement unit at runtime.
public onUnitChange(args: Event): void {
this.diagram!.rulerSettings.measurementUnit = (args.target as HTMLSelectElement).value as MeasurementUnit;
this.diagram!.dataBind();
}
}import { bootstrapApplication } from '@angular/platform-browser';
import { AppComponent } from './app.component';
import 'zone.js';
bootstrapApplication(AppComponent).catch((err) => console.error(err));Changing the measurement unit updates both the horizontal and vertical rulers.
Convert unit
The convertUnit method of the diagram converts values between different measurement units. It accepts the value to convert, the source unit, and the target unit as parameters, and returns the converted value.
The following code demonstrates how to convert values between units:
// convertUnit - Converts values between different units
// 'diagram' is the DiagramComponent reference obtained using @ViewChild.
let centimeters: number = this.diagram.convertUnit(300, 'Pixel', 'Centimeter');
let inches: number = this.diagram.convertUnit(96, 'Pixel', 'Inch');
let pixels: number = this.diagram.convertUnit(8, 'Centimeter', 'Pixel');Marker color
The markerColor property defines the ruler marker color for both horizontal and vertical rulers. The marker appears when hovering the mouse over the diagram canvas, providing precise position feedback.
The marker color can also be customized using CSS styles for advanced styling requirements.