-
Notifications
You must be signed in to change notification settings - Fork 345
/
bubble.ts
80 lines (70 loc) 路 1.81 KB
/
bubble.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
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {KEY} from "../../module/Cache";
import {getMinMax, isFunction, isNumber, isObject} from "../../module/util";
export default {
/**
* Initializer
* @private
*/
initBubble(): void {
const $$ = this;
const {config} = $$;
if ($$.hasType("bubble")) {
config.point_show = true;
config.point_type = "circle";
config.point_sensitivity = 25;
}
},
/**
* Get user agent's computed value
* @returns {number}
* @private
*/
getBaseLength(): number {
const $$ = this;
const {width, height} = $$.state;
const cacheKey = KEY.bubbleBaseLength;
let baseLength = $$.cache.get(cacheKey);
if (!baseLength) {
$$.cache.add(cacheKey, baseLength = getMinMax("min", [width, height]));
}
return baseLength;
},
/**
* Get the radius value for bubble circle
* @param {object} d Data object
* @returns {number}
* @private
*/
getBubbleR(d): number {
const $$ = this;
let maxR = $$.config.bubble_maxR;
if (isFunction(maxR)) {
maxR = maxR.bind($$.api)(d);
} else if (!isNumber(maxR)) {
maxR = ($$.getBaseLength() / ($$.getMaxDataCount() * 2)) + 12;
}
const max = getMinMax("max", $$.getMinMaxData().max.map(d => (
$$.isBubbleZType(d) ?
$$.getBubbleZData(d.value, "y") : (
isObject(d.value) ? d.value.mid : d.value
)
)));
const maxArea = maxR * maxR * Math.PI;
const area = ($$.isBubbleZType(d) ? $$.getBubbleZData(d.value, "z") : d.value) * (maxArea / max);
return Math.sqrt(area / Math.PI);
},
/**
* Get bubble dimension data
* @param {object|Array} d data value
* @param {string} type - y or z
* @returns {number}
* @private
*/
getBubbleZData(d, type: "y" | "z"): number {
return isObject(d) ? d[type] : d[type === "y" ? 0 : 1];
}
};