-
Notifications
You must be signed in to change notification settings - Fork 356
/
Copy pathclip.ts
147 lines (121 loc) · 3.84 KB
/
clip.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
export default {
initClip(): void {
const $$ = this;
const {clip, datetimeId} = $$.state;
// MEMO: clipId needs to be unique because it conflicts when multiple charts exist
clip.id = `${datetimeId}-clip`;
clip.idXAxis = `${clip.id}-xaxis`;
clip.idYAxis = `${clip.id}-yaxis`;
clip.idGrid = `${clip.id}-grid`;
// Define 'clip-path' attribute values
clip.path = $$.getClipPath(clip.id);
clip.pathXAxis = $$.getClipPath(clip.idXAxis);
clip.pathYAxis = $$.getClipPath(clip.idYAxis);
clip.pathGrid = $$.getClipPath(clip.idGrid);
},
getClipPath(id: string): string | null {
const $$ = this;
const {config} = $$;
if (
(!config.clipPath && /-clip$/.test(id)) ||
(!config.axis_x_clipPath && /-clip-xaxis$/.test(id)) ||
(!config.axis_y_clipPath && /-clip-yaxis$/.test(id))
) {
return null;
}
return `url(#${id})`;
},
appendClip(parent, id: string): void {
id && parent.append("clipPath")
.attr("id", id)
.append("rect");
},
/**
* Set x Axis clipPath dimension
* @param {d3Selecton} node clipPath <rect> selection
* @private
*/
setXAxisClipPath(node): void {
const $$ = this;
const {config, state: {margin, width, height}} = $$;
const isRotated = config.axis_rotated;
const left = Math.max(30, margin.left) - (isRotated ? 0 : 20);
// less than 20 is not enough to show the axis label 'outer' without legend
const h = (isRotated ? (margin.top + height) + 10 : margin.bottom) + 20;
const x = isRotated ? -(1 + left) : -(left - 1);
const y = -15; // -Math.max(15, margin.top);
const w = isRotated ? margin.left + 20 : width + 10 + left;
node
.attr("x", x)
.attr("y", y)
.attr("width", w)
.attr("height", h);
},
/**
* Set y Axis clipPath dimension
* @param {d3Selection} node clipPath <rect> selection
* @private
*/
setYAxisClipPath(node): void {
const $$ = this;
const {config, state: {margin, width, height}} = $$;
const isRotated = config.axis_rotated;
const left = Math.max(30, margin.left) - (isRotated ? 20 : 0);
const isInner = config.axis_y_inner;
const x = isInner && !isRotated ?
(config.axis_y_label.text ? -20 : -1) :
(isRotated ? -(1 + left) : -(left - 1));
const y = -(isRotated ? 20 : margin.top);
const w = (isRotated ? width + 15 + left : margin.left + 20) + (isInner ? 20 : 0);
const h = (isRotated ? margin.bottom + 10 : (margin.top + height)) + 10;
node
.attr("x", x)
.attr("y", y)
.attr("width", w)
.attr("height", h);
},
updateXAxisTickClip(): void {
const $$ = this;
const {config, state: {clip, xAxisHeight}, $el: {defs}} = $$;
const newXAxisHeight = $$.getHorizontalAxisHeight("x");
if (defs && !clip.idXAxisTickTexts) {
const clipId = `${clip.id}-xaxisticktexts`;
$$.appendClip(defs, clipId);
clip.pathXAxisTickTexts = $$.getClipPath(clip.idXAxisTickTexts);
clip.idXAxisTickTexts = clipId;
}
if (
!config.axis_x_tick_multiline &&
$$.getAxisTickRotate("x") &&
newXAxisHeight !== xAxisHeight
) {
$$.setXAxisTickClipWidth();
$$.setXAxisTickTextClipPathWidth();
}
$$.state.xAxisHeight = newXAxisHeight;
},
setXAxisTickClipWidth(): void {
const $$ = this;
const {config, state: {current: {maxTickSize}}} = $$;
const xAxisTickRotate = $$.getAxisTickRotate("x");
if (!config.axis_x_tick_multiline && xAxisTickRotate) {
const sinRotation = Math.sin(Math.PI / 180 * Math.abs(xAxisTickRotate));
maxTickSize.x.clipPath = ($$.getHorizontalAxisHeight("x") - 20) / sinRotation;
} else {
maxTickSize.x.clipPath = null;
}
},
setXAxisTickTextClipPathWidth(): void {
const $$ = this;
const {state: {clip, current}, $el: {svg}} = $$;
if (svg) {
svg.select(`#${clip.idXAxisTickTexts} rect`)
.attr("width", current.maxTickSize.x.clipPath)
.attr("height", 30);
}
}
};