File size: 5,605 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
import { useMemo, useState } from 'react'
import { createPortal } from 'react-dom'
import { HOUSE_STYLE } from '../../utils/houseTheme'
import { allocateWaffleCells, type WaffleCategory } from '../../utils/waffleGrid'
import { Tooltip, useTooltip } from '../matrix/Tooltip'
import ChartFrame from '../chart/ChartFrame'
import { useChartReveal } from './useChartReveal'
import './storyCharts.css'

interface Props {
  categories: WaffleCategory[]
  title: string
  unitLabel?: string
  /** Optional key that replays the sequential box wave when scope changes. */
  replayKey?: string
}

const fmt = (n: number) => n.toLocaleString('en-US')

/**
 * 10x10 proportion waffle chart (#1400, ported from vizjb12 §4.8A): 100 rounded boxes filled from bottom-left,
 * with sequential entrance wave, interactive category spotlighting (dims others to `--neutral-data`), a bold headline
 * percentage stat readout, and `[data-box]` marks so its 100 boxes participate in cross-screen particle flights.
 */
export default function BoxWaffleChart({ categories, title, unitLabel = 'records', replayKey }: Props) {
  const { cells, shares, total } = useMemo(() => allocateWaffleCells(categories, 10), [categories])
  const { ref: revealRef, revealed, reduced } = useChartReveal<SVGSVGElement>(replayKey ?? `${title}|${total}`)
  const { bind, tip, hide } = useTooltip()
  const [hovered, setHovered] = useState<string | null>(null)
  const [pinned, setPinned] = useState<string | null>(null)

  if (total <= 0 || cells.length === 0) return null

  const activeKey = hovered ?? pinned
  const topShare = shares.reduce((best, s) => (s.count > best.count ? s : best), shares[0]!)
  const lead = (activeKey ? shares.find((s) => s.key === activeKey) : null) ?? topShare
  const leadPct = Math.round(lead.pct)

  const cellSize = 18
  const gap = 3.5
  const step = cellSize + gap
  const gridPx = 10 * step - gap
  const cellsByKey = new Map(shares.map((s) => [s.key, s.cells]))

  const legend = (
      <div className="wf-legend" role="group" aria-label={`Filter or highlight categories for ${title}`}>
        {shares.map((s) => {
          const on = pinned === s.key
          const active = activeKey === s.key
          return (
            <button
              key={s.key}
              type="button"
              className={`wf-leg-btn${on ? ' is-pinned' : ''}${active ? ' is-active' : ''}`}
              aria-pressed={on}
              onMouseEnter={() => setHovered(s.key)}
              onMouseLeave={() => setHovered(null)}
              onFocus={() => setHovered(s.key)}
              onBlur={() => setHovered(null)}
              onClick={() => setPinned((p) => (p === s.key ? null : s.key))}
            >
              <span className="wf-leg-swatch" style={{ background: s.color }} aria-hidden="true" />
              <span>{s.label}</span>
              <span className="wf-leg-n">{fmt(s.count)}</span>
            </button>
          )
        })}
      </div>
  )
  const topPct = Math.round(topShare.pct)
  const frameTitle = `${title}: ${topShare.label} is the largest share, ${topPct}% of ${fmt(total)} ${unitLabel}`

  return (
    <ChartFrame title={frameTitle} legend={legend} className="wf-frame">
    <div className="wf-wrap" aria-label={`${title}: 100-box proportion grid of ${fmt(total)} ${unitLabel}`}>
      <div className="wf-main">
        <div className="wf-stat" aria-live="polite">
          <span className="wf-stat-num">{leadPct}%</span>
          <span className="wf-stat-label">{lead.label}</span>
          <span className="wf-stat-sub">
            {fmt(lead.count)} of {fmt(total)} {unitLabel}
          </span>
        </div>
        <svg
          ref={revealRef}
          className={`wf-svg${revealed ? ' is-revealed' : ''}`}
          viewBox={`0 0 ${gridPx} ${gridPx}`}
          role="img"
          aria-label={`10 by 10 box waffle chart of ${title}. Top share: ${lead.label}, ${leadPct}% (${fmt(lead.count)} of ${fmt(total)} ${unitLabel}).`}
          onPointerLeave={() => {
            setHovered(null)
            hide()
          }}
        >
          {cells.map((c) => {
            const dimmed = activeKey != null && activeKey !== c.categoryKey
            const delay = reduced ? 0 : c.index * 5
            const b = bind({
              cat: c.label,
              val: `${Math.round(c.sharePct)}% (${cellsByKey.get(c.categoryKey) ?? 0} of 100 boxes)`,
              sub: `${fmt(c.count)} of ${fmt(total)} ${unitLabel}`,
            })
            return (
              <rect
                key={c.index}
                data-box=""
                data-group={c.categoryKey}
                className="wf-cell"
                x={(c.col * step).toFixed(2)}
                y={(c.row * step).toFixed(2)}
                width={cellSize}
                height={cellSize}
                rx={3}
                fill={dimmed ? 'var(--neutral-data)' : c.color}
                opacity={dimmed ? 0.35 : 1}
                style={{ transitionDelay: `${delay}ms, 0ms, 0ms` }}
                onPointerEnter={(e) => {
                  setHovered(c.categoryKey)
                  b.onPointerEnter(e)
                }}
                onPointerMove={b.onPointerMove}
                onPointerLeave={b.onPointerLeave}
                onClick={() => setPinned((p) => (p === c.categoryKey ? null : c.categoryKey))}
              />
            )
          })}
        </svg>
      </div>
      {typeof document !== 'undefined' && createPortal(<div className="folio" style={HOUSE_STYLE}><Tooltip tip={tip} /></div>, document.body)}
    </div>
    </ChartFrame>
  )
}