-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
149 lines (126 loc) · 4.03 KB
/
index.js
File metadata and controls
149 lines (126 loc) · 4.03 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
149
// set the dimensions and margins of the graph
var margin = {top: 30, right: 100, bottom: 30, left: 50},
width = 1000 - margin.left - margin.right,
height = 400 - margin.top - margin.bottom;
// append the svg object to the body of the page
var svg = d3.select("#svg-container")
.append("svg")
.attr("width", width + margin.left + margin.right)
.attr("height", height + margin.top + margin.bottom)
.append("g")
.attr("transform", "translate(" + margin.left + "," + margin.top + ")");
// Labels of row and columns
let start_time = 0
let end_time = 350
let resolution = 10
var myGroups = d3.range(start_time, end_time, resolution);
var myVars = ["au01r", "au01c", "au04r", "au04c", "au09r", "au09c", "au10r", "au10c", "au12r", "au12c", "au14r", "au14c"]
// Build X scales and axis:
const x = d3.scaleBand()
.range([ 0, width ])
.domain(myGroups)
.padding(0.01);
const xAxis = g => g
.attr("transform", "translate(0," + height + ")")
.call(d3.axisBottom(x));
svg.append("g")
.attr("class", "x-axis")
.call(xAxis);
// Build Y scales and axis:
const y = d3.scaleBand()
.range([ height, 0 ])
.domain(myVars)
.padding(0.01);
const yAxis = g => g
.call(d3.axisLeft(y));
svg.append("g")
.attr("class", "y-axis")
.call(yAxis);
// Group for main content
const main = svg.append("g")
.attr("class", "main");
// Build color scale
const myColor = d3.scaleSequential()
.domain([0,4])
.interpolator(d3.interpolateInferno);
// Converts wide format data to long format
const longify = (rows) => {
const extracted = []
rows.forEach((row) => {
myVars.forEach((varr) => {
extracted.push({ frame: row.min_timestamp, variable: varr, value: row[varr] });
});
});
return extracted;
}
const fetchData = () => {
const body = JSON.stringify({
query: `query MyQuery2 {
testaggau(args: {start_time: ${start_time}, end_time: ${end_time}, resolution: ${resolution}}) {
${myVars.join(', ')}
grouped_seconds
min_timestamp
}
}`});
return fetch('http://localhost:8080/v1/graphql', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body,
})
.then(res => res.json())
.then(res => {
const data = longify(res.data.testaggau);
const cells = main.selectAll(".cell")
.data(data, d => "" + d.frame + ':' + d.variable)
cells.exit().remove();
cells.enter().append("rect")
.attr("class", "cell")
.merge(cells)
.attr("x", d => x(d.frame))
.attr("y", d => y(d.variable))
.attr("width", x.bandwidth() )
.attr("height", y.bandwidth() )
.style("fill", d => myColor(d.value))
//.on("mouseover", handleMouseOver)
//.on("mouseout", handleMouseOut);
svg.selectAll(".x-axis").call(xAxis);
svg.selectAll(".y-axis").call(yAxis);
});
};
fetchData();
function zoomed() {
x.range([margin.left, width - margin.right]
.map(d => d3.event.transform.applyX(d)));
svg.selectAll(".cell")
.attr("x", d => x(d.frame))
.attr("width", x.bandwidth());
svg.selectAll(".x-axis").call(xAxis);
let k = d3.event.transform.k;
let new_resolution = Math.floor(10 / k);
if (new_resolution !== resolution) {
resolution = Math.max(new_resolution, 1);
console.log(resolution);
// Update groups used for x-axis
myGroups = d3.range(start_time, end_time, resolution);
x.domain(myGroups);
// Update cells
fetchData();
}
}
var zoom = d3.zoom().on("zoom", zoomed);
svg.call(zoom);
function handleMouseOver(d, i) {
// Specify where to put label of text
let node = svg.append("text").attrs({
id: "popup", // Create an id for text so we can select it later for removing on mouseout
x: d3.event.pageX, // - document.getElementById('svg-container').getBoundingClientRect().x,
y: d3.event.pageY, // - document.getElementById('svg-container').getBoundingClientRect().y
});
//console.log(node);
node.text(function() {
return [d.value]; // Value of the text
});
}
function handleMouseOut(d, i) {
d3.selectAll("#popup").remove();
}