From 17109dcd84fb972398ac658d203bda199b7748d3 Mon Sep 17 00:00:00 2001 From: Denis Thiessen Date: Sat, 30 Mar 2024 22:58:50 +0100 Subject: [PATCH] Added total time metrics. --- src/components/TotalTimelineAreaChart.jsx | 37 +++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/components/TotalTimelineAreaChart.jsx b/src/components/TotalTimelineAreaChart.jsx index 8284726..a0d374a 100644 --- a/src/components/TotalTimelineAreaChart.jsx +++ b/src/components/TotalTimelineAreaChart.jsx @@ -7,20 +7,30 @@ require("highcharts/modules/accessibility")(Highcharts) require("highcharts/modules/exporting")(Highcharts) require("highcharts/modules/export-data")(Highcharts) +var timeSum = 0; +var timeAverage = 0; + export default function TotalAreaChart() { const chartComponent = useRef(null) var chartOptions = getOptions() return ( +
+
+

Total time spent: {timeSum}h

+

Average time spent per day: {timeAverage}h

+
) } function getOptions() { var projectDataMap = [] + timeSum = 0; + timeAverage = 0; var entryExtremes = getLowestAndHighestDates(fileContent) var lowestStartDate = entryExtremes.lDate var highestStartDate = entryExtremes.hDate @@ -58,6 +68,11 @@ function getOptions() { itDate = new Date(itDate.valueOf() + 3600 * 1000 * 24) } + timeSum = projectDataMap.reduce(add, 0); + timeAverage = timeSum / projectDataMap.length; + timeSum = roundTo(timeSum, 2); + timeAverage = roundTo(timeAverage, 2); + // Convert accumulated data into HighCharts Series Object const highChartsSeries = { name: "Total hours spent", @@ -101,6 +116,28 @@ function getOptions() { return highchartsOptions } +function add(accumulator, a) { + return accumulator + a; +} + +function roundTo(n, digits) { + var negative = false; + if (digits === undefined) { + digits = 0; + } + if (n < 0) { + negative = true; + n = n * -1; + } + var multiplicator = Math.pow(10, digits); + n = parseFloat((n * multiplicator).toFixed(11)); + n = (Math.round(n) / multiplicator).toFixed(digits); + if (negative) { + n = (n * -1).toFixed(digits); + } + return n; +} + function getLowestAndHighestDates(entries) { var lowestDate = new Date() var highestDate = new Date(1970, 1, 1)