-
Notifications
You must be signed in to change notification settings - Fork 0
/
barleaves_dist.js
127 lines (112 loc) · 2.49 KB
/
barleaves_dist.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
function BarLeavesDist({
id = null,
width = 300,
height = null,
target = null,
color = '#0080FF',
color2 = '#99CCFF',
start = 'top',
padding = 10,
margin_l = 0,
barh = 20
} = {}){
let wscale = d3.scaleLinear()
.domain([0, 100])
.range([0, width-(padding+margin_l)])
let $target = $(target),
ticks = $target.find('.dist-grid'),
leaves = $target.find('.leaf'),
y_ranges = [],
data = {},
bars = []
let valscale = d3.scaleLinear()
.domain([0, leaves.length])
.range([0, 100])
ticks.each((i, tick) => {
let y = tick.getAttribute('y1')
y_ranges.push(parseInt(y))
})
if (start == 'top') {
y_ranges.sort(function(a, b){return b - a})
leaves.each((i, leaf) => {
let y = leaf.getAttribute('cy')
let tick = 0
for (j = 0; j < y_ranges.length; j++) {
if (y > y_ranges[j]) {
tick = y_ranges[j]
break
}
}
if (! (tick in data)) {
data[tick] = 0
}
data[tick] += 1
})
} else {
y_ranges.sort(function(a, b){return a - b})
leaves.each((i, leaf) => {
let y = leaf.getAttribute('cy')
let tick = 0
for (j = 0; j < y_ranges.length; j++) {
if (y < y_ranges[j]) {
tick = y_ranges[j]
break
}
}
if (! (tick in data)) {
data[tick] = 0
}
data[tick] += 1
})
}
let spacing = 0
if (start == 'top') {
spacing = (y_ranges[1] - y_ranges[0])/2
} else {
spacing = (y_ranges[1] - y_ranges[0])/2
}
Object.keys(data).forEach((k) => {
bars.push({
y : k - spacing,
perc : valscale(data[k])
})
})
const svg = d3.create('svg')
.attr('width', width)
.attr('height', height)
.attr('style', `max-width: 100%;
width: auto;
width: intrinsic;`
)
svg.append('g')
.selectAll('rect')
.data(bars)
.join('rect')
.attr('x', margin_l)
.attr('y', d => start == 'top' ? d.y - (barh/2) : d.y - (barh/2))
.attr('width', d => wscale(d.perc))
.attr('height', barh)
.attr('fill', color)
if (start == 'top') {
svg.append('g')
.selectAll('text')
.data(bars)
.join('text')
.text(d => d.perc.toFixed(2) + '%')
.attr('x', d => (wscale(d.perc) + 8))
.attr('y', d => d.y - (barh/2) + 13)
.attr('fill', '#FFF')
.attr('font-size', 13)
} else {
svg.append('g')
.selectAll('text')
.data(bars)
.join('text')
.text(d => d.perc.toFixed(2) + '%')
.attr('x', d => (wscale(d.perc) + 8))
.attr('y', d => d.y - (barh/2) + 13)
.attr('fill', '#FFF')
.attr('font-size', 13)
}
return svg.node()
}