Skip to content

Commit ad6df1d

Browse files
authored
Merge pull request apache#6951 from brendonboshell/containlabel-rotate2
apache#5148: Consider label rotation when grid.containLabel is true
2 parents 8800c72 + 13c41d7 commit ad6df1d

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/coord/cartesian/Grid.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
*/
66

77
import * as zrUtil from 'zrender/src/core/util';
8+
import * as BoundingRect from 'zrender/src/core/BoundingRect';
89
import {getLayoutRect} from '../../util/layout';
910
import * as axisHelper from '../../coord/axisHelper';
1011
import Cartesian2D from './Cartesian2D';
@@ -26,6 +27,18 @@ function isAxisUsedInTheGrid(axisModel, gridModel, ecModel) {
2627
return axisModel.getCoordSysModel() === gridModel;
2728
}
2829

30+
function rotateTextRect(textRect, rotate) {
31+
var rotateRadians = rotate * Math.PI / 180;
32+
var boundingBox = textRect.plain();
33+
var beforeWidth = boundingBox.width;
34+
var beforeHeight = boundingBox.height;
35+
var afterWidth = beforeWidth * Math.cos(rotateRadians) + beforeHeight * Math.sin(rotateRadians);
36+
var afterHeight = beforeWidth * Math.sin(rotateRadians) + beforeHeight * Math.cos(rotateRadians);
37+
var rotatedRect = new BoundingRect(boundingBox.x, boundingBox.y, afterWidth, afterHeight);
38+
39+
return rotatedRect;
40+
}
41+
2942
function getLabelUnionRect(axis) {
3043
var axisModel = axis.model;
3144
var labels = axisModel.getFormattedLabels();
@@ -39,8 +52,9 @@ function getLabelUnionRect(axis) {
3952
}
4053
for (var i = 0; i < labelCount; i += step) {
4154
if (!axis.isLabelIgnored(i)) {
42-
var singleRect = axisLabelModel.getTextRect(labels[i]);
43-
// FIXME consider label rotate
55+
var unrotatedSingleRect = axisLabelModel.getTextRect(labels[i]);
56+
var singleRect = rotateTextRect(unrotatedSingleRect, axisLabelModel.get('rotate') || 0);
57+
4458
rect ? rect.union(singleRect) : (rect = singleRect);
4559
}
4660
}
@@ -625,4 +639,4 @@ Grid.dimensions = Grid.prototype.dimensions = Cartesian2D.prototype.dimensions;
625639

626640
CoordinateSystem.register('cartesian2d', Grid);
627641

628-
export default Grid;
642+
export default Grid;

0 commit comments

Comments
 (0)