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

align text inside chart for far-aligned labels passing test #8359

Merged
merged 3 commits into from
Jan 31, 2021
Merged
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
34 changes: 31 additions & 3 deletions src/core/core.scale.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import defaults from './core.defaults';
import Element from './core.element';
import {_alignPixel, _measureText, renderText} from '../helpers/helpers.canvas';
import {_alignPixel, _measureText, renderText, clipArea, unclipArea} from '../helpers/helpers.canvas';
import {callback as call, each, finiteOrDefault, isArray, isFinite, isNullOrUndef, isObject, valueOrDefault} from '../helpers/helpers.core';
import {_factorize, toDegrees, toRadians, _int16Range, HALF_PI} from '../helpers/helpers.math';
import {toFont, resolve, toPadding} from '../helpers/helpers.options';
Expand Down Expand Up @@ -1468,6 +1468,7 @@ export default class Scale extends Element {
const labelSizes = me._getLabelSizes();
const tickAndPadding = tl + padding;
const widest = labelSizes.widest.width;
const lineSpace = labelSizes.highest.offset * 0.8;

let textAlign;
let x;
Expand All @@ -1486,7 +1487,7 @@ export default class Scale extends Element {
x -= (widest / 2);
} else {
textAlign = 'left';
x -= widest;
x = me.left + lineSpace;
}
}
} else if (position === 'right') {
Expand All @@ -1503,7 +1504,7 @@ export default class Scale extends Element {
x += widest / 2;
} else {
textAlign = 'right';
x += widest;
x = me.right - lineSpace;
}
}
} else {
Expand All @@ -1513,6 +1514,23 @@ export default class Scale extends Element {
return {textAlign, x};
}

/**
* @private
*/
_computeLabelArea() {
const me = this;
const chart = me.chart;
const position = me.options.position;

if (position === 'left' || position === 'right') {
return {top: 0, left: me.left, bottom: chart.height, right: me.right};
} if (position === 'top' || position === 'bottom') {
return {top: me.top, left: 0, bottom: me.bottom, right: chart.width};
}

return null;
}

/**
* @protected
*/
Expand Down Expand Up @@ -1604,6 +1622,12 @@ export default class Scale extends Element {
}

const ctx = me.ctx;

const area = me._computeLabelArea();
if (area) {
clipArea(ctx, area);
}

const items = me._labelItems || (me._labelItems = me._computeLabelItems(chartArea));
let i, ilen;

Expand All @@ -1614,6 +1638,10 @@ export default class Scale extends Element {
let y = item.textOffset;
renderText(ctx, label, 0, y, tickFont, item);
}

if (area) {
unclipArea(ctx);
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
module.exports = {
config: {
type: 'bar',
data: {
datasets: [{
data: [1, 2, 3],
}],
labels: ['Long long long long label 1', 'Label 2', 'Less more longer label 3']
},
options: {
indexAxis: 'y',
scales: {
y: {
position: 'left',
ticks: {
crossAlign: 'far',
},
afterFit: axis => {
axis.width = 64;
},
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
}
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
module.exports = {
config: {
type: 'bar',
data: {
datasets: [{
data: [1, 2, 3],
}],
labels: ['Long long long long label 1', 'Label 2', 'Less more longer label 3']
},
options: {
indexAxis: 'y',
scales: {
y: {
position: 'right',
ticks: {
crossAlign: 'far',
},
afterFit: axis => {
axis.width = 64;
},
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 512
}
},
tolerance: 0.1
};
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.