-
Notifications
You must be signed in to change notification settings - Fork 357
/
Copy pathgauge.ts
118 lines (97 loc) · 3.64 KB
/
gauge.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
/**
* Copyright (c) 2017 ~ present NAVER Corp.
* billboard.js project is licensed under the MIT license
*/
import {select as d3Select} from "d3-selection";
import {$ARC, $COMMON, $GAUGE} from "../../config/classes";
import {isFunction} from "../../module/util";
export default {
initGauge(): void {
const $$ = this;
const {config, $el: {arcs}} = $$;
const appendText = className => {
arcs.append("text")
.attr("class", className)
.style("text-anchor", "middle")
.style("pointer-events", "none");
};
if ($$.hasType("gauge")) {
const hasMulti = $$.hasMultiArcGauge();
arcs.append(hasMulti ? "g" : "path")
.attr("class", $ARC.chartArcsBackground)
.style("fill", (!hasMulti && config.gauge_background) || null);
config.gauge_units && appendText($GAUGE.chartArcsGaugeUnit);
if (config.gauge_label_show) {
appendText($GAUGE.chartArcsGaugeMin);
!config.gauge_fullCircle && appendText($GAUGE.chartArcsGaugeMax);
}
}
},
updateGaugeMax(): void {
const $$ = this;
const {config, state} = $$;
const hasMultiGauge = $$.hasMultiArcGauge();
// to prevent excluding total data sum during the init(when data.hide option is used), use $$.rendered state value
const max = hasMultiGauge ?
$$.getMinMaxData().max[0].value : $$.getTotalDataSum(state.rendered);
// if gauge_max less than max, make max to max value
if (max + config.gauge_min * (config.gauge_min > 0 ? -1 : 1) > config.gauge_max) {
config.gauge_max = max - config.gauge_min;
}
},
redrawMultiArcGauge(): void {
const $$ = this;
const {config, state, $el} = $$;
const {hiddenTargetIds} = $$.state;
const arcLabelLines = $el.main.selectAll(`.${$ARC.arcs}`)
.selectAll(`.${$ARC.arcLabelLine}`)
.data($$.arcData.bind($$));
const mainArcLabelLine = arcLabelLines.enter()
.append("rect")
.attr("class", d => `${$ARC.arcLabelLine} ${$COMMON.target} ${$COMMON.target}-${d.data.id}`)
.merge(arcLabelLines);
mainArcLabelLine
.style("fill", d => ($$.levelColor ? $$.levelColor(d.data.values[0].value) : $$.color(d.data)))
.style("display", config.gauge_label_show ? null : "none")
.each(function(d) {
let lineLength = 0;
const lineThickness = 2;
let x = 0;
let y = 0;
let transform = "";
if (hiddenTargetIds.indexOf(d.data.id) < 0) {
const updated = $$.updateAngle(d);
const innerLineLength = state.gaugeArcWidth / $$.filterTargetsToShow($$.data.targets).length *
(updated.index + 1);
const lineAngle = updated.endAngle - Math.PI / 2;
const arcInnerRadius = state.radius - innerLineLength;
const linePositioningAngle = lineAngle - (arcInnerRadius === 0 ? 0 : (1 / arcInnerRadius));
lineLength = state.radiusExpanded - state.radius + innerLineLength;
x = Math.cos(linePositioningAngle) * arcInnerRadius;
y = Math.sin(linePositioningAngle) * arcInnerRadius;
transform = `rotate(${lineAngle * 180 / Math.PI}, ${x}, ${y})`;
}
d3Select(this)
.attr("x", x)
.attr("y", y)
.attr("width", lineLength)
.attr("height", lineThickness)
.attr("transform", transform)
.style("stroke-dasharray", `0, ${lineLength + lineThickness}, 0`);
});
},
textForGaugeMinMax(value: number, isMax?: boolean): number | string {
const $$ = this;
const {config} = $$;
const format = config.gauge_label_extents;
return isFunction(format) ? format.bind($$.api)(value, isMax) : value;
},
getGaugeLabelHeight(): 20 | 0 {
const {config} = this;
return this.config.gauge_label_show && !config.gauge_fullCircle ? 20 : 0;
},
getPaddingBottomForGauge() {
const $$ = this;
return $$.getGaugeLabelHeight() * ($$.config.gauge_label_show ? 2 : 2.5);
}
};