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

Fix center axis title positions #9413

Merged
merged 3 commits into from Jul 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions src/core/core.scale.js
Expand Up @@ -132,16 +132,36 @@ function titleAlign(align, position, reverse) {
}

function titleArgs(scale, offset, position, align) {
const {top, left, bottom, right} = scale;
const {top, left, bottom, right, chart} = scale;
const {chartArea, scales} = chart;
let rotation = 0;
let maxWidth, titleX, titleY;
const height = bottom - top;
const width = right - left;

if (scale.isHorizontal()) {
titleX = _alignStartEnd(align, left, right);
titleY = offsetFromEdge(scale, position, offset);

if (isObject(position)) {
const positionAxisID = Object.keys(position)[0];
const value = position[positionAxisID];
titleY = scales[positionAxisID].getPixelForValue(value) + height - offset;
} else if (position === 'center') {
titleY = (chartArea.bottom + chartArea.top) / 2 + height - offset;
} else {
titleY = offsetFromEdge(scale, position, offset);
}
maxWidth = right - left;
} else {
titleX = offsetFromEdge(scale, position, offset);
if (isObject(position)) {
const positionAxisID = Object.keys(position)[0];
const value = position[positionAxisID];
titleX = scales[positionAxisID].getPixelForValue(value) - width + offset;
} else if (position === 'center') {
titleX = (chartArea.left + chartArea.right) / 2 - width + offset;
} else {
titleX = offsetFromEdge(scale, position, offset);
}
titleY = _alignStartEnd(align, bottom, top);
rotation = position === 'left' ? -HALF_PI : HALF_PI;
}
Expand Down Expand Up @@ -1557,7 +1577,7 @@ export default class Scale extends Element {
const align = title.align;
let offset = font.lineHeight / 2;

if (position === 'bottom') {
if (position === 'bottom' || position === 'center' || isObject(position)) {
offset += padding.bottom;
if (isArray(title.text)) {
offset += font.lineHeight * (title.text.length - 1);
Expand Down
49 changes: 49 additions & 0 deletions test/fixtures/core.scale/title/horizontal-center.js
@@ -0,0 +1,49 @@
module.exports = {
config: {
type: 'line',
options: {
events: [],
scales: {
y: {
type: 'linear',
position: 'left',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'vertical'
}
},
x: {
type: 'linear',
position: 'center',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'horizontal'
}
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 256
},
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions test/fixtures/core.scale/title/horizontal-value.js
@@ -0,0 +1,51 @@
module.exports = {
config: {
type: 'line',
options: {
events: [],
scales: {
y: {
type: 'linear',
position: 'left',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'vertical'
}
},
x: {
type: 'linear',
position: {
y: 40,
},
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'horizontal'
}
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 256
},
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 49 additions & 0 deletions test/fixtures/core.scale/title/vertical-center.js
@@ -0,0 +1,49 @@
module.exports = {
config: {
type: 'line',
options: {
events: [],
scales: {
y: {
type: 'linear',
position: 'center',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'vertical'
}
},
x: {
type: 'linear',
position: 'bottom',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'horizontal'
}
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 256
},
}
};
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
51 changes: 51 additions & 0 deletions test/fixtures/core.scale/title/vertical-value.js
@@ -0,0 +1,51 @@
module.exports = {
config: {
type: 'line',
options: {
events: [],
scales: {
y: {
type: 'linear',
position: {
x: 40
},
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'vertical'
}
},
x: {
type: 'linear',
position: 'bottom',
min: 0,
max: 100,
ticks: {
display: false
},
grid: {
display: false
},
title: {
display: true,
text: 'horizontal'
}
},
}
}
},
options: {
spriteText: true,
canvas: {
height: 256,
width: 256
},
}
};
Binary file added test/fixtures/core.scale/title/vertical-value.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.