/** * BarChart: one series of bars over time, drawn as SVG in the kit's tokens. * * One series, so no legend: the card's title names it. Bars are the accent's * ink step (it holds 3:1 against the card), 2px apart, their top corners * rounded and their feet on the baseline. The grid is three hairlines; the * axis labels the first, middle and last day only. Hovering or focusing a bar * (arrow keys once the chart has focus) shows its value; a table with every * value is there for screen readers. */ import { useLayoutEffect, useMemo, useRef, useState } from '@wordpress/element'; import { __ } from '@wordpress/i18n'; import { cn } from 'cn'; const PAD = { top: 8, right: 4, bottom: 22, left: 32 }; const GAP = 2; const RADIUS = 4; /** * The container's width, following resizes. 600 where the browser cannot * measure (tests). * * @param {Object} ref Element ref. * @return {number} Pixels. */ function useWidth( ref ) { const [ width, setWidth ] = useState( 600 ); useLayoutEffect( () => { const el = ref.current; if ( ! el || typeof window.ResizeObserver !== 'function' ) { return undefined; } const observer = new window.ResizeObserver( ( [ entry ] ) => setWidth( Math.max( 120, Math.floor( entry.contentRect.width ) ) ) ); observer.observe( el ); return () => observer.disconnect(); }, [ ref ] ); return width; } /** * A round top for the scale: 1, 2, 5 or 10 times a power of ten. * * @param {number} max Largest value. * @return {number} The top of the axis. */ function niceTop( max ) { if ( max <= 4 ) { return Math.max( 1, Math.ceil( max ) ); } const power = 10 ** Math.floor( Math.log10( max ) ); const step = [ 1, 2, 5, 10 ].find( ( s ) => s * power >= max ); return step * power; } /** * A bar whose top corners are rounded and whose foot sits on the baseline. * * @param {number} x Left. * @param {number} y Top. * @param {number} w Width. * @param {number} h Height. * @return {string} SVG path. */ function barPath( x, y, w, h ) { const r = Math.min( RADIUS, w / 2, h ); return [ `M${ x },${ y + h }`, `V${ y + r }`, `Q${ x },${ y } ${ x + r },${ y }`, `H${ x + w - r }`, `Q${ x + w },${ y } ${ x + w },${ y + r }`, `V${ y + h }`, 'Z', ].join( ' ' ); } /** * @param {Object} props * @param {Array} props.data `{ key, label, value }`, oldest first. * @param {number} props.height Pixels. * @param {string} props.title What the bars count, for the table and the summary. * @param {Function} props.formatValue Value as text for the tooltip ("3 new subscribers"). * @param {Function} props.formatLabel Bar's label as axis text (a short date). * @param {string} props.empty What to say when every value is 0. * @param {string} props.className More classes. * @return {Element} The chart. */ export function BarChart( { data = [], height = 160, title = '', formatValue = ( v ) => String( v ), formatLabel = ( l ) => String( l ), empty = '', className, } ) { const box = useRef( null ); const width = useWidth( box ); const [ active, setActive ] = useState( null ); const max = useMemo( () => data.reduce( ( m, d ) => Math.max( m, d.value || 0 ), 0 ), [ data ] ); const top = niceTop( max ); const plotW = Math.max( 1, width - PAD.left - PAD.right ); const plotH = Math.max( 1, height - PAD.top - PAD.bottom ); const slot = data.length ? plotW / data.length : plotW; const barW = Math.max( 1, slot - GAP ); const y = ( v ) => PAD.top + plotH - ( v / top ) * plotH; const ticks = [ 0, top / 2, top ].filter( ( t, i, all ) => all.indexOf( t ) === i && Number.isInteger( t ) ); const labelled = data.length ? [ 0, Math.floor( ( data.length - 1 ) / 2 ), data.length - 1 ] : []; const total = data.reduce( ( sum, d ) => sum + ( d.value || 0 ), 0 ); const onKeyDown = ( event ) => { if ( ! data.length ) { return; } const at = active === null ? data.length - 1 : active; if ( event.key === 'ArrowLeft' ) { event.preventDefault(); setActive( Math.max( 0, at - 1 ) ); } else if ( event.key === 'ArrowRight' ) { event.preventDefault(); setActive( Math.min( data.length - 1, at + 1 ) ); } else if ( event.key === 'Escape' ) { setActive( null ); } }; const shown = active !== null ? data[ active ] : null; const tipX = active !== null ? PAD.left + slot * active + slot / 2 : 0; return (
| { __( 'Day', 'mailpuff' ) } | { title } |
|---|---|
| { formatLabel( d.label ) } | { d.value || 0 } |