-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(MoodBySleepForPeriod): add chart
- Loading branch information
Showing
5 changed files
with
85 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
client/src/components/pages/Stats/MoodBySleepForPeriod.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import { Chart, Paper } from "eri"; | ||
import { MOOD_INTEGERS, MOOD_RANGE } from "../../../constants"; | ||
import { | ||
computeMean, | ||
defaultDict, | ||
getIdsInInterval, | ||
moodToColor, | ||
} from "../../../utils"; | ||
import eventsSlice from "../../../store/eventsSlice"; | ||
import { useSelector } from "react-redux"; | ||
|
||
interface Props { | ||
dateFrom: Date; | ||
dateTo: Date; | ||
} | ||
|
||
export default function MoodBySleepForPeriod({ dateFrom, dateTo }: Props) { | ||
const meanMoodsByDay = useSelector(eventsSlice.selectors.meanMoodsByDay); | ||
const minutesSleptByDateAwoke = useSelector( | ||
eventsSlice.selectors.minutesSleptByDateAwoke, | ||
); | ||
const minutesSleptKeys = getIdsInInterval( | ||
Object.keys(minutesSleptByDateAwoke), | ||
dateFrom, | ||
dateTo, | ||
); | ||
const meanMoodsKeys = getIdsInInterval( | ||
Object.keys(meanMoodsByDay), | ||
dateFrom, | ||
dateTo, | ||
); | ||
if (minutesSleptKeys.length < 2 || meanMoodsKeys.length < 2) return; | ||
const meanMoodsKeysSet = new Set(meanMoodsKeys); | ||
const intersectingKeys = minutesSleptKeys.filter((key) => | ||
meanMoodsKeysSet.has(key), | ||
); | ||
if (intersectingKeys.length < 2) return; | ||
|
||
const points = intersectingKeys.map((key) => { | ||
const y = meanMoodsByDay[key]; | ||
return { | ||
color: moodToColor(y), | ||
x: minutesSleptByDateAwoke[key] / 60, | ||
y, | ||
}; | ||
}); | ||
|
||
const ysByX = defaultDict((): number[] => []); | ||
// Round to nearest 12 for a resolution of 5 minutes | ||
for (const { x, y } of points) ysByX[Math.round(x * 12) / 12].push(y); | ||
const linePoints = Object.keys(ysByX).map((x) => ({ | ||
x: Number(x), | ||
y: computeMean(ysByX[x]), | ||
})); | ||
|
||
return ( | ||
<Paper> | ||
<h3>Average mood by time slept</h3> | ||
<Chart.LineChart | ||
aria-label="Chart displaying mood against time slept" | ||
domain={[0, Math.ceil(Math.max(...points.map(({ x }) => x)) / 5) * 5]} | ||
points={points} | ||
range={MOOD_RANGE} | ||
xAxisTitle="Hours slept" | ||
yAxisLabels={MOOD_INTEGERS.map(String)} | ||
yAxisTitle="Mood" | ||
> | ||
<Chart.Line | ||
thickness={2} | ||
data={linePoints | ||
.map(({ x, y }): [number, number] => [x, y]) | ||
.sort(([a], [b]) => a - b)} | ||
/> | ||
</Chart.LineChart> | ||
</Paper> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters