-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathheatmap.html
More file actions
148 lines (119 loc) · 3.74 KB
/
heatmap.html
File metadata and controls
148 lines (119 loc) · 3.74 KB
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
<html>
<body>
<table>
<tr>
<th> <div id="heatmap"></div> </th>
<th> <div id="scatter"></div> </th>
</tr>
</table>
</body>
</html>
<!-- load d3.js-->
<script type="text/javascript", src="./js/d3/d3.js"></script>
<script type="text/javascript">
var margin = {top:50, right:50, bottom:50, left:50}
var width = 600 - margin.left - margin.right
var height = 500 - margin.top - margin.bottom
// append svg to body
var svg = d3.select("#heatmap")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("background-color", "rgba(128, 255, 255, 0.2)")
.append("g")
.attr("transform", "translate(" + margin.left +", "+ margin.top +")")
// Labels of row and columns
var myGroups = ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J"]
var myVars = ["v1", "v2", "v3", "v4", "v5", "v6", "v7", "v8", "v9", "v10"]
//build scales and axis
var x = d3.scaleBand()
.range([0, width])
.domain(myGroups)
.padding(0.01);
svg.append("g")
.attr("transform", "translate(0, " + height +")")
.call(d3.axisBottom(x))
var y = d3.scaleBand()
.range([0, height])
.domain(myVars)
.padding(0.01);
svg.append("g")
.attr("transform", "translate(0, " + 0 +")")
.call(d3.axisLeft(y))
// colormap
//var cmap = d3.scaleLinear()
// .range(["white", "#69b3a2"])
// .domain([0, 100])
var cmap = d3.scaleSequential(d3.interpolateSpectral).domain([100, 0])
//var csvfile = "./data/heatmap_data.csv"
var csvfile = "https://raw.githubusercontent.com/holtzy/D3-graph-gallery/master/DATA/heatmap_data.csv"
var data = d3.csv(csvfile).then(
function(data){
svg.selectAll("rect")
.data(data)
.enter()
.append("rect")
.attr("x", function(d){return x(d.group)} )
.attr("y", function(d){return y(d.variable)} )
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", d=>cmap(d.value))
.on("mouseover", mouseover)
.on("mouseleave", mouseleave)
.style("stroke", "black")
.style("stroke-width", 5)
.style("stroke-opacity:1.0")
// .data(data)
// .enter()
// .append("circle")
// .attr("cx", function(d){return x(d.group)} )
//.attr("cy", function(d){return y(d.variable)} )
//.attr("r", 5)
//.style("fill", d=>cmap(d.value))
}
)
//add a tooltip
var tooltip = d3.select("body").append("div")
.style("position", "absolute")
.style("z-index", "10")
.style("visibility", "hidden")
.style("background", "white" )
.style("opacity", 0.6)
// mouseover function
var mouseover = function(d) {
//console.log( proj( [-d.fieldRa, d.fieldDec] ) )
d3.select(this)
//.style("stroke", "black")
.style("opacity", 0.5)
tooltip.style("visibility", "visible")
.text([d.group, d.variable, d.value])
.style("top", (d3.event.pageY-10)+"px")
.style("left",(d3.event.pageX+10)+"px")
.style("opacity", 1)
if ( d3.selectAll("#dot"+d.group + d.variable).empty() ) {
svg2.append("circle")
.attr("cx", x(d.group) )
.attr("cy", y(d.variable) )
.attr("r", 5)
.attr("id", "dot" + d.group + d.variable )
.style("fill", cmap(d.value))
}
else {
d3.selectAll("#dot"+d.group + d.variable).style("opacity", 1.0)
}
}
var mouseleave = function(d){
d3.select(this)
//.style("stroke", "black")
.style("opacity", 1)
d3.selectAll("#dot"+d.group + d.variable).style("opacity", 0.5)
}
// another svg
var svg2 = d3.select("#scatter")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("background-color", "rgba(255, 0, 255, 0.2)")
.append("g")
.attr("transform", "translate(" + margin.left +", "+ margin.top +")")
</script>