Ruler in Vue Diagram
The ruler provides horizontal and vertical guides for measuring in the Vue Diagram control. 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.
Define rulers
The rulerSettingsproperty of diagram is used to control the visibility and appearance of the ruler in the diagram.
The showRulers property is used to show or hide the rulers in the diagram.
The following code shows how to add a ruler to the diagram.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { DiagramComponent as EjsDiagram } from '@syncfusion/ej2-vue-diagrams';
const width = "600px";
const height = "600px";
const rulerSettings = {
showRulers: true
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style><template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent } from '@syncfusion/ej2-vue-diagrams';
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "600px",
rulerSettings: {
showRulers: true
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style>Customizing the Ruler
horizontalRuler and verticalRuler properties of rulerSettings are used to customize the rulers appearance in the diagram.
By default, the ruler segments are arranged based on pixel values.
The HorizontalRuler’s interval property defines the spacing between ruler segments, and the segmentWidth property sets the width of each segment. Similarly, the VerticalRuler’s interval and segmentWidth properties control the interval and segment width for the vertical ruler.
The HorizontalRuler’s tickAlignment property aligns the ruler ticks to the left or right side, while the VerticalRuler’s tickAlignment aligns them to the top or bottom.
The HorizontalRuler’s thickness property sets the thickness of the horizontal ruler, and the VerticalRuler’s thickness property sets the thickness of the vertical ruler.
The following code shows how the diagram ruler can be customized.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { DiagramComponent as EjsDiagram } from '@syncfusion/ej2-vue-diagrams';
const width = "100%";
const height = "600px";
const rulerSettings = {
showRulers: true,
horizontalRuler: { interval: 8, segmentWidth: 100, thickness: 35, tickAlignment: "RightOrBottom" },
verticalRuler: { interval: 10, segmentWidth: 200, thickness: 35, tickAlignment: "LeftOrTop" }
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style><template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent } from '@syncfusion/ej2-vue-diagrams';
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "600px",
rulerSettings: {
showRulers: true,
horizontalRuler: { interval: 8, segmentWidth: 100, thickness: 35, tickAlignment: "RightOrBottom" },
verticalRuler: { interval: 10, segmentWidth: 200, thickness: 35, tickAlignment: "LeftOrTop" }
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style>Arrange tick
The HorizontalRuler’s arrangeTick and VerticalRuler’s arrangeTick functions allow you to customize the appearance of ruler ticks. These functions are called for each tick rendering.
The following code demonstrates how to use the arrangeTick function to customize the tickLength.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { DiagramComponent as EjsDiagram } from '@syncfusion/ej2-vue-diagrams';
const width = "600px";
const height = "600px";
const arrangeTicks = (args) => {
if (args.tickInterval % 10 === 0) {
args.tickLength = 25;
}
};
const nodes= [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
},
];
const rulerSettings = {
showRulers: true,
horizontalRuler: { interval: 10, segmentWidth: 50, thickness: 50,orientation: 'Horizontal',arrangeTick: arrangeTicks, },
verticalRuler: { interval: 20, segmentWidth: 200, thickness: 20, tickAlignment: "LeftOrTop", markerColor: 'blue', }
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style><template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :nodes='nodes' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent } from '@syncfusion/ej2-vue-diagrams';
let nodes= [
{
id: 'node1',
width: 100,
height: 100,
offsetX: 100,
offsetY: 100,
},
];
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "600px",
nodes: nodes,
rulerSettings: {
showRulers: true,
horizontalRuler: { interval: 10, segmentWidth: 50, thickness: 50,orientation: 'Horizontal',arrangeTick: this.arrangeTicks },
verticalRuler: { interval: 20, segmentWidth: 200, thickness: 20, tickAlignment: "LeftOrTop", markerColor: 'blue', }
}
}
},
methods: {
arrangeTicks(args) {
if (args.tickInterval % 10 === 0) {
args.tickLength = 25;
}
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style>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.
<template>
<div id="app">
<ejs-diagram id="diagram" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent } from '@syncfusion/ej2-vue-diagrams';
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "600px",
rulerSettings: {
showRulers: true,
// Display the ruler values in centimeters.
measurementUnit: 'Centimeter'
}
}
}
}
</script>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.
<template>
<div id="app">
<div style="margin-bottom: 10px;">
<label for="unit">Select ruler unit: </label>
<select id="unit" @change="onUnitChange">
<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" ref="diagramObj" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script setup>
import { ref } from 'vue';
import { DiagramComponent as EjsDiagram } from '@syncfusion/ej2-vue-diagrams';
const diagramObj = ref(null);
const width = "600px";
const height = "600px";
const rulerSettings = {
showRulers: true,
// Display the ruler values in inches.
measurementUnit: 'Inch'
}
// Change the ruler measurement unit at runtime.
const onUnitChange = (args) => {
const diagram = diagramObj.value.ej2Instances;
diagram.rulerSettings.measurementUnit = args.target.value;
diagram.dataBind();
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style><template>
<div id="app">
<div style="margin-bottom: 10px;">
<label for="unit">Select ruler unit: </label>
<select id="unit" @change="onUnitChange">
<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" ref="diagramObj" :width='width' :height='height' :rulerSettings='rulerSettings'></ejs-diagram>
</div>
</template>
<script>
import { DiagramComponent } from '@syncfusion/ej2-vue-diagrams';
export default {
name: "App",
components: {
"ejs-diagram": DiagramComponent
},
data() {
return {
width: "100%",
height: "600px",
rulerSettings: {
showRulers: true,
// Display the ruler values in inches.
measurementUnit: 'Inch'
}
}
},
methods: {
// Change the ruler measurement unit at runtime.
onUnitChange(args) {
const diagram = this.$refs.diagramObj.ej2Instances;
diagram.rulerSettings.measurementUnit = args.target.value;
diagram.dataBind();
}
}
}
</script>
<style>
@import "../node_modules/@syncfusion/ej2-vue-diagrams/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-base/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-popups/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-splitbuttons/styles/tailwind3.css";
@import "../node_modules/@syncfusion/ej2-navigations/styles/tailwind3.css";
</style>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 diagram instance obtained using this.$refs.diagramObj.ej2Instances.
let centimeters = diagram.convertUnit(300, 'Pixel', 'Centimeter');
let inches = diagram.convertUnit(96, 'Pixel', 'Inch');
let pixels = diagram.convertUnit(8, 'Centimeter', 'Pixel');Marker color
The HorizontalRuler’s markerColor and VerticalRuler’s markerColor properties are used to define the ruler marker color and marker will be shown while hovering mouse over the diagram canvas.
The MarkerColor property can be customized using the [
marker] CSS style.