diff --git a/pages/components/charts.html b/pages/components/charts.html index be30103..5e4a5aa 100644 --- a/pages/components/charts.html +++ b/pages/components/charts.html @@ -14,14 +14,18 @@
Warum sehen die Diagramme bei einem Zeitraum größer als 10 Tage anders aus? Die Daten werden, besonders bei großen Datenmengen (über 10 Tage), dadurch reduziert, dass ein Durchschnittswert für jeden Tag gebildet wird. Da es insgesamt 48 gemessene Datensätze pro Tag gibt, wäre das Diagramm ansonsten schwer bis kaum ablesbar. So sind auch bei großen Zeiträumen langfristige Tendenzen ablesbar. Die unreduzierten Daten sind per API abrufbar. diff --git a/pages/index.html b/pages/index.html index 72d7ec4..6452bfe 100644 --- a/pages/index.html +++ b/pages/index.html @@ -32,6 +32,9 @@ edit_calendar + + refresh + {% with weather=weather_data %} {% include './components/weather.html' %} diff --git a/public/css/app.css b/public/css/app.css index 4e3894b..10483fb 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -139,4 +139,10 @@ footer { .display-info { text-align: center; margin-bottom: 0%; +} + +#reload-button { + position: fixed; + bottom: 100px; + right: 23px; } \ No newline at end of file diff --git a/public/js/app.js b/public/js/app.js index cee8fc6..6eb95bc 100644 --- a/public/js/app.js +++ b/public/js/app.js @@ -46,7 +46,7 @@ document.addEventListener('DOMContentLoaded', async () => { errorHandler('current-data', 204); } - await updateCurrentData(data); + //await updateCurrentData(data); // Handled by applyDateRange() // Set up background task for keeping the 'measured' date up-to-date setInterval(() => { @@ -144,7 +144,8 @@ function applyDateRange() { reject(); return; } - + + await updateCurrentData(dataObject); await drawCharts(data); resolve(); }); @@ -207,19 +208,23 @@ async function updateCurrentData(data) { * @returns {string} HTML containing the weight delta (in a fitting color) */ function getWeightDelta(data) { - // Get weight from most recent record - var weightCurrent = data[Object.keys(data).length - 1].weight; - // Get weight from the record in the middle of the array (measured approximately 24 hours ago) - // This is done in case there is no record from *exactly* 24 hours ago - var weightStartIndex = Math.floor(((Object.keys(data).length - 1) / 2)) - var weightStart = data[weightStartIndex].weight; - // Calculate the delta of the current and start weight - var weightDelta = weightCurrent - weightStart; - // Limit float to 2 decimal places - weightDelta = Number(weightDelta.toFixed(2)); - // Format HTML string with green color for weight growth and red color for weight decline - var weightDeltaString = weightDelta >= 0 ? `
+${weightDelta}
` : `${weightDelta}
`; - return weightDeltaString; + let timespan = document.getElementById("delta-span-input").value * 60000; // in ms + if (timespan === undefined || timespan == null || timespan === 0) { + timespan = 86400000; // 24h + } + let i = Object.keys(data).length - 1; + let newer_measured = new Date(data[i].measured); + let newer_weight = data[i].weight; + + while (newer_measured.getTime() - Date.parse(data[i].measured) < timespan && i > 0) { + i--; + } + let older_weight = data[i].weight; + + let weightDelta = newer_weight - older_weight; + weightDelta = weightDelta.toFixed(2); + + return weightDelta >= 0 ? `+${weightDelta}
` : `${weightDelta}
`; } /** diff --git a/public/js/charts.js b/public/js/charts.js index 5fc9e44..6b8f43f 100644 --- a/public/js/charts.js +++ b/public/js/charts.js @@ -30,6 +30,17 @@ async function drawCharts(data) { data = data['data']; + // Try to get the time interval between records to use for delta graph. + let deltaGraphInterval = document.getElementById("delta-span-input").value; + + // If none or invalid interval is specified, use 24 hours by default (in ms). + if (typeof deltaGraphInterval !== 'string' || deltaGraphInterval === '' || deltaGraphInterval === '0' ) { + deltaGraphInterval = 86400000; + } else { + // Turn input string into number and convert to milliseconds. + deltaGraphInterval = parseInt(deltaGraphInterval) * 60000; + } + if (data == undefined || data == [] || Object.keys(data) == 0) { errorHandler('charts', 204); return; @@ -46,6 +57,7 @@ async function drawCharts(data) { await drawCompareChart(data, false); await drawTempChart(data); await drawWeightChart(data); + await drawDeltaChart(data, deltaGraphInterval); await drawHumidityChart(data); document.getElementById('beelogger-charts-loader').classList.add('hide'); @@ -139,7 +151,7 @@ async function drawTempChart(data) { } /** - * Generates and renders the weight chart on the page. + * Generates and renders the weight delta chart on the page. * * @param {Array} data Array containing the data records that the graph should be generated from */ @@ -171,6 +183,60 @@ async function drawWeightChart(data) { }); } +/** + * Generates and renders the weight delta chart on the page. + * + * @param {Array} data Array containing the data records that the graph should be generated from + * @param {number} interval The amount of time between records used for delta calculation (in between will be ignored) + */ +async function drawDeltaChart(data, interval) { + // Create array to visualize as a chart. + let deltaData = [['Gemessen', 'Delta (g)']]; + + // Start with newest record. + let recordIndex = Object.keys(data).length - 1; + + // Go through each record. + while (recordIndex !== 0) { + let newerRecord = data[recordIndex]; + let newerRecordTimestamp = new Date(newerRecord.measured); + let newerWeight = newerRecord.weight; + + // Go further in the dataset until the set interval is reached again. + while (newerRecordTimestamp.getTime() - Date.parse(data[recordIndex].measured) < interval && recordIndex > 0) { + recordIndex--; + } + + // The weight of the record one interval further. + let olderWeight = data[recordIndex].weight; + + // Calculate the difference between the two weights. + let weightDelta = newerWeight - olderWeight; + + // Push timestamp and weight data to visualization data array. + deltaData.push([newerRecordTimestamp, weightDelta]); + } + + let weightDataTable = google.visualization.arrayToDataTable(deltaData); + let weightChart = new google.visualization.LineChart(document.getElementById('delta_chart')); + + weightChart.draw(weightDataTable, { + height: '100%', + lineWidth: 2, + colors: ['black'], + chartArea: { + left: '10%', + top: '10%', + right: '10%', + width: '100%', + height: '70%' + }, + legend: { + position: 'bottom' + }, + }); +} + /** * Generates and renders the humidity chart on the page. *