2021-09-26 08:59:17 +02:00
|
|
|
import {
|
2021-09-30 21:09:15 +02:00
|
|
|
IWorkout,
|
2021-09-26 08:59:17 +02:00
|
|
|
IWorkoutApiChartData,
|
|
|
|
IWorkoutChartData,
|
2021-09-26 11:21:09 +02:00
|
|
|
TCoordinates,
|
2021-09-26 08:59:17 +02:00
|
|
|
TWorkoutDatasets,
|
|
|
|
} from '@/types/workouts'
|
2021-11-14 13:33:07 +01:00
|
|
|
import { convertStatsDistance } from '@/utils/units'
|
2021-09-26 08:59:17 +02:00
|
|
|
|
|
|
|
export const getDatasets = (
|
|
|
|
chartData: IWorkoutApiChartData[],
|
2021-11-14 13:33:07 +01:00
|
|
|
t: CallableFunction,
|
|
|
|
useImperialUnits: boolean
|
2021-09-26 08:59:17 +02:00
|
|
|
): IWorkoutChartData => {
|
|
|
|
const datasets: TWorkoutDatasets = {
|
|
|
|
speed: {
|
|
|
|
label: t('workouts.SPEED'),
|
|
|
|
backgroundColor: ['#FFFFFF'],
|
|
|
|
borderColor: ['#8884d8'],
|
|
|
|
borderWidth: 2,
|
|
|
|
data: [],
|
|
|
|
yAxisID: 'ySpeed',
|
|
|
|
},
|
|
|
|
elevation: {
|
|
|
|
label: t('workouts.ELEVATION'),
|
|
|
|
backgroundColor: ['#e5e5e5'],
|
|
|
|
borderColor: ['#cccccc'],
|
|
|
|
borderWidth: 1,
|
|
|
|
fill: true,
|
|
|
|
data: [],
|
|
|
|
yAxisID: 'yElevation',
|
|
|
|
},
|
|
|
|
}
|
|
|
|
const distance_labels: unknown[] = []
|
|
|
|
const duration_labels: unknown[] = []
|
2021-09-26 11:21:09 +02:00
|
|
|
const coordinates: TCoordinates[] = []
|
2021-09-26 08:59:17 +02:00
|
|
|
|
|
|
|
chartData.map((data) => {
|
|
|
|
distance_labels.push(data.distance)
|
|
|
|
duration_labels.push(data.duration)
|
2021-11-14 13:33:07 +01:00
|
|
|
datasets.speed.data.push(
|
|
|
|
convertStatsDistance('km', data.speed, useImperialUnits)
|
|
|
|
)
|
|
|
|
datasets.elevation.data.push(
|
|
|
|
convertStatsDistance('m', data.elevation, useImperialUnits)
|
|
|
|
)
|
2021-09-26 11:21:09 +02:00
|
|
|
coordinates.push({ latitude: data.latitude, longitude: data.longitude })
|
2021-09-26 08:59:17 +02:00
|
|
|
})
|
|
|
|
|
2021-09-26 11:21:09 +02:00
|
|
|
return { distance_labels, duration_labels, datasets, coordinates }
|
2021-09-26 08:59:17 +02:00
|
|
|
}
|
2021-09-30 21:09:15 +02:00
|
|
|
|
|
|
|
export const getDonutDatasets = (
|
|
|
|
workouts: IWorkout[]
|
|
|
|
): Record<number, Record<string, number>> => {
|
|
|
|
const total = workouts.length
|
|
|
|
if (total === 0) {
|
|
|
|
return {}
|
|
|
|
}
|
|
|
|
|
|
|
|
const datasets: Record<number, Record<string, number>> = {}
|
|
|
|
workouts.map((workout) => {
|
|
|
|
if (!datasets[workout.sport_id]) {
|
|
|
|
datasets[workout.sport_id] = {
|
|
|
|
count: 0,
|
|
|
|
percentage: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
datasets[workout.sport_id].count += 1
|
|
|
|
datasets[workout.sport_id].percentage =
|
|
|
|
datasets[workout.sport_id].count / total
|
|
|
|
})
|
|
|
|
|
|
|
|
return datasets
|
|
|
|
}
|
2021-11-02 12:24:31 +01:00
|
|
|
|
|
|
|
export const defaultOrder = {
|
|
|
|
order: 'desc',
|
|
|
|
order_by: 'workout_date',
|
|
|
|
}
|