All files / src/features/splits/utils splitsChartUtils.js

100% Statements 30/30
100% Branches 36/36
100% Functions 10/10
100% Lines 28/28

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 1512x                           21x 18x 18x               17x 2x     20x 15x   15x 15x 15x     15x       15x     20x                   15x 14x   19x                   15x 19x 19x 19x 19x   23x                   15x       9x                                             2x 2x                                                               2x                
const chartColors = {
  control: '#3B82F6',
  holdout: '#10B981',
  treatments: [
    '#F59E0B',
    '#EF4444',
    '#8B5CF6',
    '#EC4899',
    '#06B6D4',
    '#84CC16',
  ],
};
 
export function getTreatmentIds(splitResults) {
  if (!splitResults || splitResults.length === 0) return [];
  const firstResult = splitResults[0];
  return Object.keys(firstResult.treatments || {});
}
 
export function transformToChartData(
  splitResults,
  recipeWeights = null,
  hasHoldout = true,
) {
  if (!splitResults || splitResults.length === 0) {
    return { labels: [], datasets: [] };
  }
 
  const labels = splitResults.map((r) => r.transactionDate);
  const treatmentIds = getTreatmentIds(splitResults);
 
  const control = recipeWeights?.control || {};
  const holdout = recipeWeights?.holdout || {};
  const controlLabel = control.weight !== null && control.weight !== undefined
    ? `Control ${control.weight}%`
    : 'Control';
  const holdoutLabel = holdout.weight !== null && holdout.weight !== undefined
    ? `Holdout ${holdout.weight}%`
    : 'Holdout';
 
  const datasets = [
    {
      label: controlLabel,
      data: splitResults.map((r) => r.controlCount || 0),
      borderColor: chartColors.control,
      backgroundColor: `${chartColors.control}20`,
      tension: 0.3,
      fill: false,
      pointRadius: 4,
      pointHoverRadius: 6,
    },
  ];
 
  if (hasHoldout) {
    datasets.push({
      label: holdoutLabel,
      data: splitResults.map((r) => r.holdoutCount || 0),
      borderColor: chartColors.holdout,
      backgroundColor: `${chartColors.holdout}20`,
      tension: 0.3,
      fill: false,
      pointRadius: 4,
      pointHoverRadius: 6,
    });
  }
 
  treatmentIds.forEach((id, index) => {
    const color = chartColors.treatments[index % chartColors.treatments.length];
    const weight = recipeWeights?.treatments?.[id];
    const label = weight !== undefined ? `Treatment ${weight}% (${id})` : `Treatment ${id}`;
    datasets.push({
      label,
      data: splitResults.map((r) => r.treatments?.[id] || 0),
      borderColor: color,
      backgroundColor: `${color}20`,
      tension: 0.3,
      fill: false,
      pointRadius: 4,
      pointHoverRadius: 6,
    });
  });
 
  return { labels, datasets };
}
 
export function getChartOptions() {
  return {
    responsive: true,
    maintainAspectRatio: false,
    interaction: {
      mode: 'index',
      intersect: false,
    },
    plugins: {
      legend: {
        position: 'bottom',
        labels: {
          usePointStyle: true,
          padding: 20,
          font: {
            size: 12,
          },
        },
      },
      tooltip: {
        mode: 'index',
        intersect: false,
        callbacks: {
          label: (context) => {
            const value = context.raw?.toLocaleString() || '0';
            return `${context.dataset.label}: ${value}`;
          },
        },
      },
    },
    scales: {
      x: {
        display: true,
        title: {
          display: true,
          text: 'Date',
          font: {
            size: 12,
            weight: 'bold',
          },
        },
        grid: {
          display: false,
        },
      },
      y: {
        display: true,
        title: {
          display: true,
          text: 'Count',
          font: {
            size: 12,
            weight: 'bold',
          },
        },
        beginAtZero: true,
        ticks: {
          callback: (value) => value.toLocaleString(),
        },
      },
    },
  };
}
 
export { chartColors };