Skip to content

Commit

Permalink
feat(graph): added legend to sleep stage breakdown graph
Browse files Browse the repository at this point in the history
  • Loading branch information
TomPlum committed Nov 17, 2024
1 parent 947600b commit 072d7d6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
import { CartesianGrid, ReferenceLine, ResponsiveContainer, Scatter, ScatterChart, Tooltip, XAxis, YAxis } from 'recharts'
import {
CartesianGrid,
Legend,
ReferenceLine,
ResponsiveContainer,
Scatter,
ScatterChart,
Tooltip,
XAxis,
YAxis
} from 'recharts'
import { SleepMetric } from 'modules/controls/MetricConfiguration'
import { SleepSessionStageBreakdownGraphProps } from 'modules/graph/components/SleepSessionStageBreakdownGraph/types'
import { useCallback, useMemo } from 'react'
Expand All @@ -7,6 +17,7 @@ import { SleepStage } from 'data/useSleepData'
import { SleepStageBar } from 'modules/graph/components/SleepStageBar'
import styles from './SleepSessionStageBreakdownGraph.module.scss'
import { useChartSize } from 'modules/graph/hooks/useChartSize'
import { getMetricColour } from 'modules/graph/hooks/useGraphStyles'

export const SleepSessionStageBreakdownGraph = ({ stages, sounds }: SleepSessionStageBreakdownGraphProps) => {
const { size, chartRef } = useChartSize()
Expand Down Expand Up @@ -167,6 +178,23 @@ export const SleepSessionStageBreakdownGraph = ({ stages, sounds }: SleepSession
}

<Tooltip />

<Legend
height={30}
verticalAlign='top'
id='sleep-stage-breakdown-legend'
payload={yDomain.map(stage => ({
id: stage,
value: stage,
type: 'diamond',
color: getMetricColour(stage)
}))}
formatter={(value) => (
<span style={{ color: getMetricColour(value as SleepMetric) }}>
{`${value.split('_').map((v: string) => `${v.charAt(0).toUpperCase()}${v.slice(1)}`).join(' ')}`}
</span>
)}
/>
</ScatterChart>
</ResponsiveContainer>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { SleepStageBarProps } from './types'

export const SleepStageBar = ({ cx, cy, payload, xAxis, chartHeight, uniqueMetrics }: SleepStageBarProps) => {
const barWidth = xAxis.scale(payload.end.valueOf()) - xAxis.scale(payload.start.valueOf())
const barHeight = (chartHeight / uniqueMetrics) - 35
const barHeight = (chartHeight / uniqueMetrics * 0.9) - 40

return (
<g>
Expand Down

0 comments on commit 072d7d6

Please sign in to comment.