OpenPanel is an open-source web and product analytics platform, an open-source alternative to Mixpanel with optional self-hosting.
/**
* Shared 3D bar-depth geometry — the single source of truth for the
* perspective math used by BOTH `<Bar perspective>` (which shrinks a
* bar's front-face top) AND the bar-depth surfaces (`<BarDepthBack>`'s side +
* lid). Keeping the formula here means the front face and the 3D lid can never
* drift out of alignment.
*/
/** Hard ceiling on side-face thickness in px. Capped further by the column gap
* so depth never bleeds past the next bar's leading edge. */
export const BAR_DEPTH_MAX_PX = 7;
/** The side parallelogram's back edge lifts by `depth * this ratio`, giving a
* subtle head-on perspective slope. */
export const BAR_DEPTH_PERSPECTIVE_RATIO = 0.45;
/**
* Maximum side-face depth in px for a chart, clamped so depth never spills past
* the gap between bars. `stepWidth` is d3-scaleBand's `step()` (bandwidth +
* gap); `bandWidth` is a single bar's width. Returns 0 for gapless/dense charts.
*/
export function barDepthMaxDepth(stepWidth: number, bandWidth: number): number {
const gap = Math.max(0, stepWidth - bandWidth);
return Math.min(bandWidth * 0.22, Math.max(0, gap - 1), BAR_DEPTH_MAX_PX);
}
/**
* Per-bar side-face depth + perspective rise.
*
* - `absOffset` ∈ [0, 1]: the bar's normalized distance from the chart's
* horizontal center. 0 = dead center (no depth); 1 = chart edge (full depth).
* - `naturalHeight`: the bar's pixel height. Depth is capped by it so a short
* bar's side never reads wider than the bar is tall.
* - `maxDepth`: from `barDepthMaxDepth`.
*/
export function barDepthAndRise(
absOffset: number,
naturalHeight: number,
maxDepth: number
): { depth: number; perspectiveRise: number } {
const offset = Math.min(1, Math.max(0, absOffset));
const cappedMaxDepth = Math.min(maxDepth, Math.max(0, naturalHeight));
const depth = offset * cappedMaxDepth;
return { depth, perspectiveRise: depth * BAR_DEPTH_PERSPECTIVE_RATIO };
}