Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updating the way of calculate the midPoint #519

Merged
merged 2 commits into from
Jul 28, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 46 additions & 6 deletions projects/swimlane/ngx-graph/src/lib/graph/graph.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1210,15 +1210,55 @@ export class GraphComponent implements OnInit, OnChanges, OnDestroy, AfterViewIn
if (points.length % 2 === 1) {
edge.midPoint = points[Math.floor(points.length / 2)];
} else {
const _first = points[points.length / 2];
const _second = points[points.length / 2 - 1];
edge.midPoint = {
x: (_first.x + _second.x) / 2,
y: (_first.y + _second.y) / 2
};
// Checking if the current layout is Elk
if ((this.layout as Layout)?.settings?.properties?.['elk.direction']) {
this._calcMidPointElk(edge, points);
} else {
const _first = points[points.length / 2];
const _second = points[points.length / 2 - 1];
edge.midPoint = {
x: (_first.x + _second.x) / 2,
y: (_first.y + _second.y) / 2
};
}
}
}

private _calcMidPointElk(edge: Edge, points: any): void {
let _firstX = null;
let _secondX = null;
let _firstY = null;
let _secondY = null;
const orientation = (this.layout as Layout).settings?.properties['elk.direction'];
const hasBend =
orientation === 'RIGHT' ? points.some(p => p.y !== points[0].y) : points.some(p => p.x !== points[0].x);

if (hasBend) {
// getting the last two points
_firstX = points[points.length - 1];
_secondX = points[points.length - 2];
_firstY = points[points.length - 1];
_secondY = points[points.length - 2];
} else {
if (orientation === 'RIGHT') {
_firstX = points[0];
_secondX = points[points.length - 1];
_firstY = points[points.length / 2];
_secondY = points[points.length / 2 - 1];
} else {
_firstX = points[points.length / 2];
_secondX = points[points.length / 2 - 1];
_firstY = points[0];
_secondY = points[points.length - 1];
}
}

edge.midPoint = {
x: (_firstX.x + _secondX.x) / 2,
y: (_firstY.y + _secondY.y) / 2
};
}

public basicUpdate(): void {
if (this.view) {
this.width = this.view[0];
Expand Down
Loading