-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbarleaves.js
88 lines (75 loc) · 1.65 KB
/
barleaves.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
function BarLeaves({
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),
leaves = $target.find('.leaf'),
data = {},
bars = []
//height = 320
let valscale = d3.scaleLinear()
.domain([0, leaves.length])
.range([0, 100])
leaves.each((i, leaf) => {
let y = leaf.getAttribute('cy')
if (! (y in data)) {
data[y] = 0
}
data[y] += 1
})
Object.keys(data).forEach((k) => {
bars.push({
y : parseFloat(k),
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 => 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) + 16)
.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) + 16)
.attr('fill', '#FFF')
.attr('font-size', 13)
}
return svg.node()
}