mirror of
https://github.com/elisspace/FxLifeSheet.git
synced 2026-08-29 15:44:00 +00:00
285 lines
9.2 KiB
HTML
285 lines
9.2 KiB
HTML
<div id="table-container-averages">
|
|
</div>
|
|
|
|
<script type="text/javascript">
|
|
function getRequest(url, callback) {
|
|
var xhr = new XMLHttpRequest();
|
|
xhr.onreadystatechange = function() {
|
|
if (xhr.readyState == 4 && xhr.status == 200) {
|
|
callback(xhr.responseText);
|
|
}
|
|
}
|
|
xhr.open("GET", url, true);
|
|
xhr.send();
|
|
}
|
|
|
|
function updateData() {
|
|
getRequest("https://where-is-felix-today-backend.herokuapp.com/api.json", function(data) {
|
|
const overviewTable = JSON.parse(data)["otherFxLifeData"]["overviewTable"]["value"];
|
|
const keys = [{
|
|
"name": "mood",
|
|
"customTitle": "mood",
|
|
"unit": "",
|
|
"rounding": 2,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "weight",
|
|
"customTitle": "Weight",
|
|
"unit": "lbs",
|
|
"rounding": 2,
|
|
"good": "down",
|
|
"percentage": false
|
|
}, {
|
|
"name": "gym",
|
|
"customTitle": "Gym visits",
|
|
"unit": "",
|
|
"rounding": 0,
|
|
"good": "up",
|
|
"percentage": true
|
|
}, {
|
|
"name": "minutesread",
|
|
"customTitle": "Books Read",
|
|
"unit": "minutes",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "sleepdurationwithings",
|
|
"customTitle": "Sleep Duration",
|
|
"unit": "hours",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "withingssleepinghr",
|
|
"customTitle": "Sleeping HR",
|
|
"unit": "beats/min",
|
|
"rounding": 1,
|
|
"good": "bad",
|
|
"percentage": false
|
|
}, {
|
|
"name": "alcoholintake",
|
|
"customTitle": "Alcohol Intake",
|
|
"unit": "drinks/day",
|
|
"rounding": 1,
|
|
"good": "bad",
|
|
"percentage": false
|
|
}, {
|
|
"name": "veggies",
|
|
"customTitle": "Ate Vegetables",
|
|
"unit": "",
|
|
"rounding": 0,
|
|
"good": "up",
|
|
"percentage": true
|
|
}, {
|
|
"name": "meditated",
|
|
"customTitle": "Meditated",
|
|
"unit": "minutes",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "healthy",
|
|
"customTitle": "Healthy",
|
|
"unit": "",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "headache",
|
|
"customTitle": "Headache",
|
|
"unit": "",
|
|
"rounding": 1,
|
|
"good": "bad",
|
|
"percentage": false
|
|
}, {
|
|
"name": "hoursdriving",
|
|
"customTitle": "Hours Driven",
|
|
"unit": "hours",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}, {
|
|
"name": "macroadherence",
|
|
"customTitle": "Macro Counting Adherence",
|
|
"unit": "",
|
|
"rounding": 1,
|
|
"good": "up",
|
|
"percentage": false
|
|
}];
|
|
// Key have to be lower case
|
|
const rows = ["week", "month", "quarter", "year", "alltime"];
|
|
for (var i = 0; i < keys.length; i++) {
|
|
const keyData = keys[i];
|
|
const key = keyData["name"]
|
|
const value = overviewTable[key];
|
|
const container = document.getElementById("table-container-averages");
|
|
const table = document.createElement("table");
|
|
table.setAttribute("cellspacing", 0)
|
|
table.setAttribute("cellpadding", 0)
|
|
table.setAttribute("id", key);
|
|
table.setAttribute("class", "metrics-table");
|
|
const thead = document.createElement("thead");
|
|
const tr = document.createElement("tr");
|
|
const th = document.createElement("th");
|
|
th.innerHTML = keyData["customTitle"];
|
|
th.colSpan = "2"
|
|
tr.appendChild(th);
|
|
thead.appendChild(tr);
|
|
table.appendChild(thead);
|
|
const tbody = document.createElement("tbody");
|
|
|
|
// Calculate the minimum and maximum value of each metric
|
|
let minValue = null;
|
|
let maxValue = null;
|
|
for (var j = 0; j < rows.length; j++) {
|
|
let calculatedValue = overviewTable[key + rows[j]]
|
|
if (calculatedValue != null) {
|
|
if (minValue == null || calculatedValue < minValue) {
|
|
minValue = calculatedValue;
|
|
}
|
|
if (maxValue == null || calculatedValue > maxValue) {
|
|
maxValue = calculatedValue;
|
|
}
|
|
}
|
|
}
|
|
|
|
for (var j = 0; j < rows.length; j++) {
|
|
const tr = document.createElement("tr");
|
|
const td = document.createElement("td");
|
|
td.innerHTML = rows[j].charAt(0).toUpperCase() + rows[j].slice(1); // capitalize
|
|
tr.appendChild(td);
|
|
const td2 = document.createElement("td");
|
|
let calculatedValue = overviewTable[key + rows[j]]
|
|
if (calculatedValue == maxValue) {
|
|
if (keyData["good"] == "up") {
|
|
td2.setAttribute("class", "best-value");
|
|
} else {
|
|
td2.setAttribute("class", "worst-value");
|
|
}
|
|
} else if (calculatedValue == minValue) {
|
|
if (keyData["good"] == "up") {
|
|
td2.setAttribute("class", "worst-value");
|
|
} else {
|
|
td2.setAttribute("class", "best-value");
|
|
}
|
|
}
|
|
let suffix = "";
|
|
if (keyData["percentage"]) {
|
|
calculatedValue *= 100
|
|
suffix = "% of days"
|
|
}
|
|
calculatedValue = roundNumberExactly(calculatedValue, keyData["rounding"]);
|
|
|
|
if (keyData["unit"]) {
|
|
suffix = " " + keyData["unit"];
|
|
}
|
|
td2.innerHTML = calculatedValue + suffix;
|
|
td.innerHTML += " (" + overviewTable[key + rows[j] + "count"] + ")";
|
|
tr.appendChild(td2);
|
|
tbody.appendChild(tr);
|
|
}
|
|
table.appendChild(tbody);
|
|
container.appendChild(table);
|
|
}
|
|
})
|
|
}
|
|
updateData();
|
|
|
|
// Round the given number, exact number of decimal places
|
|
function roundNumberExactly(number, decimals) {
|
|
return (Math.round(number * Math.pow(10, decimals)) / Math.pow(10, decimals)).toFixed(decimals);
|
|
}
|
|
</script>
|
|
|
|
<style type="text/css">
|
|
* {
|
|
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
|
|
color: #353535;
|
|
}
|
|
|
|
.metrics-table tr:nth-child(odd) {
|
|
background-color: #f7f7f7;
|
|
}
|
|
|
|
#table-container-averages thead th {
|
|
font-weight: bold;
|
|
text-align: center;
|
|
}
|
|
|
|
.metrics-table td {
|
|
border: 1px solid #e8e8e8;
|
|
}
|
|
|
|
.metrics-table th {
|
|
border: 1px solid #e8e8e8;
|
|
}
|
|
|
|
.metrics-table th,
|
|
.metrics-table td {
|
|
padding: 10px 15px;
|
|
}
|
|
|
|
.metrics-table>*>tr>td:first-child {
|
|
text-align: right;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.metrics-table tr {
|
|
line-height: 16px;
|
|
font-size: 88%;
|
|
}
|
|
|
|
#table-container-averages {
|
|
margin-top: -20px;
|
|
margin-left: auto;
|
|
margin-right: auto;
|
|
text-align: center;
|
|
}
|
|
|
|
.metrics-table {
|
|
border: none;
|
|
width: auto;
|
|
margin: 5px;
|
|
margin-right: 15px;
|
|
margin-left: 15px;
|
|
display: inline-block;
|
|
}
|
|
|
|
.metrics-table>*>tr>td:first-child {
|
|
text-align: right;
|
|
white-space: nowrap;
|
|
width: 50px;
|
|
}
|
|
|
|
.metrics-table tr {
|
|
line-height: 16px;
|
|
font-size: 88%;
|
|
width: 100%;
|
|
}
|
|
|
|
.metrics-table tr .ago-subtle {
|
|
font-size: 85%;
|
|
}
|
|
|
|
.metrics-table tr .highlighted {
|
|
padding: 4px;
|
|
}
|
|
|
|
.best-value {
|
|
color: green;
|
|
border-right: 1px solid green !important;
|
|
}
|
|
|
|
.worst-value {
|
|
color: red;
|
|
border-right: 1px solid red !important;
|
|
}
|
|
|
|
@media screen and (max-width: 800px) {
|
|
.metrics-table tr {
|
|
line-height: 12px;
|
|
}
|
|
}
|
|
</style> |