All files / src legendUtils.ts

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

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                                                                          17x   2x                           1x                         1x                           13x                          
/**
 * Minimal helpers — no runtime Plotly dependency.
 * All coordinates are in paper space ([0-1]).
 */
 
export interface FontCfg {
  size?: number;
  color?: string;
}
 
export interface LegendCfg {
  x?: number;
  y?: number;
  xanchor?: 'left' | 'center' | 'right';
  yanchor?: 'top' | 'middle' | 'bottom';
  orientation?: 'v' | 'h';
  font?: FontCfg;
}
 
export interface MarginCfg {
  l?: number;
  r?: number;
  t?: number;
  b?: number;
}
 
/**
 * pos:
 *   'left'   → vertical legend left of plot
 *   'right'  → vertical legend right of plot
 *   'top'    → horizontal legend above plot
 *   'bottom' → horizontal legend below plot
 */
export function getLegendAndMargin(
  pos: 'left' | 'right' | 'top' | 'bottom' = 'left',
  showLegend: boolean = false
): { legend: LegendCfg; margin: MarginCfg } {
  switch (pos) {
    case 'left':
      return {
        legend: {
          x: -0.05,
          y: 0.5,
          xanchor: 'right',
          yanchor: 'middle',
          orientation: 'v',
          font: { size: 9 }
        },
        // Give a bit more room when legend is visible
        margin: { l: showLegend ? 80 : 30 }
      };
 
    case 'right':
      return {
        legend: {
          x: 1,
          y: 0.5,
          xanchor: 'left',
          yanchor: 'middle',
          orientation: 'v',
          font: { size: 9 }
        },
        margin: { r: showLegend ? 120 : 10 }
      };
 
    case 'top':
      return {
        legend: {
          x: 0.5,
          y: 1,
          xanchor: 'center',
          yanchor: 'bottom',
          orientation: 'h',
          font: { size: 9 }
        },
        margin: { t: 20 }
      };
 
    case 'bottom':
    default:
      return {
        legend: {
          x: 0.5,
          y: -0.1,
          xanchor: 'center',
          yanchor: 'top',
          orientation: 'h',
          font: { size: 9 }
        },
        margin: { b: 50 }
      };
  }
}