Ruler in ASP.NET Core Diagram

The Ruler provides a horizontal and vertical guide for measuring in the ASP.NET Core Diagram control. The Ruler can be used to measure the diagram objects, indicate positions, and align diagram elements. This is especially useful in creating scale models.

Adding Rulers to the Diagram

  • The rulerSettings property is used to control the visibility and appearance of the ruler in the diagram.

  • The RulerSettings showRulers property is used to show or hide the rulers in the diagram.

  • The RulerSettings horizontalRuler and verticalRuler properties are used to customize the rulers appearance in the diagram.

Ruler

Customizing the Ruler

By default, the ruler segments are arranged based on pixel values.

  • The HorizontalRuler’s interval property allows to define the interval between ruler segments and the segmentWidth property allows to define the segment width of the ruler. Similarly, you can use the VerticalRuler’s interval and segmentWidth properties are used to define the interval and segment width of the vertical ruler.

  • The HorizontalRuler’s tickAlignment property is used to align the ruler tick either left or right side of the ruler. The VerticalRuler’s tickAlignment property is used to align the ruler tick either top or bottom side of the ruler.

  • The HorizontalRuler’s arrangeTick and VerticalRuler’s arrangeTick function is provided for the purpose of customizing the appearance of ruler ticks. It will be called for each tick rendering.

  • The HorizontalRuler’s markerColor and VerticalRuler’s markerColor properties are used to define the ruler marker color and marker will be shown when performing the interaction in the diagram.

The MarkerColor property can be customized using the Marker CSS style.

Customize

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.

<ejs-diagram id="diagram" width="100%" height="600px">
    <!-- Display the ruler values in centimeters. -->
    <e-diagram-rulersettings showRulers="true" measurementUnit="Centimeter">
    </e-diagram-rulersettings>
</ejs-diagram>

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.

<div style="margin-bottom: 10px;">
    <label for="unit">Select ruler unit: </label>
    <select id="unit" onchange="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 id="diagram" width="100%" height="600px">
    <!-- Display the ruler values in inches. -->
    <e-diagram-rulersettings showRulers="true" measurementUnit="Inch">
    </e-diagram-rulersettings>
</ejs-diagram>

<script>
    // Change the ruler measurement unit at runtime.
    function onUnitChange(args) {
        var diagram = document.getElementById("diagram").ej2_instances[0];
        diagram.rulerSettings.measurementUnit = args.target.value;
        diagram.dataBind();
    }
</script>

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:

<script>
    var diagram = document.getElementById("diagram").ej2_instances[0];
    // convertUnit - Converts values between different units
    var centimeters = diagram.convertUnit(300, 'Pixel', 'Centimeter');
    var inches = diagram.convertUnit(96, 'Pixel', 'Inch');
    var pixels = diagram.convertUnit(8, 'Centimeter', 'Pixel');
</script>