All files / viewer/src/react TableCharts.tsx

100% Statements 85/85
100% Branches 45/45
100% Functions 36/36
100% Lines 73/73

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415                                      5x 1054x 12x 20x 5x 1008x 1008x                       20x 20x                                                           3x 3x                                                                     23x 1460x                     23x                     141x   161x 161x       1135x 1135x                                                                                 6x 1x 5x 23x 5x 5x                                     10x           5x   6x   5x 5x 5x 29x 5x 5x 5x 5x 5x                 3x                       23x 23x                                       23x 23x   92x                                                               9x 1x 8x 64x 72x 8x 8x 8x 8x 8x 8x   8x     9x     9x                               54x                                       72x                                                       5x 1x 4x 4x 4x 4x 4x 4x 4x 4x                         4x 12x 12x 12x                        
import { type CSSProperties, type ReactNode } from 'react';
import {
  type IESProfile,
  buildConeDiagram,
  buildIntensityTable,
  buildLuminanceTable,
  formatGraphValue,
  type ConeOptions,
  type IntensityTableOptions,
  type LuminanceTableOptions,
} from '@ies-tools/core';
import { graphFrame, heatColor } from '../graphs.js';
 
interface ChartStyleProps {
  /** Viewbox width / height; the background fills it and the chart is centered. Defaults to the native layout. */
  aspectRatio?: number;
  className?: string;
  style?: CSSProperties;
}
const muted = '#b7c1ce';
const format = (value: number | null) => (value === null ? '—' : formatGraphValue(value));
const offset = (distance: number, angle: number) => distance * Math.tan((angle * Math.PI) / 180);
const angleLabel = (angle: number | null) => (angle === null ? '—' : `${formatGraphValue(angle)}°`);
const fractions = (values: (number | null)[][]) => {
  const max = Math.max(0, ...values.flat().map((v) => v ?? 0));
  return values.map((row) => row.map((v) => (max > 0 && v !== null ? v / max : 0)));
};
 
function Frame({
  width,
  height,
  label,
  aspectRatio,
  className,
  style,
  children,
}: ChartStyleProps & { width: number; height: number; label: string; children: ReactNode }) {
  const frame = graphFrame(width, height, aspectRatio);
  return (
    <svg
      xmlns="http://www.w3.org/2000/svg"
      width={frame.width}
      height={frame.height}
      className={className}
      style={{
        width: '100%',
        height: 'auto',
        fontFamily: 'sans-serif',
        maxWidth: Math.max(620, width),
        background: '#111',
        color: '#ddd',
        ...style,
      }}
      viewBox={`${frame.x} ${frame.y} ${frame.width} ${frame.height}`}
      // SVG contains the chart itself, with a textual image description.
      // oxlint-disable-next-line jsx-a11y/prefer-tag-over-role
      role="img"
      aria-label={label}
    >
      <title>{label}</title>
      <rect x={frame.x} y={frame.y} width={frame.width} height={frame.height} fill="#111" />
      {children}
    </svg>
  );
}
 
/** Non-Type C files get a readable SVG message rather than an exception. */
function TypeCOnly({ name, ...props }: ChartStyleProps & { name: string }) {
  const label = `${name} is available for Type C photometry only`;
  return (
    <Frame width={520} height={120} label={label} {...props}>
      <text x="24" y="64" fill="currentColor" fontSize="14">
        {label}.
      </text>
    </Frame>
  );
}
 
function Table({
  x,
  y,
  corner,
  columns,
  rows,
  cells,
  tint,
  cellWidth,
  headerWidth = cellWidth,
  cellHeight = 18,
  fontSize = 10,
}: {
  x: number;
  y: number;
  corner: string;
  columns: string[];
  rows: string[];
  cells: string[][];
  /** Value fractions in [0, 1]; alpha follows sqrt(fraction) for a subtle tint. */
  tint?: number[][];
  cellWidth: number;
  headerWidth?: number;
  cellHeight?: number;
  fontSize?: number;
}) {
  const text = (tx: number, ty: number, value: string, fill = 'currentColor', key?: string) => (
    <text
      key={key}
      x={tx}
      y={ty + cellHeight / 2 + fontSize * 0.35}
      textAnchor="middle"
      fill={fill}
      fontSize={fontSize}
    >
      {value}
    </text>
  );
  return (
    <g>
      <rect
        x={x}
        y={y}
        width={headerWidth + columns.length * cellWidth}
        height={(rows.length + 1) * cellHeight}
        fill="#161c26"
        stroke="#334155"
      />
      {text(x + headerWidth / 2, y, corner, muted)}
      {columns.map((column, j) => text(x + headerWidth + (j + 0.5) * cellWidth, y, column, muted, `c${j}`))}
      {rows.map((row, i) => {
        const top = y + (i + 1) * cellHeight;
        return (
          <g key={i}>
            {text(x + headerWidth / 2, top, row, muted)}
            {cells[i]!.map((cell, j) => {
              const fraction = tint?.[i]?.[j] ?? 0;
              return (
                <g key={j}>
                  {fraction > 0 && (
                    <rect
                      x={x + headerWidth + j * cellWidth}
                      y={top}
                      width={cellWidth}
                      height={cellHeight}
                      fill={heatColor(fraction)}
                      fillOpacity={0.6 * Math.sqrt(fraction)}
                    />
                  )}
                  {text(x + headerWidth + (j + 0.5) * cellWidth, top, cell)}
                </g>
              );
            })}
          </g>
        );
      })}
      <path
        d={`M${x} ${y + cellHeight}h${headerWidth + columns.length * cellWidth}M${x + headerWidth} ${y}v${(rows.length + 1) * cellHeight}`}
        stroke="#334155"
      />
    </g>
  );
}
 
/** Props for {@link ConeDiagramGraph}. */
export interface ConeDiagramGraphProps extends ConeOptions, ChartStyleProps {
  /** Type C profile to evaluate. */
  profile: IESProfile;
}
/**
 * SVG side-view cone diagram: centre illuminance and beam/field diameters at each distance, plus a table.
 *
 * @example
 * ```tsx
 * <ConeDiagramGraph profile={profile} maxDistance={6} />
 * ```
 */
export function ConeDiagramGraph({ profile, aspectRatio, className, style, ...options }: ConeDiagramGraphProps) {
  if (profile.photometricType !== 1)
    return <TypeCOnly name="Cone diagram" aspectRatio={aspectRatio} className={className} style={style} />;
  const cone = buildConeDiagram(profile, options);
  const deepest = Math.max(...cone.rows.map((row) => row.distance));
  const [c0] = cone.planes;
  const crossings = [
    {
      name: 'Field cone',
      tone: 0.35,
      opacity: 0.3,
      left: c0.beam.fieldLeftDegrees,
      right: c0.beam.fieldRightDegrees,
      status: c0.beam.fieldAngleStatus,
    },
    {
      name: 'Beam cone',
      tone: 0.85,
      opacity: 0.45,
      left: c0.beam.beamLeftDegrees,
      right: c0.beam.beamRightDegrees,
      status: c0.beam.beamAngleStatus,
    },
  ].filter(
    (cut): cut is typeof cut & { left: number; right: number } =>
      cut.status === 'ok' &&
      cut.left !== undefined &&
      cut.right !== undefined &&
      Math.abs(cut.left) < 90 &&
      Math.abs(cut.right) < 90,
  );
  const widest = Math.max(
    1e-9,
    ...crossings.flatMap((cut) => [cut.left, cut.right]).map((angle) => Math.abs(offset(deepest, angle))),
  );
  const apex = { x: 260, y: 76 };
  const depthScale = 220 / deepest;
  const scale = Math.min(depthScale, 140 / widest);
  const screenY = (distance: number) => apex.y + distance * depthScale;
  const tableTop = 330,
    rowHeight = 20,
    height = tableTop + (cone.rows.length + 1) * rowHeight + 46;
  const title = `Cone diagram, centre intensity ${formatGraphValue(cone.centerIntensity)} candela, distances up to ${formatGraphValue(deepest)} metres`;
  return (
    <Frame width={520} height={height} label={title} aspectRatio={aspectRatio} className={className} style={style}>
      <text x="24" y="28" fill="currentColor" fontSize="14">
        Cone diagram · C0–C180 side view
      </text>
      <text x="24" y="48" fill={muted} fontSize="11">
        I(γ 0°) {formatGraphValue(cone.centerIntensity)} cd · E = I / d² · beam 50 % · field 10 %
      </text>
      {crossings.map((cut) => (
        <polygon
          key={cut.name}
          points={`${apex.x},${apex.y} ${apex.x + offset(deepest, cut.left) * scale},${screenY(deepest)} ${apex.x + offset(deepest, cut.right) * scale},${screenY(deepest)}`}
          fill={heatColor(cut.tone)}
          fillOpacity={cut.opacity}
          stroke={heatColor(cut.tone)}
        >
          <title>{cut.name}</title>
        </polygon>
      ))}
      <circle cx={apex.x} cy={apex.y} r="5" fill="#fde68a" />
      {cone.rows.map((row) => {
        const y = screenY(row.distance);
        return (
          <g key={row.distance}>
            <path d={`M100 ${y}H420`} stroke="#64748b" strokeDasharray="3 3" />
            <text x="92" y={y + 4} textAnchor="end" fill="currentColor" fontSize="11">
              {formatGraphValue(row.distance)} m
            </text>
            <text x="428" y={y - 2} fill="currentColor" fontSize="10">
              {formatGraphValue(row.illuminance)} lx
            </text>
            <text x="428" y={y + 10} fill={muted} fontSize="9">
              Ø {format(row.beamDiameters[0])} / {format(row.fieldDiameters[0])} m
            </text>
          </g>
        );
      })}
      <Table
        x={24}
        y={tableTop}
        corner="Distance"
        columns={['E centre', 'Beam Ø C0', 'Beam Ø C90', 'Field Ø C0', 'Field Ø C90']}
        rows={cone.rows.map((row) => `${formatGraphValue(row.distance)} m`)}
        cells={cone.rows.map((row) => [
          `${formatGraphValue(row.illuminance)} lx`,
          ...[...row.beamDiameters, ...row.fieldDiameters].map((d) => (d === null ? '—' : `${formatGraphValue(d)} m`)),
        ])}
        cellWidth={79}
        headerWidth={77}
        cellHeight={rowHeight}
      />
      <text x="24" y={height - 26} fill={muted} fontSize="10">
        Beam C0–C180 {angleLabel(c0.beam.beamAngleDegrees)} · C90–C270{' '}
        {angleLabel(cone.planes[1].beam.beamAngleDegrees)} · Field C0–C180 {angleLabel(c0.beam.fieldAngleDegrees)} ·
        C90–C270 {angleLabel(cone.planes[1].beam.fieldAngleDegrees)}
      </text>
      <text x="24" y={height - 10} fill="#9ca3af" fontSize="10">
        Diameters use per-side 50 %/10 % crossings of the peak · — means no crossing below 90°
      </text>
    </Frame>
  );
}
 
/** Props for {@link LuminanceTableGraph}. */
export interface LuminanceTableGraphProps extends LuminanceTableOptions, ChartStyleProps {
  /** Type C profile to evaluate. */
  profile: IESProfile;
}
/**
 * SVG glare-angle tables: average luminance L(C, γ) in cd/m² over the luminous opening, and intensity I(C, γ).
 *
 * @example
 * ```tsx
 * <LuminanceTableGraph profile={profile} />
 * ```
 */
export function LuminanceTableGraph({ profile, aspectRatio, className, style, ...options }: LuminanceTableGraphProps) {
  if (profile.photometricType !== 1)
    return <TypeCOnly name="Luminance table" aspectRatio={aspectRatio} className={className} style={style} />;
  const table = buildLuminanceTable(profile, options);
  const columns = table.cPlanes.map((c) => `C${formatGraphValue(c)}`);
  const rows = table.gammas.map((g) => `γ ${formatGraphValue(g)}°`);
  const cellWidth = Math.min(80, 424 / columns.length),
    cellHeight = 18;
  const tableHeight = (rows.length + 1) * cellHeight;
  const secondTop = 96 + tableHeight;
  const height = secondTop + 26 + tableHeight + 44;
  const { opening } = table;
  const size =
    opening.shape === 'circular'
      ? `Ø ${formatGraphValue(opening.width)} m`
      : `${formatGraphValue(opening.width)} × ${formatGraphValue(opening.length)} m`;
  const title = table.luminance
    ? `Glare-angle luminance table in candela per square metre, luminous opening ${size}`
    : 'Glare-angle intensity table; luminance is unavailable because the luminous area is zero';
  return (
    <Frame width={520} height={height} label={title} aspectRatio={aspectRatio} className={className} style={style}>
      <text x="24" y="28" fill="currentColor" fontSize="14">
        Average luminance · cd/m²
      </text>
      <text x="24" y="48" fill={muted} fontSize="11">
        L = I / (A_bottom·cos γ + A_side·sin γ) · opening {size}
        {opening.height > 0 ? ` × ${formatGraphValue(opening.height)} m high` : ''}
      </text>
      {table.luminance ? (
        <Table
          x={24}
          y={64}
          corner=""
          columns={columns}
          rows={rows}
          cells={table.luminance.map((row) => row.map(format))}
          tint={fractions(table.luminance)}
          cellWidth={cellWidth}
          headerWidth={48}
          cellHeight={cellHeight}
        />
      ) : (
        <text x="24" y="90" fill="#fca5a5" fontSize="12" role="alert">
          Luminance unavailable: the file declares a zero luminous area (width, length and height are 0).
        </text>
      )}
      <text x="24" y={secondTop + 8} fill="currentColor" fontSize="14">
        Luminous intensity · cd
      </text>
      <Table
        x={24}
        y={secondTop + 18}
        corner=""
        columns={columns}
        rows={rows}
        cells={table.intensity.map((row) => row.map(format))}
        tint={fractions(table.intensity)}
        cellWidth={cellWidth}
        headerWidth={48}
        cellHeight={cellHeight}
      />
      <text x="24" y={height - 12} fill="#9ca3af" fontSize="10">
        Average over the declared luminous opening · A_side follows the opening silhouette seen from each C plane
      </text>
    </Frame>
  );
}
 
/** Props for {@link IntensityTableGraph}. */
export interface IntensityTableGraphProps extends IntensityTableOptions, ChartStyleProps {
  /** Type C profile to tabulate. */
  profile: IESProfile;
}
/**
 * SVG table of the measured candela grid: γ rows, C columns, with a subtle heat-map tint. Large grids widen the
 * SVG (cells keep a readable minimum width); use `maxColumns`/`maxRows` to thin a grid for a fixed-size image.
 *
 * @example
 * ```tsx
 * <IntensityTableGraph profile={profile} unit="cd/klm" />
 * ```
 */
export function IntensityTableGraph({ profile, aspectRatio, className, style, ...options }: IntensityTableGraphProps) {
  if (profile.photometricType !== 1)
    return <TypeCOnly name="Intensity table" aspectRatio={aspectRatio} className={className} style={style} />;
  const table = buildIntensityTable(profile, options);
  const headerWidth = 48,
    cellWidth = Math.max(40, Math.min(80, 424 / table.cPlanes.length)),
    cellHeight = 16;
  const width = Math.max(520, 48 + headerWidth + table.cPlanes.length * cellWidth);
  const height = 72 + (table.gammas.length + 1) * cellHeight + 34;
  const title = `Luminous intensity table in ${table.unit}, ${table.gammas.length} vertical by ${table.cPlanes.length} horizontal angles, maximum ${formatGraphValue(table.maximum)}`;
  return (
    <Frame width={width} height={height} label={title} aspectRatio={aspectRatio} className={className} style={style}>
      <text x="24" y="28" fill="currentColor" fontSize="14">
        Luminous intensity table · {table.unit}
      </text>
      <text x="24" y="48" fill={muted} fontSize="11">
        Rows γ (vertical) · columns C (horizontal) · shown peak {formatGraphValue(table.maximum)} {table.unit}
        {options.unit === 'cd/klm' && table.unit === 'cd' ? ' · no declared lamp flux, shown in cd' : ''}
      </text>
      <Table
        x={24}
        y={62}
        corner="γ \ C"
        columns={table.cPlanes.map((c) => `${formatGraphValue(c)}°`)}
        rows={table.gammas.map((g) => `${formatGraphValue(g)}°`)}
        cells={table.values.map((row) => row.map(formatGraphValue))}
        tint={table.values.map((row) => row.map((v) => (table.maximum > 0 ? v / table.maximum : 0)))}
        cellWidth={cellWidth}
        headerWidth={headerWidth}
        cellHeight={cellHeight}
        fontSize={Math.min(10, cellWidth / 4.4)}
      />
      <text x="24" y={height - 12} fill="#9ca3af" fontSize="10">
        Measured values without interpolation · tint alpha ∝ √(value / peak)
      </text>
    </Frame>
  );
}