public record SvgFigure(string Content);
Formatter.Register<SvgFigure>((v,w)=>w.Write(v.Content),"text/html");
public static class View {
static string F(double x)=>x.ToString("0.###",CultureInfo.InvariantCulture);
static string Esc(string s)=>System.Security.SecurityElement.Escape(s);
public static double Q(double[] a,double q) {var s=a.OrderBy(x=>x).ToArray();double k=(s.Length-1)*q;int i=(int)k;return s[i]+(s[Math.Min(i+1,s.Length-1)]-s[i])*(k-i);}
public static (double mean,double lo,double hi,double[] draws) Boot(double[] a,int n,int seed) {var rng=new Random(seed);var b=new double[n];for(int i=0;i<n;i++){double sum=0;for(int j=0;j<a.Length;j++)sum+=a[rng.Next(a.Length)];b[i]=sum/a.Length;}return(a.Average(),Q(b,.025),Q(b,.975),b);}
static void Text(StringBuilder s,double x,double y,string t,int size=12) => s.Append($"<text x='{F(x)}' y='{F(y)}' text-anchor='middle' font-family='sans-serif' font-size='{size}'>{Esc(t)}</text>");
static void Line(StringBuilder s,double x,double y,double x2,double y2,string color="black")=>s.Append($"<line x1='{F(x)}' y1='{F(y)}' x2='{F(x2)}' y2='{F(y2)}' stroke='{color}'/>");
static StringBuilder Start(int w=900,int h=400)=>new StringBuilder($"<svg xmlns='http://www.w3.org/2000/svg' width='{w}' height='{h}' viewBox='0 0 {w} {h}'><rect width='{w}' height='{h}' fill='white'/>");
public static SvgFigure Hist(double[] data,int bins,string title,string xlabel) {var s=Start();double left=90,top=55,w=740,h=270,min=data.Min(),max=data.Max();if(max==min)max=min+1;var counts=new int[bins];foreach(double v in data)counts[Math.Min(bins-1,(int)((v-min)/(max-min)*bins))]++;int ymax=Math.Max(1,counts.Max());Text(s,450,25,title,16);Line(s,left,top,left,top+h);Line(s,left,top+h,left+w,top+h);for(int k=0;k<=4;k++){double y=top+h-k*h/4;Line(s,left-5,y,left+w,y,"#dddddd");Text(s,left-35,y+4,F(ymax*k/4.0));double x=left+k*w/4;Text(s,x,top+h+18,F(min+(max-min)*k/4));}for(int i=0;i<bins;i++){double bw=w/bins;double bh=h*counts[i]/ymax;s.Append($"<rect x='{F(left+i*bw)}' y='{F(top+h-bh)}' width='{F(Math.Max(1,bw-1))}' height='{F(bh)}' fill='#77aadd'/>");}double mean=data.Average();double mx=left+(mean-min)/(max-min)*w;Line(s,mx,top,mx,top+h,"#c0392b");Text(s,700,43,"Trait rouge : moyenne (ms)",11);Text(s,450,380,xlabel);Text(s,30,40,"Effectif");s.Append("</svg>");return new SvgFigure(s.ToString());}
public static SvgFigure Boxes(double[][] values,string[] labels,string title) {var s=Start();double left=90,top=55,w=740,h=270,max=values.SelectMany(x=>x).Max();if(max<=0)max=1;Text(s,450,25,title,16);Line(s,left,top,left,top+h);Line(s,left,top+h,left+w,top+h);for(int k=0;k<=4;k++){double y=top+h-k*h/4;Line(s,left-5,y,left+w,y,"#dddddd");Text(s,left-35,y+4,F(max*k/4));}for(int i=0;i<values.Length;i++){var a=values[i];double x=left+(i+.5)*w/values.Length;double Y(double v)=>top+h-v/max*h;double q1=Q(a,.25),med=Q(a,.5),q3=Q(a,.75),iqr=q3-q1;double low=a.Where(v=>v>=q1-1.5*iqr).Min(),high=a.Where(v=>v<=q3+1.5*iqr).Max();Line(s,x,Y(low),x,Y(high));Line(s,x-8,Y(low),x+8,Y(low));Line(s,x-8,Y(high),x+8,Y(high));s.Append($"<rect x='{F(x-18)}' y='{F(Y(q3))}' width='36' height='{F(Math.Max(.5,Y(q1)-Y(q3)))}' fill='#9ecae1' stroke='black'/>");Line(s,x-18,Y(med),x+18,Y(med));foreach(double v in a.Where(v=>v<low || v>high))s.Append($"<circle cx='{F(x)}' cy='{F(Y(v))}' r='3' fill='white' stroke='black'/>");double ym=Y(a.Average());s.Append($"<polygon points='{F(x)},{F(ym-5)} {F(x+5)},{F(ym)} {F(x)},{F(ym+5)} {F(x-5)},{F(ym)}' fill='#c0392b'/>");Text(s,x,top+h+20,labels[i]);}Text(s,30,40,"ms");Text(s,700,43,"Losange : moyenne ; cercle : valeur hors moustaches",10);Text(s,450,380,"Puzzle / solveur ; moustaches à 1,5 IQR");s.Append("</svg>");return new SvgFigure(s.ToString());}
public static SvgFigure Bootstrap(double[] data,double[] sizes,double[] widths,double mean) {var s=Start();double left=65,top=60,w=340,h=260,min=data.Min(),max=data.Max();if(max==min)max=min+1;int bins=50;var counts=new int[bins];foreach(double v in data)counts[Math.Min(bins-1,(int)((v-min)/(max-min)*bins))]++;int ymax=Math.Max(1,counts.Max());Text(s,230,25,"Moyennes du pool rééchantillonné",15);Text(s,670,25,"Largeur percentile selon taille",15);Line(s,left,top,left,top+h);Line(s,left,top+h,left+w,top+h);for(int k=0;k<=4;k++){double y=top+h-k*h/4;Text(s,left-25,y+4,F(ymax*k/4.0));Text(s,left+k*w/4,top+h+18,F(min+(max-min)*k/4));}for(int i=0;i<bins;i++){double bw=w/bins,bh=h*counts[i]/ymax;s.Append($"<rect x='{F(left+i*bw)}' y='{F(top+h-bh)}' width='{F(Math.Max(1,bw-1))}' height='{F(bh)}' fill='#74bb8e'/>");}foreach(double v in new[]{Q(data,.025),mean,Q(data,.975)}) {double x=left+(v-min)/(max-min)*w;Line(s,x,top,x,top+h,v==mean?"black":"#c0392b");}Text(s,235,44,"Noir : moyenne ; rouge : percentiles 2,5 / 97,5",10);Text(s,230,375,"Moyenne (ms) ; effectif en ordonnée");left=520;Line(s,left,top,left,top+h);Line(s,left,top+h,left+w,top+h);double maxw=Math.Max(widths.Max(),.000001);for(int k=0;k<=4;k++)Text(s,left-27,top+h-k*h/4+4,F(maxw*k/4));for(int i=0;i<sizes.Length;i++){double x=left+(sizes[i]-sizes[0])/(sizes[sizes.Length-1]-sizes[0])*w,y=top+h-widths[i]/maxw*h;if(i>0){double px=left+(sizes[i-1]-sizes[0])/(sizes[sizes.Length-1]-sizes[0])*w,py=top+h-widths[i-1]/maxw*h;Line(s,px,py,x,y,"#7044aa");}s.Append($"<circle cx='{F(x)}' cy='{F(y)}' r='4' fill='#7044aa'/>");Text(s,x,top+h+18,F(sizes[i]));}Text(s,690,375,"Nombre de chronos ; largeur (ms)");s.Append("</svg>");return new SvgFigure(s.ToString());}
}
Console.WriteLine("Bootstrap percentile et figures SVG chargés.");