ROOT » GRAF2D » GRAF » TLegend

class TLegend: public TPave, public TAttText


Legend class

This class displays a legend box (TPaveText) containing several legend entries. Each legend entry is made of a reference to a ROOT object, a text label and an option specifying which graphical attributes (marker/line/fill) should be displayed.

The following example shows how to create a legend. In this example the legend contains a histogram, a function and a graph. The histogram is put in the legend using its reference pointer whereas the graph and the function are added using their names. Note that, because TGraph contructors do not have the TGraph name as parameter, the graph name should be specified using the SetName method.

When an object is added by name, a scan is performed on the list of objects contained in the current pad (gPad) and also in the possible TMultiGraph and THStack present in the pad. If a matching name is found, the coresponding object is added in the legend using its pointer.

output of html602/MACRO_TLegend_1.gif
{
   TCanvas *c1 = new TCanvas("c1","c1",600,500);
   gStyle->SetOptStat(0);

   TH1F *h1 = new TH1F("h1","TLegend Example",200,-10,10);
   h1->FillRandom("gaus",30000);
   h1->SetFillColor(kGreen);
   h1->SetFillStyle(3003);
   h1->Draw();

   TF1 *f1=new TF1("f1","1000*TMath::Abs(sin(x)/x)",-10,10);
   f1->SetLineColor(kBlue);
   f1->SetLineWidth(4);
   f1->Draw("same");

   const Int_t n = 20;
   Double_t x[n], y[n], ex[n], ey[n];
   for (Int_t i=0;i<n;i++) {
      x[i]  = i*0.1;
      y[i]  = 1000*sin(x[i]+0.2);
      x[i]  = 17.8*x[i]-8.9;
      ex[i] = 1.0;
      ey[i] = 10.*i;
   }
   TGraphErrors *gr = new TGraphErrors(n,x,y,ex,ey);
   gr->SetName("gr");
   gr->SetLineColor(kRed);
   gr->SetLineWidth(2);
   gr->SetMarkerStyle(21);
   gr->SetMarkerSize(1.3);
   gr->SetMarkerColor(7);
   gr->Draw("P");

   leg = new TLegend(0.1,0.7,0.48,0.9);
   leg->SetHeader("The Legend Title");
   leg->AddEntry(h1,"Histogram filled with random numbers","f");
   leg->AddEntry("f1","Function abs(#frac{sin(x)}{x})","l");
   leg->AddEntry("gr","Graph with error bars","lep");
   leg->Draw();

   return c1;
}

TLegend inherits from TAttText therefore changing any text attributes (text alignmemt, font, color...) on a legend will changed the text attributes on each line.
In particular it can be interessting to change the text alignement that way. In order to have a base-line vertical alignment instead of a centered one simply do:
   leg->SetTextAlign(13);
leg->SetTextAlign(11); TLegendEntry *le = leg->AddEntry(h1,"Histogram filled with random numbers","f"); le->SetTextColor(kBlue);;
Note that the TPad class has a method to build automatically a legend for all objects in the pad. It is called TPad::BuildLegend().

Each item in the legend is added using the AddEntry method. This method defines the object to be added (by reference or name), the label associated to this object and an option which a combination of:

  • L: draw line associated with TAttLine if obj inherits from TAttLine
  • P: draw polymarker associated with TAttMarker if obj inherits from TAttMarker
  • F: draw a box with fill associated wit TAttFill if obj inherits TAttFill
  • E: draw vertical error bar

As shown in the following example, passing a NULL pointer as first parameter in AddEntry is also valid. This allows to add text or blank lines in a legend.

output of html602/MACRO_TLegend_3.gif
{
   TCanvas *c2 = new TCanvas("c2","c2",500,300);

   TLegend* leg = new TLegend(0.2, 0.2, .8, .8);
   TH1* h = new TH1F("", "", 1, 0, 1);

   leg->AddEntry(h, "Histogram \"h\"", "l");
   leg->AddEntry((TObject*)0, "", "");
   leg->AddEntry((TObject*)0, "Some text", "");
   leg->AddEntry((TObject*)0, "", "");
   leg->AddEntry(h, "Histogram \"h\" again", "l");

   leg->Draw();
   return c2;
}

It is possible to draw the legend entries over several columns using the method SetNColumns() like in the following example.
output of html602/MACRO_TLegend_5.gif
{
   TCanvas *c3 = new TCanvas("c2","c2",500,300);

   TLegend* leg = new TLegend(0.2, 0.2, .8, .8);
   TH1* h = new TH1F("", "", 1, 0, 1);

   leg-> SetNColumns(2);

   leg->AddEntry(h, "Column 1 line 1", "l");
   leg->AddEntry(h, "Column 2 line 1", "l");
   leg->AddEntry(h, "Column 1 line 2", "l");
   leg->AddEntry(h, "Column 2 line 2", "l");

   leg->Draw();
   return c3;
}

Function Members (Methods)

public:
virtual~TLegend()
voidTObject::AbstractMethod(const char* method) const
TLegendEntry*AddEntry(const TObject* obj, const char* label = "", Option_t* option = "lpf")
TLegendEntry*AddEntry(const char* name, const char* label = "", Option_t* option = "lpf")
virtual voidTObject::AppendPad(Option_t* option = "")
virtual voidTObject::Browse(TBrowser* b)
static TClass*Class()
virtual const char*TObject::ClassName() const
virtual voidClear(Option_t* option = "")MENU
virtual TObject*TObject::Clone(const char* newname = "") const
virtual Int_tTObject::Compare(const TObject* obj) const
virtual voidTPave::ConvertNDCtoPad()
virtual voidCopy(TObject& obj) const
virtual voidTObject::Delete(Option_t* option = "")MENU
virtual voidDeleteEntry()MENU
Int_tTAttLine::DistancetoLine(Int_t px, Int_t py, Double_t xp1, Double_t yp1, Double_t xp2, Double_t yp2)
virtual Int_tTPave::DistancetoPrimitive(Int_t px, Int_t py)
virtual voidDraw(Option_t* option = "")
virtual TBox*TBox::DrawBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2)
virtual voidTObject::DrawClass() constMENU
virtual TObject*TObject::DrawClone(Option_t* option = "") constMENU
virtual voidTPave::DrawPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br")
virtual voidTObject::Dump() constMENU
virtual voidEditEntryAttFill()
virtual voidEditEntryAttLine()
virtual voidEditEntryAttMarker()
virtual voidEditEntryAttText()
virtual voidTObject::Error(const char* method, const char* msgfmt) const
virtual voidTObject::Execute(const char* method, const char* params, Int_t* error = 0)
virtual voidTObject::Execute(TMethod* method, TObjArray* params, Int_t* error = 0)
virtual voidTPave::ExecuteEvent(Int_t event, Int_t px, Int_t py)
virtual voidTObject::Fatal(const char* method, const char* msgfmt) const
virtual TObject*TObject::FindObject(const char* name) const
virtual TObject*TObject::FindObject(const TObject* obj) const
virtual Rectangle_tTBox::GetBBox()
virtual TPointTBox::GetBBoxCenter()
Int_tTPave::GetBorderSize() const
Float_tGetColumnSeparation() const
Double_tTPave::GetCornerRadius() const
virtual Option_t*TObject::GetDrawOption() const
static Long_tTObject::GetDtorOnly()
TLegendEntry*GetEntry() const
Float_tGetEntrySeparation() const
virtual Color_tTAttFill::GetFillColor() const
virtual Style_tTAttFill::GetFillStyle() const
virtual const char*GetHeader() const
virtual const char*TObject::GetIconName() const
virtual Color_tTAttLine::GetLineColor() const
virtual Style_tTAttLine::GetLineStyle() const
virtual Width_tTAttLine::GetLineWidth() const
TList*GetListOfPrimitives() const
Float_tGetMargin() const
virtual Option_t*TPave::GetName() const
Int_tGetNColumns() const
Int_tGetNRows() const
virtual char*TObject::GetObjectInfo(Int_t px, Int_t py) const
static Bool_tTObject::GetObjectStat()
virtual Option_t*TPave::GetOption() const
Int_tTPave::GetShadowColor() const
virtual Short_tTAttText::GetTextAlign() const
virtual Float_tTAttText::GetTextAngle() const
virtual Color_tTAttText::GetTextColor() const
virtual Font_tTAttText::GetTextFont() const
virtual Float_tTAttText::GetTextSize() const
virtual const char*TObject::GetTitle() const
virtual UInt_tTObject::GetUniqueID() const
Double_tTBox::GetX1() const
Double_tTPave::GetX1NDC() const
Double_tTBox::GetX2() const
Double_tTPave::GetX2NDC() const
Double_tTBox::GetY1() const
Double_tTPave::GetY1NDC() const
Double_tTBox::GetY2() const
Double_tTPave::GetY2NDC() const
virtual Bool_tTObject::HandleTimer(TTimer* timer)
virtual ULong_tTPave::Hash() const
virtual voidTBox::HideToolTip(Int_t event)
virtual voidTObject::Info(const char* method, const char* msgfmt) const
virtual Bool_tTObject::InheritsFrom(const char* classname) const
virtual Bool_tTObject::InheritsFrom(const TClass* cl) const
virtual voidInsertEntry(const char* objectName = "", const char* label = "", Option_t* option = "lpf")MENU
virtual voidTObject::Inspect() constMENU
voidTObject::InvertBit(UInt_t f)
virtual TClass*IsA() const
Bool_tTBox::IsBeingResized() const
virtual Bool_tTObject::IsEqual(const TObject* obj) const
virtual Bool_tTObject::IsFolder() const
virtual Int_tTBox::IsInside(Double_t x, Double_t y) const
Bool_tTObject::IsOnHeap() const
virtual Bool_tTPave::IsSortable() const
virtual Bool_tTAttFill::IsTransparent() const
Bool_tTObject::IsZombie() const
virtual voidTPave::ls(Option_t* option = "") const
voidTObject::MayNotUse(const char* method) const
virtual voidTAttLine::Modify()
virtual Bool_tTObject::Notify()
voidTObject::Obsolete(const char* method, const char* asOfVers, const char* removedFromVers) const
voidTObject::operator delete(void* ptr)
voidTObject::operator delete(void* ptr, void* vp)
voidTObject::operator delete[](void* ptr)
voidTObject::operator delete[](void* ptr, void* vp)
void*TObject::operator new(size_t sz)
void*TObject::operator new(size_t sz, void* vp)
void*TObject::operator new[](size_t sz)
void*TObject::operator new[](size_t sz, void* vp)
virtual voidPaint(Option_t* option = "")
virtual voidTBox::PaintBox(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Option_t* option = "")
virtual voidTPave::PaintPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br")
virtual voidTPave::PaintPaveArc(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize = 4, Option_t* option = "br")
virtual voidPaintPrimitives()
virtual voidTObject::Pop()
virtual voidPrint(Option_t* option = "") const
virtual Int_tTObject::Read(const char* name)
virtual voidRecursiveRemove(TObject* obj)
virtual voidTAttFill::ResetAttFill(Option_t* option = "")
virtual voidTAttLine::ResetAttLine(Option_t* option = "")
virtual voidTAttText::ResetAttText(Option_t* toption = "")
voidTObject::ResetBit(UInt_t f)
virtual voidTObject::SaveAs(const char* filename = "", Option_t* option = "") constMENU
virtual voidTAttFill::SaveFillAttributes(ostream& out, const char* name, Int_t coldef = 1, Int_t stydef = 1001)
virtual voidTAttLine::SaveLineAttributes(ostream& out, const char* name, Int_t coldef = 1, Int_t stydef = 1, Int_t widdef = 1)
virtual voidSavePrimitive(ostream& out, Option_t* option = "")
virtual voidTAttText::SaveTextAttributes(ostream& out, const char* name, Int_t alidef = 12, Float_t angdef = 0, Int_t coldef = 1, Int_t fondef = 61, Float_t sizdef = 1)
virtual voidTBox::SetBBoxCenter(const TPoint& p)
virtual voidTBox::SetBBoxCenterX(const Int_t x)
virtual voidTBox::SetBBoxCenterY(const Int_t y)
virtual voidTBox::SetBBoxX1(const Int_t x)
virtual voidTBox::SetBBoxX2(const Int_t x)
virtual voidTBox::SetBBoxY1(const Int_t y)
virtual voidTBox::SetBBoxY2(const Int_t y)
voidTObject::SetBit(UInt_t f)
voidTObject::SetBit(UInt_t f, Bool_t set)
virtual voidTPave::SetBorderSize(Int_t bordersize = 4)MENU
voidSetColumnSeparation(Float_t columnSeparation)MENU
virtual voidTPave::SetCornerRadius(Double_t rad = 0.20000000000000001)MENU
voidSetDefaults()
virtual voidTObject::SetDrawOption(Option_t* option = "")MENU
static voidTObject::SetDtorOnly(void* obj)
virtual voidSetEntryLabel(const char* label)MENU
virtual voidSetEntryOption(Option_t* option)MENU
voidSetEntrySeparation(Float_t entryseparation)MENU
virtual voidTAttFill::SetFillAttributes()MENU
virtual voidTAttFill::SetFillColor(Color_t fcolor)
virtual voidTAttFill::SetFillColorAlpha(Color_t fcolor, Float_t falpha)
virtual voidTAttFill::SetFillStyle(Style_t fstyle)
virtual voidSetHeader(const char* header = "")MENU
virtual voidTAttLine::SetLineAttributes()MENU
virtual voidTAttLine::SetLineColor(Color_t lcolor)
virtual voidTAttLine::SetLineColorAlpha(Color_t lcolor, Float_t lalpha)
virtual voidTAttLine::SetLineStyle(Style_t lstyle)
virtual voidTAttLine::SetLineWidth(Width_t lwidth)
voidSetMargin(Float_t margin)MENU
virtual voidTPave::SetName(const char* name = "")MENU
voidSetNColumns(Int_t nColumns)MENU
static voidTObject::SetObjectStat(Bool_t stat)
virtual voidTPave::SetOption(Option_t* option = "br")
virtual voidTPave::SetShadowColor(Int_t color)MENU
virtual voidTAttText::SetTextAlign(Short_t align = 11)
virtual voidTAttText::SetTextAngle(Float_t tangle = 0)MENU
virtual voidTAttText::SetTextAttributes()MENU
virtual voidTAttText::SetTextColor(Color_t tcolor = 1)
virtual voidTAttText::SetTextColorAlpha(Color_t tcolor, Float_t talpha)
virtual voidTAttText::SetTextFont(Font_t tfont = 62)
virtual voidTAttText::SetTextSize(Float_t tsize = 1)
virtual voidTAttText::SetTextSizePixels(Int_t npixels)
virtual voidTBox::SetToolTipText(const char* text, Long_t delayms = 1000)
virtual voidTObject::SetUniqueID(UInt_t uid)
virtual voidTPave::SetX1(Double_t x1)
virtual voidTPave::SetX1NDC(Double_t x1)
virtual voidTPave::SetX2(Double_t x2)
virtual voidTPave::SetX2NDC(Double_t x2)
virtual voidTPave::SetY1(Double_t y1)
virtual voidTPave::SetY1NDC(Double_t y1)
virtual voidTPave::SetY2(Double_t y2)
virtual voidTPave::SetY2NDC(Double_t y2)
virtual voidShowMembers(TMemberInspector& insp) const
virtual voidStreamer(TBuffer&)
voidStreamerNVirtual(TBuffer& ClassDef_StreamerNVirtual_b)
virtual voidTObject::SysError(const char* method, const char* msgfmt) const
Bool_tTObject::TestBit(UInt_t f) const
Int_tTObject::TestBits(UInt_t f) const
TLegend()
TLegend(const TLegend& legend)
TLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char* header = "", Option_t* option = "brNDC")
virtual voidTObject::UseCurrentStyle()
virtual voidTObject::Warning(const char* method, const char* msgfmt) const
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0)
virtual Int_tTObject::Write(const char* name = 0, Int_t option = 0, Int_t bufsize = 0) const
protected:
virtual voidTObject::DoError(int level, const char* location, const char* fmt, va_list va) const
voidTObject::MakeZombie()
TLegend&operator=(const TLegend&)

Data Members

public:
static TObject::(anonymous)TObject::kBitMask
static TObject::EStatusBitsTObject::kCanDelete
static TBox::(anonymous)TBox::kCannotMove
static TObject::EStatusBitsTObject::kCannotPick
static TObject::EStatusBitsTObject::kHasUUID
static TObject::EStatusBitsTObject::kInvalidObject
static TObject::(anonymous)TObject::kIsOnHeap
static TObject::EStatusBitsTObject::kIsReferenced
static TObject::EStatusBitsTObject::kMustCleanup
static TPave::(anonymous)TPave::kNameIsAction
static TObject::EStatusBitsTObject::kNoContextMenu
static TObject::(anonymous)TObject::kNotDeleted
static TObject::EStatusBitsTObject::kObjInCanvas
static TObject::(anonymous)TObject::kOverwrite
static TObject::(anonymous)TObject::kSingleKey
static TObject::(anonymous)TObject::kWriteDelete
static TObject::(anonymous)TObject::kZombie
protected:
Int_tTPave::fBorderSizewindow box bordersize in pixels
Float_tfColumnSeparationseparation between columns, as a fraction of
Double_tTPave::fCornerRadiusCorner radius in case of option arc
Float_tfEntrySeparationseparation between entries, as a fraction of
Color_tTAttFill::fFillColorfill area color
Style_tTAttFill::fFillStylefill area style
Int_tTPave::fInit(=0 if transformation to NDC not yet done)
Color_tTAttLine::fLineColorline color
Style_tTAttLine::fLineStyleline style
Width_tTAttLine::fLineWidthline width
Float_tfMarginfraction of total width used for symbol
Int_tfNColumnsnumber of columns in the legend
TStringTPave::fNamePave name
TStringTPave::fOptionPave style
TList*fPrimitiveslist of TLegendEntries
Bool_tTBox::fResizing!True if box is being resized
Int_tTPave::fShadowColorColor of the pave's shadow
Short_tTAttText::fTextAlignText alignment
Float_tTAttText::fTextAngleText angle
Color_tTAttText::fTextColorText color index
Font_tTAttText::fTextFontText font number
Float_tTAttText::fTextSizeText size
Double_tTBox::fX1X of 1st point
Double_tTPave::fX1NDCX1 point in NDC coordinates
Double_tTBox::fX2X of 2nd point
Double_tTPave::fX2NDCX2 point in NDC coordinates
Double_tTBox::fY1Y of 1st point
Double_tTPave::fY1NDCY1 point in NDC coordinates
Double_tTBox::fY2Y of 2nd point
Double_tTPave::fY2NDCY2 point in NDC coordinates

Class Charts

Inheritance Inherited Members Includes Libraries
Class Charts

Function documentation

TLegend()
   Default constructor.
   
 
TLegend(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char* header = "", Option_t* option = "brNDC")
   Normal Contructor.
   

A TLegend is a Pave with several TLegendEntry(s). x1,y1,x2,y2 are the coordinates of the Legend in the current pad (in normalized coordinates by default) "header" is the title that will be displayed at the top of the legend it is treated like a regular entry and supports TLatex. The default is no header (header = 0). The options are the same as for TPave Default = "brNDC"

 
TLegend(const TLegend& legend)
   Copy constuctor.
   
 
TLegend& operator=(const TLegend& )
   Assignment operator.
   
 
~TLegend()
   Default destructor.
   
 
TLegendEntry * AddEntry(const TObject* obj, const char* label = "", Option_t* option = "lpf")
   Add a new entry to this legend. "obj" is the object to be represented.
   "label" is the text you wish to associate with obj in the legend.
   If "label" is null or empty, the title of the object will be used.
   

Options are:

  • L: draw line associated with TAttLine if obj inherits from TAttLine
  • P: draw polymarker associated with TAttMarker if obj inherits from TAttMarker
  • F: draw a box with fill associated wit TAttFill if obj inherits TAttFill
  • E: draw vertical error bar if option "L" is also specified
 
TLegendEntry * AddEntry(const char* name, const char* label = "", Option_t* option = "lpf")
   Add a new entry to this legend. "name" is the name of an object in the pad to
   be represented label is the text you wish to associate with obj in the legend
   if label is null or empty, the title of the object will be used.
   

Options are:

  • L: draw line associated with TAttLine if obj inherits from TAttLine
  • P: draw polymarker associated with TAttMarker if obj inherits from TAttMarker
  • F: draw a box with fill associated wit TAttFill if obj inherits TAttFill
  • E: draw vertical error bar if option "L" is also specified
 
void Clear(Option_t* option = "")
   Clear all entries in this legend, including the header.
   
 
void Copy(TObject& obj) const
   Copy this legend into "obj".
   
 
void DeleteEntry()
   Delete entry at the mouse position.
   
 
void Draw(Option_t* option = "")
   Draw this legend with its current attributes.
   
 
void EditEntryAttFill()
   Edit the fill attributes for the entry pointed by the mouse.
   
 
void EditEntryAttLine()
   Edit the line attributes for the entry pointed by the mouse.
   
 
void EditEntryAttMarker()
   Edit the marker attributes for the entry pointed by the mouse.
   
 
void EditEntryAttText()
   Edit the text attributes for the entry pointed by the mouse.
   
 
TLegendEntry * GetEntry() const
   Get entry pointed to by the mouse.
   This method is mostly a tool for other methods inside this class.
   
 
const char * GetHeader() const
   Returns the header, which is the title that appears at the top
   of the legend.
   
 
void InsertEntry(const char* objectName = "", const char* label = "", Option_t* option = "lpf")
   Add a new entry before the entry at the mouse position.
   
 
void Paint(Option_t* option = "")
   Paint this legend with its current attributes.
   
 
Int_t GetNRows() const
   Get the number of rows.
   
 
void SetNColumns(Int_t nColumns)
   Set the number of columns for the legend. The header, if set, is given
   its own row. After that, every nColumns entries are inserted into the
   same row. For example, if one calls legend.SetNColumns(2), and there
   is no header, then the first two TObjects added to the legend will be
   in the first row, the next two will appear in the second row, and so on.
   
 
void PaintPrimitives()
   Paint the entries (list of primitives) for this legend.
   
 
void Print(Option_t* option = "") const
   Dump this TLegend and its contents.
   
 
void RecursiveRemove(TObject* obj)
   Reset the legend entries pointing to "obj".
   
 
void SavePrimitive(ostream& out, Option_t* option = "")
   Save this legend as C++ statements on output stream out
   to be used with the SaveAs .C option.
   
 
void SetEntryLabel(const char* label)
   Edit the label of the entry pointed to by the mouse.
   
 
void SetEntryOption(Option_t* option)
   Edit the option of the entry pointed to by the mouse.
   
 
void SetHeader(const char* header = "")
   Sets the header, which is the "title" that appears at the top of the legend.
   
 
Float_t GetColumnSeparation() const
{ return fColumnSeparation; }
Float_t GetEntrySeparation() const
{ return fEntrySeparation; }
TList * GetListOfPrimitives() const
{return fPrimitives;}
Float_t GetMargin() const
{ return fMargin; }
Int_t GetNColumns() const
{ return fNColumns; }
void SetDefaults()
{ fEntrySeparation = 0.1f; fMargin = 0.25f; fNColumns = 1; fColumnSeparation = 0.0f; }
void SetColumnSeparation(Float_t columnSeparation)
{ fColumnSeparation = columnSeparation; }
void SetEntrySeparation(Float_t entryseparation)
{ fEntrySeparation = entryseparation; }
void SetMargin(Float_t margin)
{ fMargin = margin; }