-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlegend.js
93 lines (83 loc) · 3.47 KB
/
legend.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
// d3.legend.js
// (C) 2012 [email protected]
// MIT licence
(function() {
d3.legend = function(g) {
g.each(function() {
var g= d3.select(this),
items = {},
svg = d3.select(g.property("nearestViewportElement")),
legendPadding = g.attr("data-style-padding") || 5,
lb = g.selectAll(".legend-box").data([true]),
li = g.selectAll(".legend-items").data([true])
lb.enter().append("rect").classed("legend-box",true)
li.enter().append("g").classed("legend-items",true)
svg.selectAll("[data-legend]").each(function() {
var self = d3.select(this)
items[self.attr("data-legend")] = {
pos : self.attr("data-legend-pos") || this.getBBox().y,
active: self.attr("active") == "1" ,
color : self.attr("data-legend-color") != undefined ? self.attr("data-legend-color") : self.style("fill") != 'none' ? self.style("fill") : self.style("stroke")
}
})
items = d3.entries(items) //.sort(function(a,b) { return a.value.pos-b.value.pos})
li.selectAll("text.name")
.data(items,function(d) { return d.key})
.call(function(d) { d.enter().append("text")})
.call(function(d) { d.exit().remove()})
.attr("y",function(d,i) { return (i*1.5)+"em"})
.attr("x","3.7em")
.attr("class","name")
.text(function(d) { return d.key})
.style("fill", function(d) { return d.value.active ? "#000" : "CCC"})
.style("cursor", "pointer")
.on("click", function(d){
// Determine if current line is visible
var active = d.value.active ? false : true,
newOpacity = active ? 0 : 1;
d3.select(this).style("fill", !active ? "#000" : "CCC")
// Hide or show the elements based on the ID
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition().duration(100)
.style("opacity", newOpacity);
// Update whether or not the elements are active
d.value.active = active;
})
.on('mouseover', function(d) {
console.log("#tag"+d.key.replace(/\s+/g, ''))
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition()
.duration(100)
.style('stroke-width', 3)})
.on('mouseout', function(d) {
d3.select("#tag"+d.key.replace(/\s+/g, ''))
.transition()
.duration(100)
.style('stroke-width', 1);
});
li.selectAll("text.tooltip")
.data(items,function(d) { return d.key})
.call(function(d) { d.enter().append("text")})
.call(function(d) { d.exit().remove()})
.attr("class", "tooltip")
.attr("y",function(d,i) { return (i*1.5)+"em"})
.attr("x","1em")
.style("fill",function(d) { return d.value.color})
li.selectAll("circle")
.data(items,function(d) { return d.key})
.call(function(d) { d.enter().append("circle")})
.call(function(d) { d.exit().remove()})
.attr("cy",function(d,i) { return (i*1.5)-0.25+"em"})
.attr("cx",0)
.attr("r","0.4em")
.style("fill",function(d) { return d.value.color})
// Reposition and resize the box
var lbbox = li[0][0].getBBox()
lb.attr("x",(lbbox.x-legendPadding))
.attr("y",(lbbox.y-legendPadding))
.attr("height",(lbbox.height+2*legendPadding))
.attr("width",(lbbox.width+2*legendPadding))
})
return g
}
})()