All files / src legendUtils.ts

100% Statements 12/12
81.81% Branches 9/11
100% Functions 1/1
100% Lines 12/12

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                                                          74x 74x     74x   2x 2x   3x 3x   1x 1x     68x   68x     6x    
/**
 * Legend/margin helpers for the timeline widget.
 * The legend positioning config comes from the SDK; only the timeline-specific
 * margin values live here.
 */
import {
  getPlotlyLegendConfig,
  type PlotlyLegendPosition
} from '@ska-octopus-widget-sdk/widget-sdk';
 
import type { PlotlyLegendConfig } from '@ska-octopus-widget-sdk/widget-sdk';
 
// LegendCfg extends the SDK type with an optional font, consumed by useTimelineLayout.
export type LegendCfg = PlotlyLegendConfig & { font?: { size?: number; color?: string } };
 
export interface MarginCfg {
  l?: number;
  r?: number;
  t?: number;
  b?: number;
}
 
export function getLegendAndMargin(
  pos: PlotlyLegendPosition = 'left',
  showLegend: boolean = false
): {
  legend: ReturnType<typeof getPlotlyLegendConfig> & { font: { size: number } };
  margin: MarginCfg;
} {
  const base = getPlotlyLegendConfig(pos);
  const legend = { ...base, font: { size: 9 } };
 
  let margin: MarginCfg;
  switch (pos) {
    case 'left':
      margin = { l: showLegend ? 80 : 30 };
      break;
    case 'right':
      margin = { r: showLegend ? 120 : 10 };
      break;
    case 'top':
      margin = { t: 20 };
      break;
    case 'bottom':
    default:
      margin = { b: 76 };
      // Timeline places the bottom legend slightly lower than the SDK default (-0.1)
      return { legend: { ...base, y: -0.18, font: { size: 9 } }, margin };
  }
 
  return { legend, margin };
}