Skip to main content
Version: 1.0.0

Positioning views

A freshly added view sits at the origin (0, 0) with a default size. Laying out a diagram means moving and resizing the shapes behind its views — for a class or package that shape is a Rectangle.

Coordinates

The canvas uses screen coordinates: x grows rightward, y grows downward, and the position of a rectangle is its top-left corner. Units are abstract "diagram units" — treat them like pixels.

Moving and sizing a class view​

A ClassView exposes its box as rectangle. Set its position with setX / setY and its size with the width / height accessors:

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('School').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): Diagram
Creates a new Diagram and adds it to this project.
@paramname - optional name for the diagram, set in the default language.
createDiagram
('Overview');
const const personView: ClassViewpersonView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassView
Creates a ClassView for the given class and adds it to the diagram.
addClass
(const person: Classperson);
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setX(x: number): void
Sets the horizontal coordinate of the shape's top-left corner.
setX
(80);
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setY(y: number): void
Sets the vertical coordinate of the shape's top-left corner.
setY
(40);
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.width: number
The width of the shape's bounding rectangle. Sets the width of the shape's bounding rectangle. Throws an error if the value is negative.
width
= 140;
const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.height: number
The height of the shape's bounding rectangle. Sets the height of the shape's bounding rectangle. Throws an error if the value is negative.
height
= 70;
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.getX(): number
Returns the horizontal coordinate of the shape's top-left corner.
getX
()); // 80
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const personView: ClassViewpersonView.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.getY(): number
Returns the vertical coordinate of the shape's top-left corner.
getY
()); // 40
Width and height must be positive

Assigning a zero or negative width or height throws. A class view defaults to 100 × 50 and a package view to 60 × 30, so you only need to set a size when the default doesn't fit.

Positioning at construction with a Point​

A Point is a simple (x, y) pair. You can hand one to a Rectangle's constructor to place it in one step, which is handy when you build shapes yourself (as with notes, later in this chapter):

import { class Rectangle
A rectangle on the diagram canvas. Rectangles render node views such as ClassView and PackageView .
Rectangle
, class Point
A point in the two-dimensional coordinate space of a Diagram . Points position Shape instances on the diagram canvas and define the vertices of Path polylines.
Point
} from 'ontouml-js';
// A 120×60 box whose top-left corner is at (200, 100). const const box: Rectanglebox = new new Rectangle(topLeft?: Point, width?: number, height?: number): Rectangle
A rectangle on the diagram canvas. Rectangles render node views such as ClassView and PackageView .
Rectangle
(new new Point(x: number, y: number): Point
A point in the two-dimensional coordinate space of a Diagram . Points position Shape instances on the diagram canvas and define the vertices of Path polylines.
Point
(200, 100), 120, 60);
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const box: Rectanglebox.RectangularShape.getX(): number
Returns the horizontal coordinate of the shape's top-left corner.
getX
(), const box: Rectanglebox.RectangularShape.getY(): number
Returns the vertical coordinate of the shape's top-left corner.
getY
()); // 200 100
var console: Consoleconsole.Console.log(...data: any[]): void
The **`console.log()`** static method outputs a message to the console. [MDN Reference](https://developer.mozilla.org/docs/Web/API/console/log_static)
log
(const box: Rectanglebox.RectangularShape.width: number
The width of the shape's bounding rectangle. Sets the width of the shape's bounding rectangle. Throws an error if the value is negative.
width
, const box: Rectanglebox.RectangularShape.height: number
The height of the shape's bounding rectangle. Sets the height of the shape's bounding rectangle. Throws an error if the value is negative.
height
); // 120 60

A tidy row of classes​

Layout is just arithmetic over these setters. Here's a helper that lines views up left to right with even spacing:

import { class Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
, class ClassView
The view of a Class in a Diagram , rendered as a Rectangle (100 × 50 by default).
ClassView
} from 'ontouml-js';
const const project: Projectproject = new new Project(): Project
An OntoUML project: the top-level container of an ontology and the entry point of this library. A project holds every element of a model — classes, relations, generalizations, packages, properties, notes — as well as the diagrams and views that depict them, and provides typed access to these elements by id or by kind. New elements are created through the fluent builders exposed by the project (e.g., classBuilder , binaryRelationBuilder ), which register the created elements in the project. A project is also described by the metadata fields of the OntoUML/UFO Catalog Metadata Vocabulary, such as publisher, license, keywords, themes, and designed-for tasks.
@example```typescript const project = new Project(); const person = project.classBuilder().kind().name('Person').build(); const student = project.classBuilder().role().name('Student').build(); project .generalizationBuilder() .general(person) .specific(student) .build(); ```
Project
();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilder
Creates a builder for a new Package in this project.
packageBuilder
().PackageBuilder.root(): PackageBuilder
Sets the package as the root package of its project, i.e., the package that contains the entire model.
@returnsthis builder, for method chaining.
root
().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('School').PackageBuilder.build(): Package
Builds an instance of Package with the parameters passed to the builder. When no methods are evoked, the created package has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `contents: []`
@throwsan error if the builder has already been used.
build
();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Person').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const school: Classschool = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('School').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const course: Classcourse = const model: Packagemodel.Package.classBuilder(): ClassBuilder
Creates a builder for a Class contained in this package.
classBuilder
().ClassBuilder.kind(): ClassBuilder
Sets the stereotype of the class to «kind», which decorates rigid classes that provide an identity principle to their instances, which are functional complexes (e.g., `Person`, `Car`). Also applies the following defaults: - `restrictedTo = [ "functional-complex" ]` - `order = 1`
@returnsthis builder, for method chaining.
kind
().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilder
Adds a name to the element in the given language.
@paramvalue - the name text.@paramlanguage - language tag of `value`; defaults to LanguageString.defaultLanguage.@returnsthis builder, for method chaining.
name
('Course').ClassBuilder.build(): Class
Builds an instance of Class with the parameters passed to the builder. **WARNING:** the ordering in which methods are evoked may affect the resulting object. When no methods are evoked, the created class has the following defaults: - `id: "randomly-generated-id",` - `created: new Date(),` - `isAbstract: false,` - `isDerived: false,` - `order: 1,` - `isPowertype: false,`
@throwsan error if the builder has already been used.
build
();
const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): Diagram
Creates a new Diagram and adds it to this project.
@paramname - optional name for the diagram, set in the default language.
createDiagram
('Overview');
const const views: ClassView[]views = [const person: Classperson, const school: Classschool, const course: Classcourse].Array<Class>.map<ClassView>(callbackfn: (value: Class, index: number, array: Class[]) => ClassView, thisArg?: any): ClassView[]
Calls a defined callback function on each element of an array, and returns an array that contains the results.
@paramcallbackfn A function that accepts up to three arguments. The map method calls the callbackfn function one time for each element in the array.@paramthisArg An object to which the this keyword can refer in the callbackfn function. If thisArg is omitted, undefined is used as the this value.
map
(c: Classc => const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassView
Creates a ClassView for the given class and adds it to the diagram.
addClass
(c: Classc));
function function layoutRow(views: ClassView[], top: number, gap: number): voidlayoutRow(views: ClassView[]views: class ClassView
The view of a Class in a Diagram , rendered as a Rectangle (100 × 50 by default).
ClassView
[], top: numbertop: number, gap: numbergap: number): void {
let let x: numberx = 40; for (const const view: ClassViewview of views: ClassView[]views) { const view: ClassViewview.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setX(x: number): void
Sets the horizontal coordinate of the shape's top-left corner.
setX
(let x: numberx);
const view: ClassViewview.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.setY(y: number): void
Sets the vertical coordinate of the shape's top-left corner.
setY
(top: numbertop);
let x: numberx += const view: ClassViewview.ClassView.rectangle: Rectangle
The rectangle that renders the class on the diagram canvas.
rectangle
.RectangularShape.width: number
The width of the shape's bounding rectangle. Sets the width of the shape's bounding rectangle. Throws an error if the value is negative.
width
+ gap: numbergap;
} } function layoutRow(views: ClassView[], top: number, gap: number): voidlayoutRow(const views: ClassView[]views, 60, 40);

Key takeaways​

  • New views start at (0, 0); you position them by mutating their shapes.
  • A class/package view's box is view.rectangle; use setX / setY and the width / height accessors.
  • Positive sizes only — zero or negative dimensions throw.
  • A Point(x, y) can preset a Rectangle's top-left corner at construction.

Next: connecting elements →.