Skip to content

Commit

Permalink
fix: 修复 interval 类型 label 在转置直角坐标系下 position 计算错误问题
Browse files Browse the repository at this point in the history
  • Loading branch information
simaQ committed Mar 20, 2020
1 parent c0fd91a commit 39d0212
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/component/labels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ export default class Labels {
if ((content.isGroup && content.isGroup()) || (content.isShape && content.isShape())) {
// 如果 content 是 Group 或者 Shape,根据 textAlign 调整位置后,直接将其加入 labelGroup
const { width, height } = content.getCanvasBBox();
const textAlign = cfg.textAlign || 'left';
const textAlign = get(cfg, 'textAlign', 'left');

let x = cfg.x;
const y = cfg.y - (height / 2);
Expand All @@ -187,6 +187,7 @@ export default class Labels {
x: cfg.x,
y: cfg.y,
textAlign: cfg.textAlign,
textBaseline: get(cfg, 'textBaseline', 'middle'),
text: cfg.content,
...cfg.style,
},
Expand Down
1 change: 1 addition & 0 deletions src/geometry/label/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,7 @@ export default class GeometryLabel {
}

if (labelCfg.position) {
// 如果 label 支持 position 属性
this.setLabelPosition(label, mappingData, index, labelCfg.position);
}
const offsetPoint = this.getLabelOffset(labelCfg, index, total);
Expand Down
2 changes: 2 additions & 0 deletions src/geometry/label/interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ export interface LabelPointCfg {
color?: string;
content?: any;
textAlign?: TextAlign;
textBaseline?: string;
rotate?: number;
angle?: number;
r?: number;
Expand All @@ -33,6 +34,7 @@ export interface LabelItem extends GeometryLabelCfg {
color?: string;
content?: any;
textAlign?: TextAlign;
textBaseline?: string;
rotate?: number;
angle?: number;
r?: number;
Expand Down
38 changes: 18 additions & 20 deletions src/geometry/label/interval.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { get } from '@antv/util';

import { MappingDatum, Point } from '../../interface';
import GeometryLabel from './base';
import { LabelPointCfg } from './interface';
Expand All @@ -12,56 +14,52 @@ export default class IntervalLabel extends GeometryLabel {
const shapePoints = mappingData.points as Point[];
const point0 = coordinate.convert(shapePoints[0]);
const point1 = coordinate.convert(shapePoints[2]);
const width = ((point0.x - point1.x) / 2) * (transposed ? -1 : 1);
const height = ((point0.y - point1.y) / 2) * (transposed ? -1 : 1);
const flag = transposed ? -1 : 1;
const width = ((point0.x - point1.x) / 2) * flag;
const height = ((point0.y - point1.y) / 2) * flag;

switch (position) {
case 'right':
if (transposed) {
labelPointCfg.x -= width;
labelPointCfg.y += height;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'center';
} else {
if (!transposed) {
labelPointCfg.x -= width;
labelPointCfg.y += height;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'left';
}
labelPointCfg.textAlign = get(labelPointCfg, 'textAlign', 'left');
break;
case 'left':
if (transposed) {
labelPointCfg.x -= width;
labelPointCfg.y -= height;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'center';
labelPointCfg.x -= width * 2;
} else {
labelPointCfg.x += width;
labelPointCfg.y += height;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'right';
}
labelPointCfg.textAlign = get(labelPointCfg, 'textAlign', 'right');
break;
case 'bottom':
if (transposed) {
labelPointCfg.x -= width * 2;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'left';
labelPointCfg.x -= width;
labelPointCfg.y -= height;
} else {
labelPointCfg.y += height * 2;
labelPointCfg.textAlign = labelPointCfg.textAlign || 'center';
}

labelPointCfg.textAlign = get(labelPointCfg, 'textAlign', 'center');
labelPointCfg.textBaseline = get(labelPointCfg, 'textBaseline', 'top');
break;
case 'middle':
if (transposed) {
labelPointCfg.x -= width;
} else {
labelPointCfg.y += height;
}
labelPointCfg.textAlign = labelPointCfg.textAlign || 'center';
labelPointCfg.textAlign = get(labelPointCfg, 'textAlign', 'center');
break;
case 'top':
if (transposed) {
labelPointCfg.textAlign = labelPointCfg.textAlign || 'left';
} else {
labelPointCfg.textAlign = labelPointCfg.textAlign || 'center';
labelPointCfg.x -= width;
labelPointCfg.y += height;
}
labelPointCfg.textAlign = get(labelPointCfg, 'textAlign', 'center');
labelPointCfg.textBaseline = get(labelPointCfg, 'textBaseline', 'bottom');
break;
default:
break;
Expand Down
2 changes: 0 additions & 2 deletions src/theme/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1231,7 +1231,6 @@ export function getThemeByStylesheet(styleSheet: StyleSheet) {
style: {
fill: styleSheet.labelFillColor,
fontSize: styleSheet.labelFontSize,
textBaseline: 'middle',
fontFamily: styleSheet.fontFamily,
stroke: styleSheet.labelBorderColor,
lineWidth: styleSheet.labelBorder,
Expand All @@ -1242,7 +1241,6 @@ export function getThemeByStylesheet(styleSheet: StyleSheet) {
style: {
fill: styleSheet.innerLabelFillColor,
fontSize: styleSheet.innerLabelFontSize,
textBaseline: 'middle',
fontFamily: styleSheet.fontFamily,
stroke: styleSheet.innerLabelBorderColor,
lineWidth: styleSheet.innerLabelBorder,
Expand Down

0 comments on commit 39d0212

Please sign in to comment.