77 */
88import { useMemo , useState } from 'react'
99import * as R from 'remeda'
10+ import { match } from 'ts-pattern'
1011import type uPlot from 'uplot'
1112
1213import { ChartTooltip , type TopBottom , type LeftRight } from '~/components/ChartTooltip'
1314import { type UPlotOptions , FramedChart } from '~/components/FramedChart'
15+ import {
16+ getSampleCountMetadata ,
17+ getScalingFunction ,
18+ type ColorScale ,
19+ } from '~/components/heatmap-color-scale'
1420import {
1521 timeFormatterForRange ,
1622 useChartTheme ,
@@ -40,38 +46,20 @@ const RAMP_STOPS = [
4046 [ '--content-accent' , 'oklch(0.77 0.1919 163.7)' ] ,
4147] as const
4248
49+ const CSS_VAR_RAMP : ColorRamp = {
50+ stops : RAMP_STOPS . map ( ( [ name , fallback ] ) => `var(${ name } , ${ fallback } )` ) ,
51+ }
52+
4353// Interpolates the color between the two closest stops, for 0 <= t <= 1.
4454const rampColor = ( { stops } : ColorRamp , t : number ) : string => {
4555 const clamped = R . clamp ( t , { min : 0 , max : 1 } )
4656 const lastIndex = stops . length - 1
4757 const progress = clamped * lastIndex
4858 const start = R . clamp ( Math . floor ( progress ) , { max : lastIndex - 1 } )
49- const lerp = progress - start
59+ const localT = progress - start
5060 const from = stops [ start ]
5161 const to = stops [ start + 1 ]
52- return `color-mix(in oklch, ${ from } ${ ( 1 - lerp ) * 100 } %, ${ to } )`
53- }
54-
55- // grabbing style from the chart root instead of document body
56- // that way it can be themed e.g. `yellow-theme`
57- const getColorRamp = ( el : Element ) : ColorRamp => {
58- const style = getComputedStyle ( el )
59- return {
60- stops : RAMP_STOPS . map (
61- ( [ name , fallback ] ) => style . getPropertyValue ( name ) . trim ( ) || fallback
62- ) ,
63- }
64- }
65-
66- // count -> ramp position. Doing a square root or a log makes for some different
67- // smoothing. Nothing is truly intuitive, but linearly showing the fraction is
68- // most easily compared to the legend at the bottom
69- const rampT = ( count : number , maxCount : number ) => {
70- const fraction = count / maxCount
71- // TODO: pick!
72- return fraction
73- // return Math.log10(lerp(1, 10, fraction))
74- // return Math.sqrt(fraction)
62+ return `color-mix(in oklch, ${ from } , ${ to } ${ localT * 100 } %)`
7563}
7664
7765type Hover = {
@@ -93,6 +81,7 @@ type HeatmapProps = {
9381 unit ?: string
9482 /** Drawn rotated alongside the y-axis tick labels, e.g. a unit */
9583 yAxisLabel ?: string
84+ colorScale ?: ColorScale
9685}
9786
9887const defaultYAxisTickFormatter = ( val : number ) => val . toLocaleString ( )
@@ -105,6 +94,7 @@ export function Heatmap({
10594 yAxisTickFormatter = defaultYAxisTickFormatter ,
10695 unit,
10796 yAxisLabel,
97+ colorScale = 'ranked' ,
10898} : HeatmapProps ) {
10999 const theme = useChartTheme ( )
110100 const [ hover , setHover ] = useState < Hover | null > ( null )
@@ -118,8 +108,8 @@ export function Heatmap({
118108 const binCount = bins . length
119109 const colCount = distributions . length
120110
121- const maxSampleCount = useMemo (
122- ( ) => distributions . reduce ( ( max , d ) => ( d ? Math . max ( max , ... d . counts ) : max ) , 0 ) ,
111+ const sampleCountMetadata = useMemo (
112+ ( ) => getSampleCountMetadata ( distributions . flatMap ( ( d ) => d ?. counts ?? [ ] ) ) ,
123113 [ distributions ]
124114 )
125115
@@ -129,10 +119,22 @@ export function Heatmap({
129119
130120 // uPlot doesn't actually have heatmap support, but we can use its position
131121 // functions to make this (somewhat) easy
132- const drawCells = useMemo (
133- ( ) => ( u : uPlot ) => {
122+ const drawCells = useMemo ( ( ) => {
123+ const scaleCount = getScalingFunction ( sampleCountMetadata , colorScale )
124+
125+ return ( u : uPlot ) => {
134126 if ( binCount === 0 || colCount === 0 ) return
135- const colorRamp = getColorRamp ( u . root )
127+
128+ // since a canvas can't resolve CSS variables, we simulate it by reading
129+ // the ramp colors off the element furthest down the tree (the chart's
130+ // root element)
131+ const style = getComputedStyle ( u . root )
132+ const colorRamp = {
133+ stops : RAMP_STOPS . map (
134+ ( [ name , fallback ] ) => style . getPropertyValue ( name ) . trim ( ) || fallback
135+ ) ,
136+ }
137+
136138 const ctx = u . ctx
137139 ctx . save ( )
138140 ctx . beginPath ( )
@@ -149,7 +151,7 @@ export function Heatmap({
149151
150152 const topEdge = u . valToPos ( rowIndex + 1 , 'y' , true )
151153 const bottomEdge = u . valToPos ( rowIndex , 'y' , true )
152- ctx . fillStyle = rampColor ( colorRamp , rampT ( count , maxSampleCount ) )
154+ ctx . fillStyle = rampColor ( colorRamp , scaleCount ( count ) )
153155 ctx . fillRect (
154156 leftEdge - CELL_OVERFLOW ,
155157 topEdge - CELL_OVERFLOW ,
@@ -159,9 +161,16 @@ export function Heatmap({
159161 } )
160162 } )
161163 ctx . restore ( )
162- } ,
163- [ distributions , leftEdges , rightEdges , colCount , binCount , maxSampleCount ]
164- )
164+ }
165+ } , [
166+ distributions ,
167+ leftEdges ,
168+ rightEdges ,
169+ colCount ,
170+ binCount ,
171+ colorScale ,
172+ sampleCountMetadata ,
173+ ] )
165174
166175 const tooltipPlugin = useMemo < uPlot . Plugin > (
167176 ( ) => ( {
@@ -307,7 +316,19 @@ export function Heatmap({
307316 chartOptions = { chartOptions }
308317 data = { data }
309318 uRef = { uRef }
310- legend = { < HeatmapLegend maxSampleCount = { maxSampleCount } axisText = { theme . axisText } /> }
319+ legend = {
320+ sampleCountMetadata . ranks . size > 0 && (
321+ < HeatmapLegend
322+ minSampleCount = { match ( colorScale )
323+ . with ( 'ranked' , ( ) => sampleCountMetadata . minSampleCount )
324+ . with ( 'linear' , ( ) => 0 )
325+ . with ( 'logarithmic' , ( ) => 1 )
326+ . exhaustive ( ) }
327+ maxSampleCount = { sampleCountMetadata . maxSampleCount }
328+ axisText = { theme . axisText }
329+ />
330+ )
331+ }
311332 >
312333 { hover && (
313334 < >
@@ -342,28 +363,69 @@ export function Heatmap({
342363}
343364
344365function HeatmapLegend ( {
366+ minSampleCount,
345367 maxSampleCount,
346368 axisText,
347369} : {
370+ minSampleCount : number
348371 maxSampleCount : number
349372 axisText : string
350373} ) {
351374 return (
352375 < div className = "mt-3 flex items-center gap-2 pl-5" >
353- < span className = "text-mono-xs" style = { { color : axisText } } >
354- 0
355- </ span >
356- < div
357- className = "h-3 w-32 rounded-sm"
358- style = { {
359- backgroundImage : `linear-gradient(in oklch to right, ${ RAMP_STOPS . map (
360- ( [ name , fallback ] ) => `var(${ name } , ${ fallback } )`
361- ) . join ( ', ' ) } )`,
362- } }
363- > </ div >
376+ { minSampleCount === maxSampleCount ? (
377+ < div
378+ className = "h-3 w-3 rounded-sm"
379+ style = { {
380+ backgroundColor : R . last ( CSS_VAR_RAMP . stops ) ,
381+ } }
382+ > </ div >
383+ ) : (
384+ < >
385+ < span className = "text-mono-xs" style = { { color : axisText } } >
386+ { minSampleCount . toLocaleString ( ) }
387+ </ span >
388+ < div
389+ className = "h-3 w-32 rounded-sm"
390+ style = { {
391+ backgroundImage : `linear-gradient(in oklch to right, ${ CSS_VAR_RAMP . stops . join ( ', ' ) } )` ,
392+ } }
393+ > </ div >
394+ </ >
395+ ) }
364396 < span className = "text-mono-xs" style = { { color : axisText } } >
365397 { maxSampleCount . toLocaleString ( ) }
366398 </ span >
367399 </ div >
368400 )
369401}
402+
403+ const SAMPLE_VALUES = [ 1 , 5 , 10 , 50 , 95 , 100 ]
404+ const SAMPLE_VALUES_METADATA = getSampleCountMetadata ( SAMPLE_VALUES )
405+ const LONGEST_SAMPLE_ITEM = SAMPLE_VALUES . map ( ( n ) => n . toLocaleString ( ) . length ) . reduce (
406+ ( acc , length ) => Math . max ( acc , length ) ,
407+ 0
408+ )
409+
410+ export function SampleColorStrip ( { colorScale } : { colorScale : ColorScale } ) {
411+ const scaleCount = getScalingFunction ( SAMPLE_VALUES_METADATA , colorScale )
412+
413+ return (
414+ < div className = "flex items-end gap-px" >
415+ { SAMPLE_VALUES . map ( ( n ) => (
416+ < div
417+ key = { n }
418+ className = "text-secondary text-mono-code flex flex-col items-center text-xs"
419+ >
420+ < div className = "text-center" style = { { width : `${ LONGEST_SAMPLE_ITEM + 0.5 } ch` } } >
421+ { n . toLocaleString ( ) }
422+ </ div >
423+ < div
424+ className = "h-3 w-full"
425+ style = { { backgroundColor : rampColor ( CSS_VAR_RAMP , scaleCount ( n ) ) } }
426+ />
427+ </ div >
428+ ) ) }
429+ </ div >
430+ )
431+ }
0 commit comments