File size: 14,504 Bytes
d15f07c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/**
 * Scale transforms for choropleth and bubble maps (inspired by d3-in-angular COVID demo).
 * Maps a raw metric value to a display position t ∈ [0, 1] for color and bubble sizing.
 */

import type { CensusValueMode } from './censusMapValueMode'
import { giniLetterSuffix } from './giniLetterGrade'

export type CensusScaleId = 'linear' | 'sqrt' | 'log' | 'exp'

/**
 * Choropleth fill easing close to D3’s ``easeCubicInOut`` (see d3-in-angular county demos).
 * Use on SVG ``style`` (not the ``fill`` attribute) so the browser can interpolate colors.
 */
export const CENSUS_CHORO_FILL_TRANSITION =
  'fill 1.2s cubic-bezier(0.65, 0, 0.35, 1), stroke 0.45s cubic-bezier(0.65, 0, 0.35, 1)'

/** Robust choropleth range (reduces β€œflat” maps when a few outliers dominate min/max). */
export function quantileExtent(
  values: (number | null | undefined)[],
  qLow = 0.04,
  qHigh = 0.96,
): { min: number; max: number } {
  const nums = values
    .filter((x): x is number => typeof x === 'number' && Number.isFinite(x))
    .sort((a, b) => a - b)
  const n = nums.length
  if (n < 2) return { min: 0, max: 1 }
  const lo = nums[Math.max(0, Math.min(n - 1, Math.floor(qLow * (n - 1))))]!
  const hi = nums[Math.max(0, Math.min(n - 1, Math.ceil(qHigh * (n - 1))))]!
  if (!(lo < hi)) {
    const mid = nums[Math.floor(n / 2)]!
    return { min: mid * 0.9, max: mid * 1.1 }
  }
  return { min: lo, max: hi }
}

/** Min/max of observed values (same as ``quantileExtent(..., 0, 1)``). Used for bubble radii so sizes span the full metric range. */
export function minMaxExtent(values: (number | null | undefined)[]): { min: number; max: number } {
  return quantileExtent(values, 0, 1)
}

export const CENSUS_SCALES: { id: CensusScaleId; label: string }[] = [
  { id: 'linear', label: 'Linear' },
  { id: 'sqrt', label: 'Square root' },
  { id: 'log', label: 'Logarithmic' },
  { id: 'exp', label: 'Exponential (tΒ²)' },
]

export function metricToDisplayT(
  v: number | null | undefined,
  min: number,
  max: number,
  scale: CensusScaleId,
): number | null {
  if (v == null || !Number.isFinite(v) || max <= min) return null
  const clamped = Math.max(min, Math.min(max, v))
  const u = (clamped - min) / (max - min)
  switch (scale) {
    case 'linear':
      return u
    case 'sqrt':
      return Math.sqrt(Math.max(0, u))
    case 'log': {
      const lo = Math.log10(Math.max(min, 1))
      const hi = Math.log10(Math.max(max, 1))
      const span = hi - lo || 1e-9
      return (Math.log10(Math.max(clamped, 1)) - lo) / span
    }
    case 'exp':
      return u * u
    default:
      return u
  }
}

/** Multi-stop ramp (readable light end β†’ sky β†’ blue β†’ navy). Low end is darker
 *  than typical sequential blues so the floor of the scale still reads against
 *  the pale-blue map panel β€” otherwise the lowest-bucket places (e.g.
 *  Tuscaloosa city in a Tuscaloosa-County drill-down) fade into the
 *  background and look unfilled. */
const CHORO_RGB_STOPS: { t: number; rgb: [number, number, number] }[] = [
  { t: 0, rgb: [170, 184, 210] },
  { t: 0.12, rgb: [176, 195, 224] },
  { t: 0.28, rgb: [165, 198, 236] },
  { t: 0.44, rgb: [125, 211, 252] },
  { t: 0.58, rgb: [56, 189, 248] },
  { t: 0.72, rgb: [59, 130, 246] },
  { t: 0.86, rgb: [29, 78, 216] },
  { t: 1, rgb: [23, 37, 84] },
]

function lerpChannel(a: number, b: number, u: number): number {
  return Math.round(a + (b - a) * u)
}

export function colorFromT(t: number | null): string {
  if (t == null || !Number.isFinite(t)) return '#e2e8f0'
  const x = Math.min(1, Math.max(0, t))
  let i = 0
  while (i < CHORO_RGB_STOPS.length - 2 && x > CHORO_RGB_STOPS[i + 1]!.t) i += 1
  const lo = CHORO_RGB_STOPS[i]!
  const hi = CHORO_RGB_STOPS[i + 1]!
  const span = hi.t - lo.t || 1e-9
  const u = (x - lo.t) / span
  const r = lerpChannel(lo.rgb[0], hi.rgb[0], u)
  const g = lerpChannel(lo.rgb[1], hi.rgb[1], u)
  const b = lerpChannel(lo.rgb[2], hi.rgb[2], u)
  return `rgb(${r},${g},${b})`
}

/**
 * Bubble fill ramp β€” **Option 1 Β· Deep Ocean** (boardroom / clinical feel).
 * Steel Blue (low) β†’ Teal (mid) β†’ Deep Emerald (high). Distinct from choropleth fills.
 */
const BUBBLE_BRAND_RGB_STOPS: { t: number; rgb: [number, number, number] }[] = [
  { t: 0, rgb: [44, 110, 138] }, // #2C6E8A Steel Blue
  { t: 0.52, rgb: [42, 157, 143] }, // #2A9D8F Teal
  { t: 1, rgb: [27, 107, 90] }, // #1B6B5A Deep Emerald
]

/** Same interpolation as ``colorFromT`` but using the bubble brand ramp; optional alpha for SVG / canvas. */
export function bubbleFillFromT(t: number | null, alpha = 0.88): string {
  if (t == null || !Number.isFinite(t)) return `rgba(226, 232, 240, ${Math.min(1, alpha + 0.07)})`
  const x = Math.min(1, Math.max(0, t))
  let i = 0
  while (i < BUBBLE_BRAND_RGB_STOPS.length - 2 && x > BUBBLE_BRAND_RGB_STOPS[i + 1]!.t) i += 1
  const lo = BUBBLE_BRAND_RGB_STOPS[i]!
  const hi = BUBBLE_BRAND_RGB_STOPS[i + 1]!
  const span = hi.t - lo.t || 1e-9
  const u = (x - lo.t) / span
  const r = lerpChannel(lo.rgb[0], hi.rgb[0], u)
  const g = lerpChannel(lo.rgb[1], hi.rgb[1], u)
  const b = lerpChannel(lo.rgb[2], hi.rgb[2], u)
  return `rgba(${r},${g},${b},${alpha})`
}

export function bubbleRadiusPx(
  v: number | null | undefined,
  min: number,
  max: number,
  scale: CensusScaleId,
  rMin = 3,
  rMax = 18,
): number {
  const t = metricToDisplayT(v, min, max, scale)
  if (t == null) return rMin
  return rMin + t * (rMax - rMin)
}

/**
 * Compact tick labels for census map bar axes (matches former Recharts XAxis tickFormatter).
 * When ``valueSpan`` is small (Gini, age, rent %), use decimals instead of rounding to 0 / ``k``.
 */
export function formatCensusMapAxisTick(x: number, valueSpan?: number): string {
  const n = Number(x)
  if (!Number.isFinite(n)) return ''
  if (valueSpan != null && Number.isFinite(valueSpan) && valueSpan > 0) {
    if (valueSpan <= 2.5) return n.toFixed(2)
    if (valueSpan <= 120) return n.toFixed(1)
  }
  if (Math.abs(n) >= 1_000_000) return `${(n / 1_000_000).toFixed(1)}M`
  if (Math.abs(n) >= 1000) return `${Math.round(n / 1000)}K`
  return String(Math.round(n))
}

/** Tables / tooltips: ``26 min``, ``1 hr 5 min``. */
export function formatMinutesDisplay(v: number): string {
  if (!Number.isFinite(v)) return 'β€”'
  const sign = v < 0 ? '-' : ''
  const whole = Math.round(Math.abs(v))
  if (whole < 60) return `${sign}${whole} min`
  const h = Math.floor(whole / 60)
  const m = whole % 60
  if (m === 0) return `${sign}${h} hr`
  return `${sign}${h} hr ${m} min`
}

/** Bar ends: ``26m``, ``1h5m``. */
export function formatMinutesCompactDisplay(v: number): string {
  if (!Number.isFinite(v)) return 'β€”'
  const sign = v < 0 ? '-' : ''
  const whole = Math.round(Math.abs(v))
  if (whole < 60) return `${sign}${whole}m`
  const h = Math.floor(whole / 60)
  const m = whole % 60
  return m === 0 ? `${sign}${h}h` : `${sign}${h}h${m}m`
}

/** X-axis ticks when the metric is a duration in minutes (never ``250k``-style magnitudes). */
export function formatCensusMapAxisTickMinutes(x: number, valueSpan?: number): string {
  const n = Number(x)
  if (!Number.isFinite(n)) return ''
  const span = valueSpan != null && Number.isFinite(valueSpan) && valueSpan > 0 ? valueSpan : 30
  if (span <= 4) return `${stripFracZeros(Math.abs(n).toFixed(1))}m`
  const r = Math.round(n)
  if (Math.abs(r) >= 60 && span >= 40) {
    const h = r / 60
    if (Math.abs(h - Math.round(h)) < 0.11) return `${Math.round(h)}h`
    return `${stripFracZeros(h.toFixed(1))}h`
  }
  return `${r}m`
}

export function formatCensusMapAxisTickForMetric(
  slug: string,
  metrics: CensusMetricFormatRow[],
  x: number,
  valueSpan?: number,
  valueMode: CensusValueMode = 'raw',
): string {
  const m = metrics.find((r) => r.slug === slug)
  if (m?.format === 'minutes' || slug === 'travel_time_to_work_minutes') {
    return formatCensusMapAxisTickMinutes(x, valueSpan)
  }
  if (valueMode === 'yoy' || valueMode === 'vs_natl') {
    const n = Number(x)
    if (!Number.isFinite(n)) return ''
    const span =
      valueSpan != null && Number.isFinite(valueSpan) && valueSpan > 0 ? valueSpan : 20
    const a = Math.abs(n)
    const decimals = span <= 3 ? 1 : a >= 10 ? 0 : 1
    return `${n.toFixed(decimals)}%`
  }
  return formatCensusMapAxisTick(x, valueSpan)
}

/** Minimal metric row for compact bar-end labels. */
export type CensusMetricFormatRow = { slug: string; format: string }

function stripFracZeros(s: string): string {
  return s.replace(/(\.\d*?)0+$/, '$1').replace(/\.$/, '')
}

/** Optional display tuning for dense tables (e.g. Data Explorer scorecard). */
export type FormatMetricValueDisplayOptions = {
  /**
   * When true, use fixed fractional width for currency/count K/M/B, percents, and years
   * so columns align (e.g. ``$64.12K`` vs ``$62.03K``, ``3.20%``).
   */
  uniformTableDecimals?: boolean
}

/** ``$`` / count style compact: 1.2k, 3.4M, 12 (no unit suffix for counts). */
function formatCompactMagnitude(abs: number, withDollar: boolean, uniformTable = false): string {
  const p = withDollar ? '$' : ''
  if (!(abs > 0) || !Number.isFinite(abs)) return `${p}0`
  if (uniformTable) {
    if (abs >= 1e9) return `${p}${(abs / 1e9).toFixed(2)}B`
    if (abs >= 1e6) return `${p}${(abs / 1e6).toFixed(2)}M`
    if (abs >= 1000) return `${p}${(abs / 1000).toFixed(2)}K`
    return withDollar ? `${p}${abs.toFixed(2)}` : `${p}${abs.toFixed(0)}`
  }
  if (abs >= 1e9) return `${p}${stripFracZeros((abs / 1e9).toFixed(2))}B`
  if (abs >= 1e6) return `${p}${stripFracZeros((abs / 1e6).toFixed(2))}M`
  if (abs >= 100_000) return `${p}${Math.round(abs / 1000)}K`
  if (abs >= 1000) return `${p}${stripFracZeros((abs / 1000).toFixed(2))}K`
  if (abs >= 100) return `${p}${Math.round(abs)}`
  if (abs >= 10) return `${p}${stripFracZeros(abs.toFixed(1))}`
  if (abs >= 1) return `${p}${stripFracZeros(abs.toFixed(1))}`
  return `${p}${stripFracZeros(abs.toFixed(2))}`
}

/**
 * Short labels for race bar ends: ``$72k``, ``1.2M``, ``4.2%``, ``0.48`` (Gini), ``37.2y``.
 * For ``yoy`` / ``vs_natl`` uses percent with sensible precision.
 */
export function formatMetricValueCompact(
  slug: string,
  v: number,
  metrics: CensusMetricFormatRow[],
  valueMode: 'raw' | 'yoy' | 'vs_natl',
): string {
  if (!Number.isFinite(v)) return 'β€”'
  if (valueMode === 'yoy' || valueMode === 'vs_natl') {
    const a = Math.abs(v)
    const decimals = a >= 10 ? 0 : 1
    return `${v.toFixed(decimals)}%`
  }
  const m = metrics.find((x) => x.slug === slug)
  const f = m?.format ?? ''
  const sign = v < 0 ? '-' : ''
  const av = Math.abs(v)
  if (f === 'minutes' || slug === 'travel_time_to_work_minutes') {
    return formatMinutesCompactDisplay(v)
  }
  if (f === 'currency') return `${sign}${formatCompactMagnitude(av, true)}`
  if (f === 'count') return `${sign}${formatCompactMagnitude(av, false)}`
  if (f === 'percent') return `${stripFracZeros(v.toFixed(1))}%`
  if (f === 'ratio') {
    const base = stripFracZeros(v.toFixed(3))
    return slug === 'gini_income_inequality' && valueMode === 'raw' ? `${base}${giniLetterSuffix(v)}` : base
  }
  if (f === 'years') return `${stripFracZeros(v.toFixed(1))}y`
  return `${sign}${formatCompactMagnitude(av, false)}`
}

/**
 * Human-readable values for tooltips, tables, and bar-chart main figures: ``$39.3M``, ``39287K``,
 * percents for YoY / vs-national modes, and full words for minutes.
 *
 * Pass ``{ uniformTableDecimals: true }`` from scorecard-style tables so K/M/B and % line up.
 */
export function formatMetricValueDisplay(
  slug: string,
  v: number | null | undefined,
  metrics: CensusMetricFormatRow[],
  valueMode: CensusValueMode = 'raw',
  options?: FormatMetricValueDisplayOptions,
): string {
  if (v == null || !Number.isFinite(v)) return 'β€”'
  const u = options?.uniformTableDecimals ?? false
  if (valueMode === 'yoy' || valueMode === 'vs_natl') {
    const a = Math.abs(v)
    return `${v.toFixed(u ? 2 : a >= 10 ? 0 : 1)}%`
  }
  const m = metrics.find((x) => x.slug === slug)
  const f = m?.format ?? ''
  if (f === 'minutes' || slug === 'travel_time_to_work_minutes') {
    return formatMinutesDisplay(v)
  }
  if (f === 'currency') {
    const sign = v < 0 ? '-' : ''
    return `${sign}${formatCompactMagnitude(Math.abs(v), true, u)}`
  }
  if (f === 'count') {
    const sign = v < 0 ? '-' : ''
    return `${sign}${formatCompactMagnitude(Math.abs(v), false, u)}`
  }
  if (f === 'percent') return `${v.toFixed(u ? 2 : 1)}%`
  if (f === 'ratio') return v.toFixed(3)
  if (f === 'years') return `${v.toFixed(u ? 2 : 1)} yrs`
  const sign = v < 0 ? '-' : ''
  return `${sign}${formatCompactMagnitude(Math.abs(v), false, u)}`
}

/**
 * Connecticut legacy counties (09001-09015) vs planning regions (091xx).
 *
 * Since the 2022 ACS vintage Connecticut reports by planning region, so the export emits each
 * legacy county with null metrics and a `data_note`. We never convert planning-region values into
 * county figures (planning regions do not nest in counties and medians/rates cannot be averaged).
 * Only the real FIPS to name mapping is kept here.
 */
export const CT_LEGACY_COUNTY_NAMES: Record<string, string> = {
  '09001': 'Fairfield County',
  '09003': 'Hartford County',
  '09005': 'Litchfield County',
  '09007': 'Middlesex County',
  '09009': 'New Haven County',
  '09011': 'New London County',
  '09013': 'Tolland County',
  '09015': 'Windham County',
}

/** Tooltip / legend text for a county the source reports only by planning region. */
export const CT_PLANNING_REGION_UNAVAILABLE_TEXT =
  'Not available: Connecticut reports by planning region since 2022'

/** True when a county row (or GeoJSON properties) carries the export's planning-region `data_note`. */
export function hasPlanningRegionNote(props: Record<string, unknown> | null | undefined): boolean {
  const note = props?.data_note
  return typeof note === 'string' && note.trim().length > 0
}

/**
 * True when a Connecticut county should explain its missing figure: the export marked it with the
 * planning-region note, or it is a legacy county (09001-09015) in data exported before that note
 * existed. Callers check that the value is missing first; this never supplies a number.
 */
export function isConnecticutPlanningRegionGap(
  geoid: string,
  props: Record<string, unknown> | null | undefined,
): boolean {
  if (!geoid.startsWith('09')) return false
  return hasPlanningRegionNote(props) || Object.prototype.hasOwnProperty.call(CT_LEGACY_COUNTY_NAMES, geoid)
}