-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtitle.ts
113 lines (103 loc) · 2.45 KB
/
title.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { deepMix } from '@antv/util';
import { applyStyle } from '../shape/utils';
import { subObject } from '../utils/helper';
import { GuideComponentComponent as GCC, G2TitleOptions } from '../runtime';
import { createComponent, maybeAppend } from './utils';
export type TitleComponentOptions = G2TitleOptions;
type TitleStyleProps = G2TitleOptions & {
x: number;
y: number;
width: number;
height: number;
};
function inferStyleByAlign(
x: number,
y: number,
width: number,
align: TitleStyleProps['align'],
) {
switch (align) {
case 'center':
return {
x: x + width / 2,
y,
textAlign: 'middle',
};
case 'right':
return {
x: x + width,
y,
textAlign: 'right',
};
default:
return {
x,
y,
textAlign: 'left',
};
}
}
const Title = createComponent<TitleStyleProps>({
render(attributes, container) {
const {
width,
title,
subtitle,
spacing = 2,
align = 'left',
x,
y,
...style
} = attributes;
container.style.transform = `translate(${x}, ${y})`;
const titleStyle = subObject(style, 'title');
const subtitleStyle = subObject(style, 'subtitle');
const mainTitle = maybeAppend(container, '.title', 'text')
.attr('className', 'title')
.call(applyStyle, {
...inferStyleByAlign(0, 0, width, align),
fontSize: 14,
textBaseline: 'top',
text: title,
...titleStyle,
})
.node();
const bounds = mainTitle.getLocalBounds();
maybeAppend(container, '.sub-title', 'text')
.attr('className', 'sub-title')
.call((selection) => {
if (!subtitle) return selection.node().remove();
selection.node().attr({
...inferStyleByAlign(0, bounds.max[1] + spacing, width, align),
fontSize: 12,
textBaseline: 'top',
text: subtitle,
...subtitleStyle,
});
});
},
});
/**
* Title Component.
*/
export const TitleComponent: GCC<TitleComponentOptions> = (options) => {
return ({ value, theme }) => {
const { x, y, width, height } = value.bbox;
return new Title({
style: deepMix({}, theme.title, {
x,
y,
width,
height,
...options,
}),
});
};
};
TitleComponent.props = {
defaultPosition: 'top',
defaultOrder: 2,
defaultSize: 36,
defaultCrossPadding: [20, 20],
defaultPadding: [12, 12],
};