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

Fix the issue of labels in centre will overlap when alignLabels switc… #1702

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
5 changes: 5 additions & 0 deletions src/gui/price-axis-widget.ts
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,11 @@ export class PriceAxisWidget implements IDestroyable {
}

recalculateOverlapping(top, 1, this._size.height, rendererOptions);
// move the last top to the first bottom to prevent overlapping between the items in top and bottom
if (top.length && bottom.length) {
bottom.splice(0, 0, top.splice(0, 1)[0]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer an implementation which isn't performing splice operations on two arrays. It should be more efficient to rather pass the item from the top array (as a reference without a splice) into the recalculateOverlapping function as a parameter and have the recalculateOverlapping function know how to handle this extra parameter. This might add more complexity.

Or alternatively. Earlier in the current function, we could create the top array, and then sort it. Then create the bottom array and push the desired item into that, and then sort the bottom array.

// split into two parts
const top = views.filter((view: IPriceAxisView) => view.coordinate() <= center);
const bottom = views.filter((view: IPriceAxisView) => view.coordinate() > center);

// sort top from center to top
top.sort((l: IPriceAxisView, r: IPriceAxisView) => r.coordinate() - l.coordinate());

if (top.length && bottom.length) {
	// add the last top to the first bottom to prevent overlapping between the items in top and bottom.
	bottom.push(top[0]);
}

bottom.sort((l: IPriceAxisView, r: IPriceAxisView) => l.coordinate() - r.coordinate());

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, as I remember we already had such string before. There is a risk it brokes the desired behavior on some cases

}

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

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
function runTestCase(container) {
const chartOptions = {
height: 500,
width: 600,
rightPriceScale: {
scaleMargins: {
top: 0,
bottom: 0,
},
entireTextOnly: true,
alignLabels: true,
},
layout: { attributionLogo: false },
};

const chart = (window.chart = LightweightCharts.createChart(
container,
chartOptions
));

const levels = [
7.9,
5.44,
4.23,
4.08,
3.42,
3.09,
2.78,
2.24,
2.14,
2.13,
2.12,
2.05,
2.04,
1.92,
1.69,
1.67,
1.47,
1.32,
1.11,
0.90712,
0.63113,
0.40527,
0.20527,
0.10527,
0.00527,
0.00027,
0.00007,
0.00001,
].map(price => ({ price }));

const colorByIndex = index => {
const r = index & 0b10000 >> 4;
const g = index & 0b01100 >> 2;
const b = index & 0b00011;
return `rgb(${Math.floor(r * 255)}, ${Math.floor(g * 255 / 4)}, ${Math.floor(b * 255 / 4)})`;
};

for (let i = 0; i < levels.length; i++) {
const s = chart.addLineSeries({
color: colorByIndex(i),
});
s.setData([
{ time: 10000, value: levels[i].price },
{ time: 20000, value: levels[i].price },
]);
levels[i].series = s;
}

for (let i = 0; i < 4; i++) {
chart.removeSeries(levels[i].series);
delete levels[i].series;
}

for (let i = 3; i >= 2; i--) {
const s = chart.addLineSeries({
color: colorByIndex(i),
});
s.setData([
{ time: 10000, value: levels[i].price },
{ time: 20000, value: levels[i].price },
]);
levels[i].series = s;
}
}