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.
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 ProjectAn 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.Project } from 'ontouml-js';
const const project: Projectproject = new new Project(): ProjectAn 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.Project();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilderCreates a builder for a new
Package
in this project.packageBuilder().PackageBuilder.root(): PackageBuilderSets the package as the root package of its project, i.e., the package
that contains the entire model.root().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('School').PackageBuilder.build(): PackageBuilds 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: []`build();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets 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`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Person').ClassBuilder.build(): ClassBuilds 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,`build();
const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): DiagramCreates a new
Diagram
and adds it to this project.createDiagram('Overview');
const const personView: ClassViewpersonView = const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassViewCreates a
ClassView
for the given class and adds it to the
diagram.addClass(const person: Classperson);
const personView: ClassViewpersonView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setX(x: number): voidSets the horizontal coordinate of the shape's top-left corner.setX(80);
const personView: ClassViewpersonView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setY(y: number): voidSets the vertical coordinate of the shape's top-left corner.setY(40);
const personView: ClassViewpersonView.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.width: numberThe 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: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.height: numberThe 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[]): voidThe **`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: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.getX(): numberReturns the horizontal coordinate of the shape's top-left corner.getX()); // 80
var console: Consoleconsole.Console.log(...data: any[]): voidThe **`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: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.getY(): numberReturns the vertical coordinate of the shape's top-left corner.getY()); // 40
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 RectangleA rectangle on the diagram canvas. Rectangles render node views such as
ClassView
and
PackageView
.Rectangle, class PointA 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): RectangleA rectangle on the diagram canvas. Rectangles render node views such as
ClassView
and
PackageView
.Rectangle(new new Point(x: number, y: number): PointA 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[]): voidThe **`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(): numberReturns the horizontal coordinate of the shape's top-left corner.getX(), const box: Rectanglebox.RectangularShape.getY(): numberReturns the vertical coordinate of the shape's top-left corner.getY()); // 200 100
var console: Consoleconsole.Console.log(...data: any[]): voidThe **`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: numberThe 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: numberThe 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 ProjectAn 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.Project, class ClassViewThe 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(): ProjectAn 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.Project();
const const model: Packagemodel = const project: Projectproject.Project.packageBuilder(): PackageBuilderCreates a builder for a new
Package
in this project.packageBuilder().PackageBuilder.root(): PackageBuilderSets the package as the root package of its project, i.e., the package
that contains the entire model.root().NamedElementBuilder<PackageBuilder>.name(value: string, language?: string): PackageBuilderAdds a name to the element in the given language.name('School').PackageBuilder.build(): PackageBuilds 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: []`build();
const const person: Classperson = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets 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`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Person').ClassBuilder.build(): ClassBuilds 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,`build();
const const school: Classschool = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets 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`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('School').ClassBuilder.build(): ClassBuilds 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,`build();
const const course: Classcourse = const model: Packagemodel.Package.classBuilder(): ClassBuilderCreates a builder for a
Class
contained in this package.classBuilder().ClassBuilder.kind(): ClassBuilderSets 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`kind().NamedElementBuilder<ClassBuilder>.name(value: string, language?: string): ClassBuilderAdds a name to the element in the given language.name('Course').ClassBuilder.build(): ClassBuilds 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,`build();
const const diagram: Diagramdiagram = const project: Projectproject.Project.createDiagram(name?: string): DiagramCreates a new
Diagram
and adds it to this project.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.map(c: Classc => const diagram: Diagramdiagram.Diagram.addClass(clazz: Class): ClassViewCreates 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 ClassViewThe 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: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setX(x: number): voidSets the horizontal coordinate of the shape's top-left corner.setX(let x: numberx);
const view: ClassViewview.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.setY(y: number): voidSets the vertical coordinate of the shape's top-left corner.setY(top: numbertop);
let x: numberx += const view: ClassViewview.ClassView.rectangle: RectangleThe rectangle that renders the class on the diagram canvas.rectangle.RectangularShape.width: numberThe 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; usesetX/setYand thewidth/heightaccessors. - Positive sizes only — zero or negative dimensions throw.
- A
Point(x, y)can preset aRectangle's top-left corner at construction.
Next: connecting elements →.