Skip to content

Commit

Permalink
1620: Fixed review issues
Browse files Browse the repository at this point in the history
  • Loading branch information
eugene-korobko committed Jun 21, 2024
1 parent 7108f6b commit 68fbe34
Showing 1 changed file with 48 additions and 47 deletions.
95 changes: 48 additions & 47 deletions src/gui/price-axis-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,52 @@ function buildPriceAxisViewsGetter(
};
}

function recalculateOverlapping(views: IPriceAxisView[], direction: 1 | -1, scaleHeight: number, rendererOptions: Readonly<PriceAxisViewRendererOptions>): void {
if (!views.length) {
return;
}
let currentGroupStart = 0;

const initLabelHeight = views[0].height(rendererOptions, true);
let spaceBeforeCurrentGroup = direction === 1
? scaleHeight / 2 - (views[0].getFixedCoordinate() - initLabelHeight / 2)
: views[0].getFixedCoordinate() - initLabelHeight / 2 - scaleHeight / 2;
spaceBeforeCurrentGroup = Math.max(0, spaceBeforeCurrentGroup);

for (let i = 1; i < views.length; i++) {
const view = views[i];
const prev = views[i - 1];
const height = prev.height(rendererOptions, false);
const coordinate = view.getFixedCoordinate();
const prevFixedCoordinate = prev.getFixedCoordinate();

const overlap = direction === 1
? coordinate > prevFixedCoordinate - height
: coordinate < prevFixedCoordinate + height;

if (overlap) {
const fixedCoordinate = prevFixedCoordinate - height * direction;
view.setFixedCoordinate(fixedCoordinate);
const edgePoint = fixedCoordinate - direction * height / 2;
const outOfViewport = direction === 1 ? edgePoint < 0 : edgePoint > scaleHeight;
if (outOfViewport && spaceBeforeCurrentGroup > 0) {
// shift the whole group up or down
const desiredGroupShift = direction === 1 ? -1 - edgePoint : edgePoint - scaleHeight;
const possibleShift = Math.min(desiredGroupShift, spaceBeforeCurrentGroup);
for (let k = currentGroupStart; k < views.length; k++) {
views[k].setFixedCoordinate(views[k].getFixedCoordinate() + direction * possibleShift);
}
spaceBeforeCurrentGroup -= possibleShift;
}
} else {
currentGroupStart = i;
spaceBeforeCurrentGroup = direction === 1
? prevFixedCoordinate - height - coordinate
: coordinate - (prevFixedCoordinate + height);
}
}
}

export class PriceAxisWidget implements IDestroyable {
private readonly _pane: PaneWidget;
private readonly _options: Readonly<ChartOptionsInternalBase>;
Expand Down Expand Up @@ -566,51 +612,6 @@ export class PriceAxisWidget implements IDestroyable {
this._fixLabelOverlap(views, rendererOptions, center);
}

private _recalculateOverlapping(views: IPriceAxisView[], direction: 1 | -1, scaleHeight: number, rendererOptions: Readonly<PriceAxisViewRendererOptions>): void {
if (views.length) {
let currentGroupStart = 0;

const initLabelHeight = views[0].height(rendererOptions, true);
let spaceBeforeCurrentGroup = direction === 1
? scaleHeight / 2 - (views[0].getFixedCoordinate() - initLabelHeight / 2)
: views[0].getFixedCoordinate() - initLabelHeight / 2 - scaleHeight / 2;
spaceBeforeCurrentGroup = Math.max(0, spaceBeforeCurrentGroup);

for (let i = 1; i < views.length; i++) {
const view = views[i];
const prev = views[i - 1];
const height = prev.height(rendererOptions, false);
const coordinate = view.getFixedCoordinate();
const prevFixedCoordinate = prev.getFixedCoordinate();

const overlap = direction === 1
? coordinate > prevFixedCoordinate - height
: coordinate < prevFixedCoordinate + height;

if (overlap) {
const fixedCoordinate = prevFixedCoordinate - height * direction;
view.setFixedCoordinate(fixedCoordinate);
const edgePoint = fixedCoordinate - direction * height / 2;
const outOfViewport = direction === 1 ? edgePoint < 0 : edgePoint > scaleHeight;
if (outOfViewport && spaceBeforeCurrentGroup > 0) {
// shift the whole group up or down
const desiredGroupShift = direction === 1 ? -1 - edgePoint : edgePoint - scaleHeight;
const possibleShift = Math.min(desiredGroupShift, spaceBeforeCurrentGroup);
for (let k = currentGroupStart; k < views.length; k++) {
views[k].setFixedCoordinate(views[k].getFixedCoordinate() + direction * possibleShift);
}
spaceBeforeCurrentGroup -= possibleShift;
}
} else {
currentGroupStart = i;
spaceBeforeCurrentGroup = direction === 1
? prevFixedCoordinate - height - coordinate
: coordinate - (prevFixedCoordinate + height);
}
}
}
}

private _fixLabelOverlap(views: IPriceAxisView[], rendererOptions: Readonly<PriceAxisViewRendererOptions>, center: number): void {
if (this._size === null) {
return;
Expand Down Expand Up @@ -642,8 +643,8 @@ export class PriceAxisWidget implements IDestroyable {
}
}

this._recalculateOverlapping(top, 1, this._size.height, rendererOptions);
this._recalculateOverlapping(bottom, -1, this._size.height, rendererOptions);
recalculateOverlapping(top, 1, this._size.height, rendererOptions);
recalculateOverlapping(bottom, -1, this._size.height, rendererOptions);
}

private _drawBackLabels(target: CanvasRenderingTarget2D): void {
Expand Down

0 comments on commit 68fbe34

Please sign in to comment.