Skip to content

Commit

Permalink
feat: add settings to ShotDispersion
Browse files Browse the repository at this point in the history
  • Loading branch information
thraizz committed Jun 2, 2024
1 parent 82f727a commit fe414d7
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 8 deletions.
3 changes: 0 additions & 3 deletions src/components/panels/AveragesPerSession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,3 @@ const parseDate = (input: string) => {
return input;
}
};

console.log(parseDate("01/01/21"));
console.log(parseDate("19.05.24"));
1 change: 0 additions & 1 deletion src/components/panels/AveragesPerSessionGraph.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ export const AveragesPerSessionGraph = ({
yField: keyof GolfSwingData;
data: ClubDataForTable;
}) => {
console.log(data);
const spec: VisualizationSpec = {
$schema: "https://vega.github.io/schema/vega-lite/v5.json",
data: { name: "table" },
Expand Down
6 changes: 4 additions & 2 deletions src/components/panels/ShotDispersion.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { useMemo } from "react";
import { Vega, VisualizationSpec } from "react-vega";
import { useSelectedSessions } from "../../hooks/useSelectedSessions";
import { useSelectedSessionsWithSettings } from "../../hooks/useSelectedSessions";
import { GolfSwingData } from "../../types/GolfSwingData";
import { SettingsForm } from "./SettingsForm";

export const ShotDispersion = () => {
const shots = useCarryAndDeviation();
Expand Down Expand Up @@ -35,6 +36,7 @@ export const ShotDispersion = () => {

return (
<div className="flex h-auto flex-col gap-3 rounded-xl bg-white p-4">
<SettingsForm />
<h4 className="mb-4 text-xl font-bold text-gray-800">Shot Dispersion</h4>
<div className="block h-[400px] w-full">
<Vega spec={spec} data={{ table: shots }} />
Expand All @@ -44,7 +46,7 @@ export const ShotDispersion = () => {
};

const useCarryAndDeviation = () => {
const sessions = useSelectedSessions();
const sessions = useSelectedSessionsWithSettings();

const shots = useMemo(() => {
if (sessions) {
Expand Down
20 changes: 20 additions & 0 deletions src/hooks/useSelectedSessions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { useContext } from "react";
import { SessionContext } from "../provider/SessionContext";
import { SettingsContext } from "../provider/SettingsContext";
import { Sessions } from "../types/Sessions";
import { dropOutliers, getAboveAverageShots } from "../utils/calculateAverages";

export const useSelectedSessions = () => {
const { sessions } = useContext(SessionContext);
Expand All @@ -12,3 +14,21 @@ export const useSelectedSessions = () => {
return acc;
}, {} as Sessions);
};

export const useSelectedSessionsWithSettings = () => {
const sessions: Sessions = useSelectedSessions();
const { settings } = useContext(SettingsContext);

return Object.values(sessions).reduce((acc, session) => {
let results = session.results;
if (settings.useIQR) {
results = dropOutliers(session.results);
}
if (settings.useAboveAverageShots) {
results = getAboveAverageShots(session.results);
}

acc[session.date] = { ...session, results };
return acc; // Add this line to return the accumulator
}, {});
};
4 changes: 2 additions & 2 deletions src/utils/calculateAverages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ const lobwedgeVariations = [
*
* @param swings - The swings to filter
*/
const dropOutliers = (swings: GolfSwingData[]) => {
export const dropOutliers = (swings: GolfSwingData[]) => {
// Filter out outliers
const filteredSwings = swings.filter((swing) => {
const club = swing["Schlägerart"] || swing["Club Type"];
Expand All @@ -293,7 +293,7 @@ const dropOutliers = (swings: GolfSwingData[]) => {
return filteredSwings;
};

const getAboveAverageShots = (swings: GolfSwingData[]) => {
export const getAboveAverageShots = (swings: GolfSwingData[]) => {
// Filter out outliers
const filteredSwings = swings.filter((swing) => {
const club = swing["Schlägerart"] || swing["Club Type"];
Expand Down

0 comments on commit fe414d7

Please sign in to comment.