Safety Histogram Deep Dive

Why start here

Safety Histogram is a good first migration target because it is clinically useful, structurally simpler than the linked AE displays, and representative of the dependency pattern we need to replace:

The display is a distribution review for lab, vital sign, ECG, or other safety measures with optional filters, grouping, normal-range overlays, bin boundary annotation, normality tests, distribution comparison, small multiples, and click-through listings.

Current code structure

Area Current files Notes
Public entrypoint src/index.js Merges settings, builds Webcharts controls/chart/table, attaches callbacks, returns the Webcharts chart object.
Settings src/configuration/*, settings-schema.json Splits renderer settings from Webcharts settings, then syncs them before render.
Layout src/layout.js, src/styles.js Creates fixed containers for controls, main chart, small multiples, and listing.
Data lifecycle src/callbacks/onInit.js, src/callbacks/onPreprocess.js Cleans data, validates variables, defines measures, calculates domains/statistics/binning.
Rendering lifecycle src/callbacks/onDraw.js, src/callbacks/onResize.js Relies on Webcharts callbacks, then customizes bars, normal ranges, hover areas, listings, and small multiples.
Statistics src/util/stats/* Local implementations for Shapiro-Wilk and Kolmogorov-Smirnov tests.
Example test-page/* Loads CSV from RhoInc data-library and renders the bundled safetyHistogram.js.
Tests test/* Node scripts compare statistical test outputs to R-generated results.

Current dependency risks

Migration target

A modernized renderer should expose a stable API independent of Webcharts:

import { createSafetyHistogram } from '@safetygraphics/safety-histogram';

const histogram = createSafetyHistogram('#container', settings);
histogram.init(data);
histogram.setSettings(nextSettings);
histogram.setData(nextData);
histogram.destroy();

Internally, split into explicit modules:

Chart.js feasibility

Chart.js can handle the core histogram display if we treat bins as categorical labels or a linear x scale with bar geometry. It should be evaluated for:

Potential issue: small multiples and custom normal-range overlays may be cleaner with a thin custom Canvas/SVG layer around Chart.js rather than forcing all behavior into Chart.js configuration.

Proposed implementation steps

Step 1: Baseline branch

Step 2: Preserve current API with seams

Step 3: Extract pure data/statistics layer

Step 4: Prototype Chart.js renderer

Step 5: Compatibility and release path

Review questions for Jeremy

  1. Should the first PR be only baseline maintenance/CI cleanup, or should it include the new wrapper API seam?
  2. Should we preserve the legacy safetyHistogram(element, settings).init(data) call exactly, or allow a clean new API with a compatibility alias?
  3. Should Chart.js be a hard requirement for Safety Histogram, or can we use custom SVG/Canvas for pieces where Chart.js fights the clinical display?
  4. Do you want this package renamed before or after transfer to SafetyGraphics?