-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex2.html
164 lines (150 loc) · 6.29 KB
/
index2.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
<!-- Fron Example of https://d3-graph-gallery.com/graph/heatmap_style.html -->
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ethereum Gas Price</title>
<!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-ZGB8HGKRF0"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'G-ZGB8HGKRF0');
</script>
</head>
<body>
<!-- Load d3.js -->
<script src="https://d3js.org/d3.v6.js"></script>
<h1>Min Ethereum Gas Price by Hour<a href="https://gas.d3serve.net">gas.d3serve.net</a></h1>
<h2><a href="https://dune.com/queries/2501963">Dune</a></h2>
<!-- Create a div where the graph will take place -->
<div id="my_dataviz"></div>
<script>
const resultKey = "Min Gas";
const getValue = (d) => {
return d[resultKey];
}
// set the dimensions and margins of the graph - margin.top - margin.bottom
var margin = {top: 80, right: 25, bottom: 30, left: 100},
width = 1450 - margin.left - margin.right;
d3.json("https://api.dune.com/api/v1/query/2501963/results?api_key=CgkyxYUuM9OfKx0G7Igf0BPm25hJFnWO").then(function(jsonRet) {
// console.log(jsonRet.result);
let data = jsonRet.result.rows ;
data = [...data, ...data, ...data]
// Labels of row and columns -> unique identifier of the column called 'group' and 'variable'
const days = Array.from(new Set(data.map(d => d.Day))).reverse();
const hours = Array.from(new Set(data.map(d => d.Hour))).sort((a, b) => { return a - b;});
// height calculation
const height =( Math.floor(data.length / 22) + 1) * 10
// append the svg object to the body of the page
const svg = d3.select("#my_dataviz")
.append("svg")
.attr("height", height + margin.top + margin.bottom )
.attr("width", width + margin.left + margin.right)
.append("g")
.attr("transform", `translate(${margin.left}, ${margin.top})`);
const myColor = d3.scaleLinear()
.range(["green", "white" ,"red"]) // Change the range to white to green
.domain([d3.min(data, d => getValue(d)), d3.median(data, d => getValue(d)), d3.max(data, d => getValue(d))])
// Build X scales and axis:
const x = d3.scaleBand()
.range([ 0, width ])
.domain(hours)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.attr("transform", `translate(0, ${height+10})`)
.call(d3.axisBottom(x).tickSize(0))
.select(".domain").remove()
// Build Y scales and axis:
const y = d3.scaleBand()
.range([ height, 0 ])
.domain(days)
.padding(0.05);
svg.append("g")
.style("font-size", 15)
.call(d3.axisLeft(y).tickSize(0))
.select(".domain").remove()
// create a tooltip
const tooltip = d3.select("#my_dataviz")
.append("div")
.style("opacity", 0)
.attr("class", "tooltip")
.style("background-color", "white")
.style("border", "solid")
.style("border-width", "2px")
.style("border-radius", "5px")
.style("padding", "5px")
// Three function that change the tooltip when user hover / move / leave a cell
const mouseover = function(event,d) {
tooltip
.style("opacity", 1)
d3.select(this)
.style("stroke", "black")
.style("opacity", 1)
}
const mousemove = function(event,d) {
tooltip
.html("The exact value of<br>this cell is: " + getValue(d))
.style("left", (event.x)/2 + "px")
.style("top", (event.y)/2 + "px")
}
const mouseleave = function(event,d) {
tooltip
.style("opacity", 0)
d3.select(this)
.style("stroke", "none")
.style("opacity", 0.8)
}
// add the squares
svg.selectAll()
.data(data, function(d) {return d.Day+':'+d.Hour;})
.join("g")
.attr("transform", function(d) { return "translate(" + x(d.Hour) + "," + y(d.Day) + ")"; })
.append("rect")
.attr("rx", 4)
.attr("ry", 4)
.attr("width", x.bandwidth())
.attr("height", y.bandwidth())
.style("fill", function(d) { return myColor(getValue(d)); })
.style("stroke-width", 4)
.style("stroke", "none")
.style("opacity", 0.8)
.on("mouseover", mouseover)
.on("mousemove", mousemove)
.on("mouseleave", mouseleave)
.select(function() { return this.parentNode; })
.append("text")
.attr("x", x.bandwidth() / 2)
.attr("y", y.bandwidth() / 2)
.attr("dy", ".35em")
// font size
.style("font-size", "0.8em")
// bold
.style("font-weight", "bold")
.text(d=>getValue(d).toFixed(2))
.style("text-anchor", "middle")
.style("fill", "black")
// Add title to graph
svg.append("text")
.attr("x", 0)
.attr("y", -50)
.attr("text-anchor", "left")
.style("font-size", "22px")
.text("Ethereum Gas Price (gas.zzn.im)");
// Add subtitle to graph
svg.append("text")
.attr("x", 0)
.attr("y", -20)
.attr("text-anchor", "left")
.style("font-size", "14px")
.style("fill", "grey")
.style("max-width", 400)
.text(resultKey + " price per by day and hour (in UTC timezone)");
});
</script>
</body>
</html>