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

Adjust legend hitboxes when RTL and each item has a different size #9353

Merged
merged 2 commits into from Jul 10, 2021
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 3 additions & 20 deletions src/plugins/plugin.legend.js
Expand Up @@ -234,6 +234,7 @@ export class Legend extends Element {
}
const titleHeight = me._computeTitleHeight();
const {legendHitBoxes: hitboxes, options: {align, labels: {padding}, rtl}} = me;
const rtlHelper = getRtlAdapter(rtl, me.left, me.width);
if (this.isHorizontal()) {
let row = 0;
let left = _alignStartEnd(align, me.left + padding, me.right - me.lineWidths[row]);
Expand All @@ -243,28 +244,9 @@ export class Legend extends Element {
left = _alignStartEnd(align, me.left + padding, me.right - me.lineWidths[row]);
}
hitbox.top += me.top + titleHeight + padding;
hitbox.left = left;
hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(left), hitbox.width);
left += hitbox.width + padding;
}

if (rtl) {
// When the legend is in RTL mode, each row starts at the right
// To ensure that click handling works correctly, we need to ensure that the items in the
// hitboxes array line up with how the legend items are drawn (this hack is required until V4)
const boxMap = hitboxes.reduce((map, box) => {
map[box.row] = map[box.row] || [];
map[box.row].push(box);
return map;
}, {});

const newBoxes = [];
Object.keys(boxMap).forEach(key => {
boxMap[key].reverse();
newBoxes.push(...boxMap[key]);
});

me.legendHitBoxes = newBoxes;
}
} else {
let col = 0;
let top = _alignStartEnd(align, me.top + titleHeight + padding, me.bottom - me.columnSizes[col].height);
Expand All @@ -275,6 +257,7 @@ export class Legend extends Element {
}
hitbox.top = top;
hitbox.left += me.left + padding;
hitbox.left = rtlHelper.leftForLtr(rtlHelper.x(hitbox.left), hitbox.width);
top += hitbox.height + padding;
}
}
Expand Down