Skip to content

Commit c0e8baf

Browse files
align text inside chart for far-aligned labels
1 parent df1303f commit c0e8baf

File tree

1 file changed

+44
-3
lines changed

1 file changed

+44
-3
lines changed

src/core/core.scale.js

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import defaults from './core.defaults';
22
import Element from './core.element';
3-
import {_alignPixel, _measureText, renderText} from '../helpers/helpers.canvas';
3+
import {_alignPixel, _measureText, renderText, clipArea, unclipArea} from '../helpers/helpers.canvas';
44
import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUndef, isObject, valueOrDefault} from '../helpers/helpers.core';
55
import {_factorize, toDegrees, toRadians, _int16Range, HALF_PI} from '../helpers/helpers.math';
66
import {toFont, resolve, toPadding} from '../helpers/helpers.options';
@@ -1468,6 +1468,7 @@ export default class Scale extends Element {
14681468
const labelSizes = me._getLabelSizes();
14691469
const tickAndPadding = tl + padding;
14701470
const widest = labelSizes.widest.width;
1471+
const lineSpace = labelSizes.highest.offset * 0.8;
14711472

14721473
let textAlign;
14731474
let x;
@@ -1486,7 +1487,7 @@ export default class Scale extends Element {
14861487
x -= (widest / 2);
14871488
} else {
14881489
textAlign = 'left';
1489-
x -= widest;
1490+
x = me.left + lineSpace;
14901491
}
14911492
}
14921493
} else if (position === 'right') {
@@ -1503,7 +1504,7 @@ export default class Scale extends Element {
15031504
x += widest / 2;
15041505
} else {
15051506
textAlign = 'right';
1506-
x += widest;
1507+
x = me.right - lineSpace;
15071508
}
15081509
}
15091510
} else {
@@ -1513,6 +1514,36 @@ export default class Scale extends Element {
15131514
return {textAlign, x};
15141515
}
15151516

1517+
/**
1518+
* @private
1519+
*/
1520+
_computeLabelArea() {
1521+
const me = this;
1522+
const position = me.options.position;
1523+
const ctx = me.ctx;
1524+
1525+
const area = {
1526+
left: me.left,
1527+
top: me.top,
1528+
right: me.right,
1529+
bottom: me.bottom,
1530+
};
1531+
1532+
if (position === 'left' || position === 'right') {
1533+
if (me.isHorizontal()) {
1534+
area.left -= me._labelSizes.first.width;
1535+
area.right += me._labelSizes.last.width;
1536+
} else {
1537+
area.top -= me._labelSizes.first.height;
1538+
area.bottom += me._labelSizes.first.height;
1539+
}
1540+
1541+
return area;
1542+
}
1543+
1544+
return null;
1545+
}
1546+
15161547
/**
15171548
* @protected
15181549
*/
@@ -1604,6 +1635,12 @@ export default class Scale extends Element {
16041635
}
16051636

16061637
const ctx = me.ctx;
1638+
1639+
const area = me._computeLabelArea();
1640+
if (area) {
1641+
clipArea(ctx, area);
1642+
}
1643+
16071644
const items = me._labelItems || (me._labelItems = me._computeLabelItems(chartArea));
16081645
let i, ilen;
16091646

@@ -1614,6 +1651,10 @@ export default class Scale extends Element {
16141651
let y = item.textOffset;
16151652
renderText(ctx, label, 0, y, tickFont, item);
16161653
}
1654+
1655+
if (area) {
1656+
unclipArea(ctx);
1657+
}
16171658
}
16181659

16191660
/**

0 commit comments

Comments
 (0)