Skip to content

Commit 2ce79a3

Browse files
committed
fix: keep reversed scale titles aligned
1 parent ab79c3b commit 2ce79a3

2 files changed

Lines changed: 81 additions & 4 deletions

File tree

src/core/core.scale.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,10 @@ function createTickContext(parent, index, tick) {
119119
});
120120
}
121121

122-
function titleAlign(align, position, reverse) {
122+
function titleAlign(align, position) {
123123
/** @type {CanvasTextAlign} */
124124
let ret = _toLeftRightCenter(align);
125-
if ((reverse && position !== 'right') || (!reverse && position === 'right')) {
125+
if (position === 'right') {
126126
ret = reverseAlign(ret);
127127
}
128128
return ret;
@@ -1586,7 +1586,7 @@ export default class Scale extends Element {
15861586
* @protected
15871587
*/
15881588
drawTitle() {
1589-
const {ctx, options: {position, title, reverse}} = this;
1589+
const {ctx, options: {position, title}} = this;
15901590

15911591
if (!title.display) {
15921592
return;
@@ -1612,7 +1612,7 @@ export default class Scale extends Element {
16121612
color: title.color,
16131613
maxWidth,
16141614
rotation,
1615-
textAlign: titleAlign(align, position, reverse),
1615+
textAlign: titleAlign(align, position),
16161616
textBaseline: 'middle',
16171617
translation: [titleX, titleY],
16181618
strokeColor: title.strokeColor,

test/specs/core.scale.tests.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,6 +818,83 @@ describe('Core.scale', function() {
818818
});
819819
});
820820
describe('Scale Title stroke', ()=>{
821+
function getScaleTitleCalls(position, align, reverse) {
822+
const axis = position === 'left' || position === 'right' ? 'y' : 'x';
823+
const chart = window.acquireChart({
824+
type: 'scatter',
825+
data: {
826+
datasets: [{
827+
data: [{x: 0, y: 0}, {x: 10, y: 10}]
828+
}]
829+
},
830+
options: {
831+
events: [],
832+
animation: false,
833+
plugins: {
834+
legend: false
835+
},
836+
scales: {
837+
x: {
838+
display: axis === 'x',
839+
position: axis === 'x' ? position : 'bottom',
840+
min: 0,
841+
max: 10,
842+
reverse: axis === 'x' ? reverse : false,
843+
ticks: {
844+
display: false
845+
},
846+
grid: {
847+
display: false
848+
},
849+
border: {
850+
display: false
851+
},
852+
title: {
853+
display: axis === 'x',
854+
text: 'title',
855+
align
856+
}
857+
},
858+
y: {
859+
display: axis === 'y',
860+
position: axis === 'y' ? position : 'left',
861+
min: 0,
862+
max: 10,
863+
reverse: axis === 'y' ? reverse : false,
864+
ticks: {
865+
display: false
866+
},
867+
grid: {
868+
display: false
869+
},
870+
border: {
871+
display: false
872+
},
873+
title: {
874+
display: axis === 'y',
875+
text: 'title',
876+
align
877+
}
878+
}
879+
}
880+
}
881+
});
882+
const scale = chart.scales[axis];
883+
884+
scale.ctx = window.createMockContext();
885+
chart.draw();
886+
887+
return scale.ctx.getCalls().filter(({name}) => ['translate', 'setTextAlign', 'fillText'].includes(name));
888+
}
889+
890+
['top', 'bottom', 'left', 'right'].forEach(function(position) {
891+
['start', 'end'].forEach(function(align) {
892+
it('should not reposition the ' + position + ' scale title when reverse=true and align=' + align, function() {
893+
expect(getScaleTitleCalls(position, align, true)).toEqual(getScaleTitleCalls(position, align, false));
894+
});
895+
});
896+
});
897+
821898
function getChartWithScaleTitleStroke() {
822899
return window.acquireChart({
823900
type: 'line',

0 commit comments

Comments
 (0)