-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsemantle-graph.js
93 lines (80 loc) · 2.32 KB
/
semantle-graph.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
var plot_data = new Array();
var plot_state = {
quantity: "score",
measure: "value",
};
function plot(plot_data) {
Plotly.newPlot( plot_canvas, [{
x: plot_data[0],
y: plot_data[1] }],
{ margin: { t: 0 } },
{ responsive: true },
);
drawn = true;
}
function plot_replot(plot_data) {
Plotly.react( plot_canvas, [{
x: plot_data[0],
y: plot_data[1] }],
{ margin: { t: 0 } },
{ responsive: true },
);
}
function set_plot_data(plot_state) {
if (plot_state.quantity=="score") {
plot_data[1] = window.progress[0];
} else if (plot_state.quantity=="proximity") {
plot_data[1] = window.progress[2];
}
if (plot_state.measure=="value") {
plot_data[1] = measure_to_date(plot_data[1], moving_measure_value);
} else if (plot_state.measure=="max") {
plot_data[1] = measure_to_date(plot_data[1], moving_measure_max);
} else if (plot_state.measure=="mean") {
plot_data[1] = measure_to_date(plot_data[1], moving_measure_mean);
}
return plot_data;
}
function measure_to_date(data, func) {
return Array.from(data, (_,i) => func(data.slice(0,i+1)));
}
function moving_measure_value(data) {
return data[data.length-1];
}
function moving_measure_max(data) {
var max = data.reduce(
(previousValue, currentValue) => Math.max(previousValue, currentValue), 0
);
return max;
}
function moving_measure_mean(data) {
const tot = data.reduce(
(previousValue, currentValue) => previousValue + currentValue, 0
);
return (tot / data.length);
}
window.addEventListener("load", (event) => {
plot_status = document.getElementById("semantle-graph-status")
plot_canvas = document.getElementById("semantle-plot")
form = document.querySelector("form");
button = document.querySelector("button");
if (progress == null){
plot_status.textContent = "Waiting for data...";
var plot_data = [[0],[0]];
} else {
plot_status.textContent = "Data found.";
var plot_data = [progress[3],progress[0]];
}
plot(plot_data);
button.addEventListener("click", (event) => {
var plot_form = new FormData(form);
plot_state.quantity = plot_form.get("quantity");
plot_state.measure = plot_form.get("measure");
plot_data = set_plot_data(plot_state);
console.group(`Re-plotting`);
console.log(`Plotting quantity ${plot_state.quantity}`);
console.log(`Plotting measure ${plot_state.measure}`);
console.groupEnd();
plot_replot(plot_data);
});
});