Skip to content

Commit

Permalink
style: added comments to js
Browse files Browse the repository at this point in the history
  • Loading branch information
OlivierSwaak committed Jul 14, 2024
1 parent 4572d14 commit a891a51
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions docs/assets/uncertainty/uncertainty.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,36 @@ const xs = [];
const ys = [];

function addPoint(e) {
// get coordinates of mouseclick
const click_box = document.getElementById("click_box");
var rect = click_box.getBoundingClientRect();
var x = (e.clientX - rect.left - 5) / rect.width * 100;
var y = (e.clientY - rect.top - 5) / rect.width * 100;

// save coordinates
xs.push(x);
ys.push(y);

// console.log(x + ", " + y);

// create new datapoint
const datapoint = document.createElement("span");
datapoint.classList.add("datapoint");

// add datapoint to div
click_box.appendChild(datapoint);

// place datapoint at correct coordinates
datapoint.style.left = x + "%";
datapoint.style.top = y + "%";

// let datapoint be behind border to prevent clipping edge
datapoint.style.zIndex = -1;

// update mean and errorbars view box
updateCentre();
}

function updateCentre() {
// create and append mean point to view box if it does not exist yet
if (xs.length == 1) {
const datapoint = document.createElement("span");
datapoint.classList.add("datapoint");
Expand All @@ -35,13 +41,15 @@ function updateCentre() {
display_box.appendChild(datapoint);
}

// calculate mean
var x = 0;
var y = 0;
for (var i = 0; i < xs.length; i++) {
x += xs[i]/xs.length;
y += ys[i]/ys.length;
}

// calculate standard error
var x_square_sum = 0;
var y_square_sum = 0;

Expand All @@ -53,9 +61,11 @@ function updateCentre() {
var dx = Math.pow(x_square_sum, 0.5) / xs.length
var dy = Math.pow(y_square_sum, 0.5) / ys.length

// get dimensions of view box
const display_box = document.getElementById("display_box");
var rect = display_box.getBoundingClientRect();

// set errorbars
const xbar = document.getElementById("xbar");
const ybar = document.getElementById("ybar");

Expand All @@ -69,9 +79,8 @@ function updateCentre() {
ybar.y1.baseVal.value = y - dy + 5 / rect.width * 100;
ybar.y2.baseVal.value = y + dy + 5 / rect.width * 100;

// set mean point
const meanpoint = document.getElementById("mean_point");
meanpoint.style.left = x + "%";
meanpoint.style.top = y + "%";

console.log(dx, dy);
}

0 comments on commit a891a51

Please sign in to comment.