Skip to content

Instantly share code, notes, and snippets.

@vedantroy
Created November 20, 2021 22:14
Show Gist options
  • Save vedantroy/b6737de470bfac12986b977ee9624993 to your computer and use it in GitHub Desktop.
Save vedantroy/b6737de470bfac12986b977ee9624993 to your computer and use it in GitHub Desktop.
Exercise_Visualization_Backup.html
<html>
<head>
<meta charset="UTF-8" />
<style>
* { margin:0; padding:0; box-sizing:border-box; }
body {
width: 100%;
height: 100%;
}
#ExerciseLineChart {
width: 100%;
height: 100%;
}
</style>
<script>
window.INJECTED_RAW_DATA =
'[{"date":"27-10-21","bench":["4x4x125"]},{"date":"06-11-21F","bench":["5x125","7x125","2x125","4x115"],"OHP_dumbell":["4x4x70"],"squat":["8x95","3x4x115"]},{"date":"08-11-21","bench":["3x125f","4x95","2x4x105"],"OHP_dumbell":["3x8x60","6x60f"],"squat":["8x95","3x4x115"]},{"date":"10-11-21","bench":["5x95w","2x8x115","6x115","6x105"],"OHP_dumbell":["3x8x70","7x70"],"deadlift":["3x4x135","6x135"]},{"date":"12-11-21F","bench":["4x4x135"],"squat":["2x8x95","2x6x115"],"OHP_dumbell":["8x70","7x70","4x70"]},{"date":"14-11-21F","bench":["5x95w","2x8x115","8x125","6x125"],"deadlift":["4x135","7x135","8x135","6x135"],"OHP_dumbell":["3x4x80","2x80f"]},{"date":"18-11-21","bench":["4x4x135"],"OHP_dumbell":["2x8x70","7x70","?4x70"],"squat":["2x4x115","4x135","3x15"]}]'
</script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/lodash.min.js"></script>
<script src="https://unpkg.com/[email protected]/dayjs.min.js"></script>
<script src="https://unpkg.com/[email protected]/plugin/customParseFormat.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chart.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/build/global/luxon.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/chartjs-adapter-luxon.min.js"></script>
</head>
<body>
<canvas id="ExerciseLineChart"></canvas>
</body>
<script>
dayjs.extend(window.dayjs_plugin_customParseFormat)
const EXERCISES = ["bench", "deadlift", "squat", "OHP_dumbell"];
const data = JSON.parse(window.INJECTED_RAW_DATA);
const count = (str, ch) => _.countBy(str)[ch] || 0;
const assert = (cond, msg) => {
if (!cond) {
throw new Error(msg);
}
};
const expandedData = data.map(({ date, ...theRest }) => {
if (date.endsWith("F")) date = date.slice(0, -1);
const normalizedExercises = _(theRest)
.toPairs()
.map(([exerciseName, sets]) => {
assert(
EXERCISES.includes(exerciseName),
`Exercise: ${exerciseName} is not a valid exercise in: ${EXERCISES}`
);
const normalizedSets = _(sets)
.filter((reps) => reps.slice(-1) !== "w")
.map((reps) => {
const Xs = count(reps, "x");
assert(Xs === 1 || Xs === 2, `Invalid number of Xs: ${Xs} in ${reps}`);
const removeChar = (char, pos) => {
if (reps.charAt(pos) === char) {
assert(
Xs === 1,
`Cannot use ${char} when recording more than 1 set`
);
return reps.slice(0, pos) + reps.slice(pos + 1);
}
return reps
};
reps = removeChar("?", 0);
reps = removeChar("f", reps.length - 1);
return reps;
})
.value();
const expandedSets = []
for (const set of normalizedSets) {
const c = count(set, "x")
if (c === 2) {
const firstX = set.indexOf("x")
let numSets = parseInt(set.slice(0, firstX))
while (numSets-- > 0) expandedSets.push(set.slice(firstX + 1))
} else expandedSets.push(set)
}
const parsedSets = expandedSets.map(set => {
assert(count(set, "x") === 1, `Invalid set (after expansion): ${set}`)
const xIdx = set.indexOf("x")
assert(xIdx > 0, `Invalid set: ${set}`)
const reps = parseInt(set.slice(0, xIdx))
const weight = parseInt(set.slice(xIdx + 1))
return [reps, weight]
})
return [exerciseName, parsedSets];
})
.fromPairs()
.value();
// sadly, I probably won't live past 2100 so we can use 2 digit years
return { date: dayjs(date, "DD-MM-YY").toDate(), ...normalizedExercises };
});
// ChartJS stuff
const ExerciseChart = new Chart(document.getElementById("ExerciseLineChart").getContext("2d"), {
type: 'line',
options: {
scales: {
x: { type: 'time', distribution: 'linear' }
}
}
})
</script>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment