-
Notifications
You must be signed in to change notification settings - Fork 345
/
region.ts
173 lines (143 loc) 路 4.19 KB
/
region.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {select as d3Select} from "d3-selection"; // selection
import type {AxisType, RegionsType} from "../../../types/types";
import {$REGION} from "../../config/classes";
import {isValue, parseDate} from "../../module/util";
export default {
initRegion(): void {
const $$ = this;
const {$el} = $$;
$el.region.main = $el.main
.insert("g", ":first-child")
.attr("clip-path", $$.state.clip.path)
.attr("class", $REGION.regions);
},
updateRegion(): void {
const $$ = this;
const {config, $el: {region}, $T} = $$;
if (!region.main) {
$$.initRegion();
}
// hide if arc type
region.main.style("visibility", $$.hasArcType() ? "hidden" : null);
// select <g> element
const regions = region.main
.selectAll(`.${$REGION.region}`)
.data(config.regions);
$T(regions.exit())
.style("opacity", "0")
.remove();
const regionsEnter = regions
.enter()
.append("g");
regionsEnter
.append("rect")
.style("fill-opacity", "0");
region.list = regionsEnter
.merge(regions)
.attr("class", $$.classRegion.bind($$));
region.list.each(function(d) {
const g = d3Select(this);
if (g.select("text").empty() && d.label?.text) {
d3Select(this).append("text")
.style("opacity", "0");
}
});
},
redrawRegion(withTransition: boolean) {
const $$ = this;
const {$el: {region}, $T} = $$;
let regions = region.list.select("rect");
let label = region.list.selectAll("text");
regions = $T(regions, withTransition)
.attr("x", $$.regionX.bind($$))
.attr("y", $$.regionY.bind($$))
.attr("width", $$.regionWidth.bind($$))
.attr("height", $$.regionHeight.bind($$));
label = $T(label, withTransition)
.attr("transform", d => {
const {x = 0, y = 0, rotated = false} = d.label ?? {};
return `translate(${$$.regionX.bind($$)(d) + x}, ${$$.regionY.bind($$)(d) + y})${
rotated ? ` rotate(-90)` : ``
}`;
})
.attr("text-anchor", d => (d.label?.rotated ? "end" : null))
.attr("dy", "1em")
.style("fill", d => d.label?.color ?? null)
.text(d => d.label?.text);
return [
regions
.style("fill-opacity", d => (isValue(d.opacity) ? d.opacity : null))
.on("end", function() {
// remove unnecessary rect after transition
d3Select(this.parentNode)
.selectAll("rect:not([x])")
.remove();
}),
label.style("opacity", null)
];
},
getRegionXY(type: AxisType, d: RegionsType): number {
const $$ = this;
const {config, scale} = $$;
const isRotated = config.axis_rotated;
const isX = type === "x";
let key = "start";
let currScale;
let pos = 0;
if (d.axis === "y" || d.axis === "y2") {
if (!isX) {
key = "end";
}
if ((isX ? isRotated : !isRotated) && key in d) {
currScale = scale[d.axis];
pos = currScale(d[key]);
}
} else if ((isX ? !isRotated : isRotated) && key in d) {
currScale = scale.zoom || scale.x;
pos = currScale($$.axis.isTimeSeries() ? parseDate.call($$, d[key]) : d[key]);
}
return pos;
},
regionX(d: RegionsType): number {
return this.getRegionXY("x", d);
},
regionY(d: RegionsType): number {
return this.getRegionXY("y", d);
},
getRegionSize(type: "width" | "height", d: RegionsType): number {
const $$ = this;
const {config, scale, state} = $$;
const isRotated = config.axis_rotated;
const isWidth = type === "width";
const start = $$[isWidth ? "regionX" : "regionY"](d);
let currScale;
let key = "end";
let end = state[type];
if (d.axis === "y" || d.axis === "y2") {
if (!isWidth) {
key = "start";
}
if ((isWidth ? isRotated : !isRotated) && key in d) {
currScale = scale[d.axis];
end = currScale(d[key]);
}
} else if ((isWidth ? !isRotated : isRotated) && key in d) {
currScale = scale.zoom || scale.x;
end = currScale($$.axis.isTimeSeries() ? parseDate.call($$, d[key]) : d[key]);
}
return end < start ? 0 : end - start;
},
regionWidth(d: RegionsType): number {
return this.getRegionSize("width", d);
},
regionHeight(d: RegionsType): number {
return this.getRegionSize("height", d);
},
isRegionOnX(d: RegionsType): boolean {
return !d.axis || d.axis === "x";
}
};