-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8ead7c4
commit 22e16ae
Showing
6 changed files
with
165 additions
and
87 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
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
80 changes: 80 additions & 0 deletions
80
frontend/src/charts/rateBarChart/RoundedBarsWithLabels.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,80 @@ | ||
import { max, type ScaleBand, type ScaleLinear } from 'd3' | ||
import { useMemo } from 'react' | ||
import type { MetricConfig } from '../../data/config/MetricConfigTypes' | ||
import type { DemographicType } from '../../data/query/Breakdowns' | ||
import type { HetRow } from '../../data/utils/DatasetTypes' | ||
import { LABEL_SWAP_CUTOFF_PERCENT } from './constants' | ||
import EndOfBarLabel from './EndOfBarLabel' | ||
import { buildRoundedBarString } from './helpers' | ||
|
||
interface RoundedBarsWithLabelsProps { | ||
processedData: HetRow[] | ||
metricConfig: MetricConfig | ||
demographicType: DemographicType | ||
xScale: ScaleLinear<number, number> | ||
yScale: ScaleBand<string> | ||
getYPosition: (index: number, label: string) => number | ||
isTinyAndUp: boolean | ||
handleTooltip: any | ||
closeTooltip: any | ||
} | ||
|
||
export default function RoundedBarsWithLabels( | ||
props: RoundedBarsWithLabelsProps, | ||
) { | ||
const barLabelBreakpoint = useMemo(() => { | ||
const maxValue = | ||
max(props.processedData, (d) => d[props.metricConfig.metricId]) || 0 | ||
return maxValue * (LABEL_SWAP_CUTOFF_PERCENT / 100) | ||
}, [props.processedData, props.metricConfig.metricId]) | ||
|
||
return props.processedData.map((d, index) => { | ||
const barWidth = props.xScale(d[props.metricConfig.metricId]) || 0 | ||
const shouldLabelBeInside = | ||
d[props.metricConfig.metricId] > barLabelBreakpoint | ||
const yPosition = props.getYPosition(index, d[props.demographicType]) | ||
|
||
const barLabelColor = | ||
shouldLabelBeInside && d[props.demographicType] !== 'All' | ||
? 'fill-white' | ||
: 'fill-current' | ||
|
||
const roundedBarString = buildRoundedBarString(barWidth, props.yScale) | ||
|
||
if (!roundedBarString) return <></> | ||
|
||
const barAriaLabel = `${d[props.demographicType]}: ${d[props.metricConfig.metricId]} ${props.metricConfig.shortLabel}` | ||
|
||
return ( | ||
<g | ||
key={index + barAriaLabel} | ||
transform={`translate(0,${yPosition})`} | ||
onMouseMove={(e) => props.handleTooltip(e, d, false)} | ||
onMouseLeave={props.closeTooltip} | ||
onTouchStart={(e) => { | ||
props.handleTooltip(e, d, true) | ||
}} | ||
> | ||
<path | ||
d={roundedBarString} | ||
key={'path' + index + barAriaLabel} | ||
className={ | ||
d[props.demographicType] === 'All' | ||
? 'fill-timeYellow' | ||
: 'fill-altGreen' | ||
} | ||
aria-label={barAriaLabel} | ||
/> | ||
<EndOfBarLabel | ||
{...props} | ||
d={d} | ||
shouldLabelBeInside={shouldLabelBeInside} | ||
barWidth={barWidth} | ||
yScale={props.yScale} | ||
barLabelColor={barLabelColor} | ||
isTinyAndUp={props.isTinyAndUp} | ||
/> | ||
</g> | ||
) | ||
}) | ||
} |
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,54 @@ | ||
import type { ScaleBand } from 'd3' | ||
import { useMemo } from 'react' | ||
import { | ||
type DemographicType, | ||
DEMOGRAPHIC_DISPLAY_TYPES_LOWER_CASE, | ||
} from '../../data/query/Breakdowns' | ||
import type { HetRow } from '../../data/utils/DatasetTypes' | ||
import type { Fips } from '../../data/utils/Fips' | ||
import { MARGIN, Y_AXIS_LABEL_HEIGHT } from './constants' | ||
import GroupLabelsYAxis from './GroupLabelsYAxis' | ||
import { wrapLabel } from './helpers' | ||
|
||
interface YAxisProps { | ||
demographicType: DemographicType | ||
isSmAndUp: boolean | ||
processedData: HetRow[] | ||
maxLabelWidth: number | ||
yScale: ScaleBand<string> | ||
getYPosition: (index: number, label: string) => number | ||
fips: Fips | ||
} | ||
export default function YAxis(props: YAxisProps) { | ||
const wrappedLabels = useMemo(() => { | ||
return props.processedData.map((d) => ({ | ||
original: d[props.demographicType], | ||
lines: wrapLabel(d[props.demographicType], props.maxLabelWidth), | ||
})) | ||
}, [props.processedData, props.demographicType]) | ||
|
||
return ( | ||
<g> | ||
{props.isSmAndUp && ( | ||
<g> | ||
<text | ||
transform={`translate(${-MARGIN.left + Y_AXIS_LABEL_HEIGHT},${innerHeight / 2}) rotate(-90)`} | ||
textAnchor='middle' | ||
className='text-smallest font-semibold p-0 m-0' | ||
aria-label={'Y Axis Label'} | ||
> | ||
{DEMOGRAPHIC_DISPLAY_TYPES_LOWER_CASE[props.demographicType]} | ||
</text> | ||
</g> | ||
)} | ||
|
||
<GroupLabelsYAxis | ||
{...props} | ||
wrappedLabels={wrappedLabels} | ||
yScale={props.yScale} | ||
getYPosition={props.getYPosition} | ||
fips={props.fips} | ||
/> | ||
</g> | ||
) | ||
} |
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