-
Notifications
You must be signed in to change notification settings - Fork 346
/
type.js
222 lines (187 loc) 路 4.59 KB
/
type.js
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import ChartInternal from "./ChartInternal";
import {isString, isArray, extend} from "./util";
// defined chart types as category
const TYPES = {
Area: ["area", "area-spline", "area-spline-range", "area-line-range", "area-step"],
AreaRange: ["area-spline-range", "area-line-range"],
Arc: ["pie", "donut", "gauge", "radar"],
Line: ["line", "spline", "area", "area-spline", "area-spline-range", "area-line-range", "step", "area-step"],
Step: ["step", "area-step"],
Spline: ["spline", "area-spline", "area-spline-range"]
};
extend(ChartInternal.prototype, {
setTargetType(targetIds, type) {
const $$ = this;
const config = $$.config;
$$.mapToTargetIds(targetIds).forEach(id => {
$$.withoutFadeIn[id] = (type === config.data_types[id]);
config.data_types[id] = type;
});
if (!targetIds) {
config.data_type = type;
}
},
hasType(type, targetsValue) {
const $$ = this;
const types = $$.config.data_types;
const targets = targetsValue || $$.data.targets;
let has = false;
if (targets && targets.length) {
targets.forEach(target => {
const t = types[target.id];
if ((t && t.indexOf(type) >= 0) || (!t && type === "line")) {
has = true;
}
});
} else if (Object.keys(types).length) {
Object.keys(types).forEach(id => {
if (types[id] === type) {
has = true;
}
});
} else {
has = $$.config.data_type === type;
}
return has;
},
/**
* Check if contains given chart types
* @param {String} type Type key
* @param {Object} targets
* @param {Array} exclude Excluded types
* @return {boolean}
* @private
*/
hasTypeOf(type, targets, exclude = []) {
return !TYPES[type]
.filter(v => exclude.indexOf(v) === -1)
.every(v => !this.hasType(v, targets));
},
/**
* Check if given data is certain chart type
* @param {Object} d Data object
* @param {String} type chart type
* @return {Boolean}
* @private
*/
isTypeOf(d, type) {
const id = isString(d) ? d : d.id;
const dataType = this.config.data_types[id];
return isArray(type) ?
type.indexOf(dataType) >= 0 : dataType === type;
},
/**
* Check if contains arc types chart
* @param {Object} targets
* @param {Array} exclude Excluded types
* @return {boolean}
* @private
*/
hasArcType(targets, exclude) {
return this.hasTypeOf("Arc", targets, exclude);
},
hasMultiArcGauge() {
return this.hasType("gauge") && this.config.gauge_type === "multi";
},
isLineType(d) {
const id = isString(d) ? d : d.id;
return !this.config.data_types[id] ||
this.isTypeOf(id, TYPES.Line);
},
isStepType(d) {
return this.isTypeOf(d, TYPES.Step);
},
isSplineType(d) {
return this.isTypeOf(d, TYPES.Spline);
},
isAreaType(d) {
return this.isTypeOf(d, TYPES.Area);
},
isAreaRangeType(d) {
return this.isTypeOf(d, TYPES.AreaRange);
},
isBarType(d) {
return this.isTypeOf(d, "bar");
},
isBubbleType(d) {
return this.isTypeOf(d, "bubble");
},
isScatterType(d) {
return this.isTypeOf(d, "scatter");
},
isPieType(d) {
return this.isTypeOf(d, "pie");
},
isGaugeType(d) {
return this.isTypeOf(d, "gauge");
},
isDonutType(d) {
return this.isTypeOf(d, "donut");
},
isRadarType(d) {
return this.isTypeOf(d, "radar");
},
isArcType(d) {
return this.isPieType(d) ||
this.isDonutType(d) ||
this.isGaugeType(d) ||
this.isRadarType(d);
},
// determine if is 'circle' data point
isCirclePoint() {
const config = this.config;
const pattern = config.point_pattern;
return config.point_type === "circle" &&
(!pattern || (isArray(pattern) && pattern.length === 0));
},
lineData(d) {
return this.isLineType(d) ? [d] : [];
},
arcData(d) {
return this.isArcType(d.data) ? [d] : [];
},
barData(d) {
return this.isBarType(d) ? d.values : [];
},
/**
* Get data adapt for data label showing
* @param {Object} d Data object
* @return {Array}
* @private
*/
labelishData(d) {
return this.isBarType(d) ||
this.isLineType(d) ||
this.isScatterType(d) ||
this.isBubbleType(d) ||
this.isRadarType(d) ? d.values : [];
},
barLineBubbleData(d) {
return this.isBarType(d) || this.isLineType(d) || this.isBubbleType(d) ?
d.values : [];
},
// https://github.com/d3/d3-shape#curves
isInterpolationType(type) {
return [
"basis",
"basis-closed",
"basis-open",
"bundle",
"cardinal",
"cardinal-closed",
"cardinal-open",
"catmull-rom",
"catmull-rom-closed",
"catmull-rom-open",
"linear",
"linear-closed",
"monotone-x",
"monotone-y",
"natural"
].indexOf(type) >= 0;
}
});