From 3cdad43b416e95e706c5cd9ba8c4a9f581652021 Mon Sep 17 00:00:00 2001 From: Klemek Date: Fri, 14 Jan 2022 20:05:29 +0100 Subject: [PATCH] bugfix and periods percents --- main.js | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/main.js b/main.js index b4bbaae..2b614ef 100644 --- a/main.js +++ b/main.js @@ -129,7 +129,7 @@ const deserialize = function(rawData) { text: subData2[3], }; }), - newPeriod: NEW_PERIOD, + newPeriod: JSON.parse(JSON.stringify(NEW_PERIOD)), }; }), }; @@ -204,7 +204,7 @@ let app = { let text = `${date.getFullYear()} (Week ${date.getWeek()})
Age: ${row}`; const views = this.getViews(row, col); for (const view in views) { - text += `
${view}: ${views[view].text}`; //TODO percent + text += `
${view}: ${views[view].text} (${views[view].percent})`; } this.getEvents(row, col).forEach(event => { text += `
- ${new Date(event.date).formatSimple()}: ${event.text}`; @@ -252,7 +252,7 @@ let app = { this.views.push({ name: 'New View', periods: [], - newPeriod: NEW_PERIOD, + newPeriod: JSON.parse(JSON.stringify(NEW_PERIOD)), }); }, deletePeriod(viewIndex, periodIndex) { @@ -271,6 +271,19 @@ let app = { this.views = []; this.$force; }, + updateStatistics() { + const total = (new Date() - this.birthdate); + this.views.forEach(view => { + console.log(view); + const data = {}; + view.periods.forEach(period => { + data[period.text + period.color] = (data[period.text + period.color] ?? 0) + ((period.endDate ? new Date(period.endDate) : new Date()) - (new Date(period.startDate))); + }); + view.periods.forEach(period => { + period.percent = `${(100 * data[period.text + period.color] / total).toFixed(3)}%`; + }); + }); + }, }, beforeMount() { this.life = Array(90).fill(Array(52)); @@ -288,10 +301,14 @@ let app = { this.events = data.events; this.views = data.views; } + this.updateStatistics(); }, mounted() { setTimeout(this.showApp); }, + beforeUpdate() { + this.updateStatistics(); + }, updated() { const data = serialize(this.birtdate, this.view, this.events, this.views); const url = new URL(window.location);