Skip to content

Commit ae0c8dd

Browse files
align text inside chart for far-aligned labels passing test (#8359)
* align text inside chart for far-aligned labels * added fix for failing test on suggestion of @kurkle Co-authored-by: Marcel Samyn <marcel.samyn@lab900.com>
1 parent ad84d28 commit ae0c8dd

File tree

5 files changed

+96
-3
lines changed

5 files changed

+96
-3
lines changed

src/core/core.scale.js

Lines changed: 31 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,23 @@ export default class Scale extends Element {
15131514
return {textAlign, x};
15141515
}
15151516

1517+
/**
1518+
* @private
1519+
*/
1520+
_computeLabelArea() {
1521+
const me = this;
1522+
const chart = me.chart;
1523+
const position = me.options.position;
1524+
1525+
if (position === 'left' || position === 'right') {
1526+
return {top: 0, left: me.left, bottom: chart.height, right: me.right};
1527+
} if (position === 'top' || position === 'bottom') {
1528+
return {top: me.top, left: 0, bottom: me.bottom, right: chart.width};
1529+
}
1530+
1531+
return null;
1532+
}
1533+
15161534
/**
15171535
* @protected
15181536
*/
@@ -1604,6 +1622,12 @@ export default class Scale extends Element {
16041622
}
16051623

16061624
const ctx = me.ctx;
1625+
1626+
const area = me._computeLabelArea();
1627+
if (area) {
1628+
clipArea(ctx, area);
1629+
}
1630+
16071631
const items = me._labelItems || (me._labelItems = me._computeLabelItems(chartArea));
16081632
let i, ilen;
16091633

@@ -1614,6 +1638,10 @@ export default class Scale extends Element {
16141638
let y = item.textOffset;
16151639
renderText(ctx, label, 0, y, tickFont, item);
16161640
}
1641+
1642+
if (area) {
1643+
unclipArea(ctx);
1644+
}
16171645
}
16181646

16191647
/**
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
module.exports = {
2+
config: {
3+
type: 'bar',
4+
data: {
5+
datasets: [{
6+
data: [1, 2, 3],
7+
}],
8+
labels: ['Long long long long label 1', 'Label 2', 'Less more longer label 3']
9+
},
10+
options: {
11+
indexAxis: 'y',
12+
scales: {
13+
y: {
14+
position: 'left',
15+
ticks: {
16+
crossAlign: 'far',
17+
},
18+
afterFit: axis => {
19+
axis.width = 64;
20+
},
21+
},
22+
}
23+
}
24+
},
25+
options: {
26+
spriteText: true,
27+
canvas: {
28+
height: 256,
29+
width: 512
30+
}
31+
}
32+
};
11.5 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
module.exports = {
2+
config: {
3+
type: 'bar',
4+
data: {
5+
datasets: [{
6+
data: [1, 2, 3],
7+
}],
8+
labels: ['Long long long long label 1', 'Label 2', 'Less more longer label 3']
9+
},
10+
options: {
11+
indexAxis: 'y',
12+
scales: {
13+
y: {
14+
position: 'right',
15+
ticks: {
16+
crossAlign: 'far',
17+
},
18+
afterFit: axis => {
19+
axis.width = 64;
20+
},
21+
},
22+
}
23+
}
24+
},
25+
options: {
26+
spriteText: true,
27+
canvas: {
28+
height: 256,
29+
width: 512
30+
}
31+
},
32+
tolerance: 0.1
33+
};
11.8 KB
Loading

0 commit comments

Comments
 (0)