From fe69fdccd82109a91a80847ebd0356377b3d2181 Mon Sep 17 00:00:00 2001 From: Art Furrer <2823246+bonham@users.noreply.github.com> Date: Sun, 17 May 2026 19:55:25 +0200 Subject: [PATCH 1/6] refactoring 1 --- client/src/lib/progress/progressChart.ts | 80 ++++++++++++++++++++++++ client/src/views/ProgressChart.vue | 75 +--------------------- 2 files changed, 81 insertions(+), 74 deletions(-) create mode 100644 client/src/lib/progress/progressChart.ts diff --git a/client/src/lib/progress/progressChart.ts b/client/src/lib/progress/progressChart.ts new file mode 100644 index 0000000..83c53f4 --- /dev/null +++ b/client/src/lib/progress/progressChart.ts @@ -0,0 +1,80 @@ +import { DateTime } from 'luxon'; +import _ from 'lodash' + +import { Track } from '@/lib/Track' + +type TracksByYearDict = Record; + +interface ChartDataPoint { + x: DateTime, + y: number, + name: string, + step: number, + originalDate: DateTime +} + +interface DSet { + label: string, + data: ChartDataPoint[], + pointRadius: number +} + +/** + * Converts yearly track data into Chart.js line datasets, calculating + * cumulative distance for each year with dates normalized to 2024 for comparison. + * @param tracksByYear Dictionary mapping years to arrays of Track objects + * @returns Array of Chart.js datasets with cumulative distance data, larger point radius for the most recent year + */ +function generateChartDataSets(tracksByYear: TracksByYearDict) { + + const yearList = _.keys(tracksByYear).map((ys) => Number.parseInt(ys)) + yearList.sort().reverse() + + const maxYear = Math.max(...yearList) + + // Add one dataset per year, with cumulative distance and normalized dates for comparison + const returnDataSetList: DSet[] = [] + for (const year of yearList) { + + if (tracksByYear[year] !== undefined) { + + // Clean and sort tracks for the year + const sortedValidTracks: Track[] = [] + for (const track of tracksByYear[year]) { + + const tTime = track.getTime() + + if (tTime?.isValid) { + sortedValidTracks.push(track) + } + sortedValidTracks.sort((a, b) => (a.getTime()!.toSeconds() - b.getTime()!.toSeconds())) + } + + // map tracks to dataset structure with cumulative distance, normalizing dates to 2024 for comparison across years + let sum = 0 + const chartData: ChartDataPoint[] = sortedValidTracks.map( + (t: Track) => { + const step = t.distance() / 1000 + sum += step + + const trackDate = t.getTime() as DateTime + const normDate = trackDate.set({ year: 2024 }) + return { + x: normDate, y: sum, step, name: t.getNameOrSrc(), originalDate: trackDate + } + }) + + // Label below is for whole dataset, not individual points + const dataset: DSet = { + label: year.toString(), + data: chartData, + pointRadius: (year === maxYear) ? 5 : 3 + } + + returnDataSetList.push(dataset) + } + } + return returnDataSetList +} + +export { generateChartDataSets, type TracksByYearDict, type ChartDataPoint, type DSet } \ No newline at end of file diff --git a/client/src/views/ProgressChart.vue b/client/src/views/ProgressChart.vue index 4bf7e79..08b00c5 100644 --- a/client/src/views/ProgressChart.vue +++ b/client/src/views/ProgressChart.vue @@ -14,6 +14,7 @@ + + diff --git a/client/src/views/TrackOverviewPage.vue b/client/src/views/TrackOverviewPage.vue index c28e6ce..fed0880 100644 --- a/client/src/views/TrackOverviewPage.vue +++ b/client/src/views/TrackOverviewPage.vue @@ -1,14 +1,12 @@