The problem is that this also creates a filling in the TGraph.
Is there any way to create a box on the TLegend without changing the drawing on the canvas?
PS: I also tried to do fstat->SetFillStyle(0); after the drawing of the legend but this also removes the box from the TLegend
You could add an empty entry and then manually draw a TBox in the position you want (and you’ll have to find out the coordinates), see as a starting guide the second example in the TLegend documentation.
Another option, maybe simpler, is to make a fake function (or histogram) with the line and fill styles as the one you want, but at a position outside the limits of the frame you are plotting, and add this function, instead of the real one, to the legend (you have to Draw it, otherwise the fill doesn’t show on the legend; drawing it outside the frame means it is not visible, but the entry is correctly added to the legend); e.g.
{
auto c1 = new TCanvas("c1","c1",600,500);
gStyle->SetOptStat(0);
auto h1 = new TH1F("h1","TLegend Example",200,-10,10);
h1->FillRandom("gaus",30000);
h1->SetFillColor(kGreen);
h1->SetFillStyle(3003);
h1->Draw();
auto f1=new TF1("f1","x",-20,-20); // outside the range of the histogram shown (-10 to 10)
f1->SetLineColor(kBlue);
f1->SetLineWidth(2);
f1->SetFillColor(5);
f1->SetFillStyle(1001);
f1->Draw("same");
auto legend = new TLegend(0.1,0.7,0.48,0.9);
legend->AddEntry(h1,"Histogram filled with random numbers","f");
legend->AddEntry("f1","Dummy function","fl");
legend->Draw();
}