Skip to content

Commit e2f0648

Browse files
committed
Improved alignment of pixels in scales at low widths
1 parent b2c7baf commit e2f0648

File tree

3 files changed

+19
-17
lines changed

3 files changed

+19
-17
lines changed

src/core/core.scale.js

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,6 @@ export default class Scale extends Element {
999999

10001000
const borderOpts = grid.setContext(me.getContext(0));
10011001
const axisWidth = borderOpts.drawBorder ? borderOpts.borderWidth : 0;
1002-
const axisHalfWidth = axisWidth / 2;
10031002
const alignBorderValue = function(pixel) {
10041003
return _alignPixel(chart, pixel, axisWidth);
10051004
};
@@ -1009,26 +1008,26 @@ export default class Scale extends Element {
10091008
if (position === 'top') {
10101009
borderValue = alignBorderValue(me.bottom);
10111010
ty1 = me.bottom - tl;
1012-
ty2 = borderValue - axisHalfWidth;
1013-
y1 = alignBorderValue(chartArea.top) + axisHalfWidth;
1011+
ty2 = borderValue;
1012+
y1 = alignBorderValue(chartArea.top);
10141013
y2 = chartArea.bottom;
10151014
} else if (position === 'bottom') {
10161015
borderValue = alignBorderValue(me.top);
10171016
y1 = chartArea.top;
1018-
y2 = alignBorderValue(chartArea.bottom) - axisHalfWidth;
1019-
ty1 = borderValue + axisHalfWidth;
1017+
y2 = alignBorderValue(chartArea.bottom);
1018+
ty1 = borderValue;
10201019
ty2 = me.top + tl;
10211020
} else if (position === 'left') {
10221021
borderValue = alignBorderValue(me.right);
10231022
tx1 = me.right - tl;
1024-
tx2 = borderValue - axisHalfWidth;
1025-
x1 = alignBorderValue(chartArea.left) + axisHalfWidth;
1023+
tx2 = borderValue;
1024+
x1 = alignBorderValue(chartArea.left);
10261025
x2 = chartArea.right;
10271026
} else if (position === 'right') {
10281027
borderValue = alignBorderValue(me.left);
10291028
x1 = chartArea.left;
1030-
x2 = alignBorderValue(chartArea.right) - axisHalfWidth;
1031-
tx1 = borderValue + axisHalfWidth;
1029+
x2 = alignBorderValue(chartArea.right);
1030+
tx1 = borderValue;
10321031
tx2 = me.left + tl;
10331032
} else if (axis === 'x') {
10341033
if (position === 'center') {
@@ -1041,7 +1040,7 @@ export default class Scale extends Element {
10411040

10421041
y1 = chartArea.top;
10431042
y2 = chartArea.bottom;
1044-
ty1 = borderValue + axisHalfWidth;
1043+
ty1 = borderValue;
10451044
ty2 = ty1 + tl;
10461045
} else if (axis === 'y') {
10471046
if (position === 'center') {
@@ -1052,7 +1051,7 @@ export default class Scale extends Element {
10521051
borderValue = alignBorderValue(me.chart.scales[positionAxisID].getPixelForValue(value));
10531052
}
10541053

1055-
tx1 = borderValue - axisHalfWidth;
1054+
tx1 = borderValue;
10561055
tx2 = tx1 - tl;
10571056
x1 = chartArea.left;
10581057
x2 = chartArea.right;
@@ -1413,12 +1412,12 @@ export default class Scale extends Element {
14131412
let x1, x2, y1, y2;
14141413

14151414
if (me.isHorizontal()) {
1416-
x1 = _alignPixel(chart, me.left, axisWidth) - axisWidth / 2;
1417-
x2 = _alignPixel(chart, me.right, lastLineWidth) + lastLineWidth / 2;
1415+
x1 = _alignPixel(chart, me.left, axisWidth);
1416+
x2 = _alignPixel(chart, me.right, lastLineWidth);
14181417
y1 = y2 = borderValue;
14191418
} else {
1420-
y1 = _alignPixel(chart, me.top, axisWidth) - axisWidth / 2;
1421-
y2 = _alignPixel(chart, me.bottom, lastLineWidth) + lastLineWidth / 2;
1419+
y1 = _alignPixel(chart, me.top, axisWidth);
1420+
y2 = _alignPixel(chart, me.bottom, lastLineWidth);
14221421
x1 = x2 = borderValue;
14231422
}
14241423
drawLine(

src/helpers/helpers.canvas.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ export function _longestText(ctx, font, arrayOfThings, cache) {
102102
*/
103103
export function _alignPixel(chart, pixel, width) {
104104
const devicePixelRatio = chart.currentDevicePixelRatio;
105-
const halfWidth = width / 2;
105+
const halfWidth = width !== 0 ? Math.max(width / 2, 0.5) : 0;
106106
return Math.round((pixel - halfWidth) * devicePixelRatio) / devicePixelRatio + halfWidth;
107107
}
108108

test/specs/core.scale.tests.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ describe('Core.scale', function() {
199199
chart.draw();
200200

201201
expect(yScale.ctx.getCalls().filter(function(x) {
202-
return x.name === 'moveTo' && x.args[0] === 1;
202+
return x.name === 'moveTo' && x.args[0] === 0.5;
203+
}).filter(function(x, i, arr) {
204+
// Remove the extra point at the end
205+
return i !== arr.length - 1;
203206
}).map(function(x) {
204207
return x.args[1];
205208
})).toEqual(test.expected);

0 commit comments

Comments
 (0)