import { Props, c, css, useProp } from "atomico"; import { SiteButton } from "../site-button"; import { tokensColor, tokensSize } from "../site-tokens"; const staticNode = () => (
); const staticIcon = () => ( ); interface Data { label: string; hide?: boolean; items: { label: string; value: number; }[]; } function graphDiff({ minimumTolerance, colorRange }: Props) { const [data, setData] = useProp("data"); const totals = data.reduce( (totals, { items, hide }) => hide ? totals : items.map(({ value }, i) => [ Math.min(totals[i]?.[0] || value, value), Math.max(totals[i]?.[1] || value, value), ]), [] ); return ( {data.map(({ label, items, hide }, i) => ( ))}
{label}
{ setData( data.map((data, ci) => ci === i ? { ...data, hide: !hide, } : data ) ); }} > {staticIcon()} Hide
{staticNode()} {items.map(({ label, value }, i) => { const [min, max] = totals[i] || [0, 0]; const percent = hide ? 0 : value / max; const minTolerance = min * minimumTolerance; const [color] = colorRange.find( ([, range]) => minTolerance >= value || range >= percent ) || ["transparent", 0]; return ( ); })} {staticNode()}
{label}
); } graphDiff.props = { minimumTolerance: { type: Number, value: 1.2, }, colorRange: { type: Array, value: (): [string, number][] => [ ["#00FF90", 0.25], ["#FFD500", 0.5], ["#ff721b", 0.75], ["#ff0f0f", 1], ], }, data: { type: Array, value: (): Data[] => [], }, }; graphDiff.styles = [ tokensColor, tokensSize, css` :host { width: 100%; color: var(--color-text); --border: 1px solid rgba(255, 255, 255, 0.35); } .table { width: 100%; border-spacing: 0; } .td-label { font-size: var(--size-3); white-space: nowrap; padding-right: var(--size-5); } .td-title { font-size: var(--size-5); color: var(--color-title); padding: var(--size-2) var(--size-5) var(--size-2) 0px; vertical-align: top; } .td-lines { width: 100%; } .tr-group:not(:last-child) > .td-lines { border-bottom: var(--border); } .td-line { border-left: var(--border); padding-top: var(--padding-top); padding-bottom: var(--padding-bottom); } .td-space { height: 10px; } .line { width: var(--line-width); height: var(--size-1); background: var(--line-color); border-radius: 0px 10px 10px 0px; transition: 1s ease all; } .tr-subgroup { transition: 0.5s ease all; } .table-focus .tr-subgroup:not(.tr-subgroup-focus) { opacity: 0.25; } .button-hide { opacity: 0.5; } `, ]; export const SiteGraphDiff = c(graphDiff);