-
Notifications
You must be signed in to change notification settings - Fork 0
/
heatmap.html
199 lines (159 loc) · 6.52 KB
/
heatmap.html
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<!DOCTYPE html>
<html>
<style>
.plotbutton{
background-color: #4CAF50
}
</style>
<body>
<p>Enter link to a csv file (make sure same format as <a href= "https://github.com/xiaolng/widgets/blob/master/data/df_pm_v15.csv"> this example</a>) then click Plot:
</p>
<textarea id="csvlink" cols="60" >./data/df_pm_v15.csv</textarea>
<button class="plotbutton" onclick="plot()" > Plot </button>
<p></p>
<div>
<select id="barh_sortby" >
</select>
<select id="barh_order" >
<option value="ascending" selected> ascending </option>
<option value="descending" > descending </option>
</select>
</div>
<div id="barh"></div>
</body>
</html>
<!-- load d3.js-->
<script type="text/javascript", src="./js/d3/d3.js"></script>
<script type="text/javascript">
var margin = {top:70, right:70, bottom:200, left:250}
var width = 3000 - margin.left - margin.right
var height = 2500 - margin.top - margin.bottom
// append svg to body
var svg = d3.select("#barh")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.style("background-color", "rgba(128, 255, 255, 0.1)")
.append("g")
.attr("transform", "translate(" + margin.left +", "+ margin.top +")")
// read csv file, get column names
//var csvfile = "./data/df_pm_v15.csv"
//var csvfile = "./data/opsim/runs_general.csv"
function update(){
// remove previous plots
d3.select("#barh").selectAll("#xAxis").remove()
d3.select("#barh").selectAll("#yAxis").remove()
//d3.select("#barh").selectAll("text").remove()
d3.select("#barh").selectAll("rect").remove()
// get value from <select> tag
var barh_sortby = d3.select("#barh_sortby").property("value")
var barh_order = d3.select("#barh_order").property("value")
var csvfile = d3.select("#csvlink").property("value")
var metrics = [] // get column names
var opsims = [] // get row for opsim database names
d3.csv(csvfile).then( function(data) {
//console.log(data.columns)
metrics = data.columns
// add metrics to sortby select
d3.select("#barh_sortby")
.selectAll("option")
.data(metrics)
.enter()
.append("option")
.attr("value", d=>d)
.text(d=>d)
// sort data
data.sort( function(a,b){
if(barh_order=="ascending"){
return d3.ascending(a[ barh_sortby ], b[ barh_sortby ])
}
else{
return d3.descending(a[ barh_sortby ], b[ barh_sortby ])
}
})
for(var i=0; i<data.length;i++) {
// append first columns
opsims.push(data[i][metrics[0]]);
}
// Object.keys( data[0] )
width =30 * metrics.length
height = 10 * opsims.length
d3.select("#barh")
.select('svg')
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
// add axis
//build scales and axis
var x = d3.scaleBand()
.range([0, width])
.domain(metrics.slice(1))
.padding(0.01);
svg.append("g").attr("id", "xAxis")
.attr("transform", "translate(0, " + height +")")
.call(d3.axisBottom(x))
.selectAll("text")
//.attr("y", "50")
.attr("transform", "rotate(-45)")
//.style("text-anchor", "end");
var y = d3.scaleBand()
.range([0, height])
.domain(opsims)
.padding(0.01);
svg.append("g").attr("id", "yAxis")
.attr("transform", "translate(0, " + 0 +")")
.call(d3.axisLeft(y))
// draw heatmap rect
for(var i=1; i<metrics.length;i++) {
//console.log(i, metrics[i])
svg.append("g").attr("class", metrics[i]+"_rect")
.selectAll("barh"+i)
.data(data)
.enter()
.append("rect")
.attr("x", d=>x( metrics[i] ) )
.attr("y", d=>y(d[metrics[0]]) )
.attr("width", x.bandwidth())
.attr("height", y.bandwidth())
.style("fill", d=>cmap( d[metrics[i]] ))
.style("opacity", 0.8)
.on("mouseover", bar_mouseover)
.on("mouseleave", bar_mouseleave)
}
})
}
//var metrics = ['m1', 'm2', 'm3']
//var opsims = ['opsim1', 'opsim2', 'opsim3']
var cmap = d3.scaleSequential(d3.interpolateRdBu).domain([1, 0])
//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)
function bar_mouseover(d){
//console.log(this.id)
d3.select(this).style("opacity", 1)
tooltip.style("visibility", "visible")
.html([d.opsim, d.metric, d.value] + "<img src='https://www.lsst.org/sites/default/files/Screen%20Shot%202022-02-10%20at%203.18.39%20PM.png' height=50 width='auto' ></img><br>Fancy<br>")
//.text([d.opsim, d.metric, d.value])
.style("top", (d3.event.pageY-10)+"px")
.style("left",(d3.event.pageX+10)+"px")
.style("opacity", 1)
}
function bar_mouseleave(d){
d3.select(this).style("opacity", 0.8)
tooltip.style("visibility", "hidden")
}
// update when any select changes
d3.selectAll("select").on("change", function(){ update() } )
// run when first load
update()
// plot function
function plot(){
// add metrics to sortby select
d3.select("#barh_sortby")
.selectAll("option").remove()
update()
}
</script>