Logo ROOT  
Reference Guide
TPaveLabel.cxx
Go to the documentation of this file.
1 // @(#)root/graf:$Id$
2 // Author: Rene Brun 17/10/95
3 
4 /*************************************************************************
5  * Copyright (C) 1995-2000, Rene Brun and Fons Rademakers. *
6  * All rights reserved. *
7  * *
8  * For the licensing terms see $ROOTSYS/LICENSE. *
9  * For the list of contributors see $ROOTSYS/README/CREDITS. *
10  *************************************************************************/
11 
12 #include <iostream>
13 #include "TROOT.h"
14 #include "TStyle.h"
15 #include "TPaveLabel.h"
16 #include "TLatex.h"
17 #include "TVirtualPad.h"
18 
20 
21 /** \class TPaveLabel
22 \ingroup BasicGraphics
23 
24 A Pave (see TPave) with a text centered in the Pave.
25 
26 \image html graf_pavelabel.png
27 */
28 
29 ////////////////////////////////////////////////////////////////////////////////
30 /// Pavelabel default constructor.
31 
33 {
34 }
35 
36 ////////////////////////////////////////////////////////////////////////////////
37 /// Pavelabel normal constructor.
38 ///
39 /// a PaveLabel is a Pave with a label centered in the Pave
40 /// The Pave is by default defined bith bordersize=5 and option ="br".
41 /// The text size is automatically computed as a function of the pave size.
42 
43 TPaveLabel::TPaveLabel(Double_t x1, Double_t y1,Double_t x2, Double_t y2, const char *label, Option_t *option)
44  :TPave(x1,y1,x2,y2,3,option), TAttText(22,0,1,gStyle->GetTextFont(),0.99)
45 {
46  fLabel = label;
47 }
48 
49 ////////////////////////////////////////////////////////////////////////////////
50 /// Pavelabel default destructor.
51 
53 {
54 }
55 
56 ////////////////////////////////////////////////////////////////////////////////
57 /// Pavelabel copy constructor.
58 
59 TPaveLabel::TPaveLabel(const TPaveLabel &pavelabel) : TPave(pavelabel), TAttText(pavelabel)
60 {
61  ((TPaveLabel&)pavelabel).Copy(*this);
62 }
63 
64 ////////////////////////////////////////////////////////////////////////////////
65 /// Copy this pavelabel to pavelabel.
66 
67 void TPaveLabel::Copy(TObject &obj) const
68 {
69  TPave::Copy(obj);
70  TAttText::Copy(((TPaveLabel&)obj));
71  ((TPaveLabel&)obj).fLabel = fLabel;
72 }
73 
74 ////////////////////////////////////////////////////////////////////////////////
75 /// Draw this pavelabel with its current attributes.
76 
78 {
79  Option_t *opt;
80  if (option && strlen(option)) opt = option;
81  else opt = GetOption();
82 
83  AppendPad(opt);
84 }
85 
86 ////////////////////////////////////////////////////////////////////////////////
87 /// Draw this pavelabel with new coordinates.
88 
89 void TPaveLabel::DrawPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option)
90 {
91  TPaveLabel *newpavelabel = new TPaveLabel(x1,y1,x2,y2,label,option);
92  newpavelabel->SetBit(kCanDelete);
93  newpavelabel->AppendPad();
94 }
95 
96 ////////////////////////////////////////////////////////////////////////////////
97 /// Paint this pavelabel with its current attributes.
98 
100 {
101  // Convert from NDC to pad coordinates
103 
104  PaintPaveLabel(fX1, fY1, fX2, fY2, GetLabel(), strlen(option)?option:GetOption());
105 }
106 
107 ////////////////////////////////////////////////////////////////////////////////
108 /// Draw this pavelabel with new coordinates.
109 
111  const char *label ,Option_t *option)
112 {
113  if (!gPad) return;
114  Int_t nch = strlen(label);
115 
116  // Draw the pave
117  TPave::PaintPave(x1,y1,x2,y2,GetBorderSize(),option);
118 
119  Float_t nspecials = 0;
120  for (Int_t i=0;i<nch;i++) {
121  if (label[i] == '!') nspecials += 1;
122  if (label[i] == '?') nspecials += 1.5;
123  if (label[i] == '#') nspecials += 1;
124  if (label[i] == '`') nspecials += 1;
125  if (label[i] == '^') nspecials += 1.5;
126  if (label[i] == '~') nspecials += 1;
127  if (label[i] == '&') nspecials += 2;
128  if (label[i] == '\\') nspecials += 3; // octal characters very likely
129  }
130  nch -= Int_t(nspecials + 0.5);
131  if (nch <= 0) return;
132 
133  // Draw label
134  Double_t wh = (Double_t)gPad->XtoPixel(gPad->GetX2());
135  Double_t hh = (Double_t)gPad->YtoPixel(gPad->GetY1());
136  if (wh==0||hh==0) return;
137  Double_t labelsize, textsize = GetTextSize();
138  Int_t automat = 0;
139  if (GetTextFont()%10 > 2) { // fixed size font specified in pixels
140  labelsize = GetTextSize();
141  } else {
142  if (TMath::Abs(textsize -0.99) < 0.001) automat = 1;
143  if (textsize == 0) { textsize = 0.99; automat = 1;}
144  Int_t ypixel = TMath::Abs(gPad->YtoPixel(y1) - gPad->YtoPixel(y2));
145  labelsize = textsize*ypixel/hh;
146  if (wh < hh) labelsize *= hh/wh;
147  }
148  TLatex latex;
149  latex.SetTextAngle(GetTextAngle());
150  latex.SetTextFont(GetTextFont());
151  latex.SetTextAlign(GetTextAlign());
152  latex.SetTextColor(GetTextColor());
153  latex.SetTextSize(labelsize);
154  if (automat) {
155  UInt_t w=0,h=0,w1=0;
156  latex.GetTextExtent(w,h,GetTitle());
157  if (!w) return;
158  labelsize = h/hh;
159  Double_t wxlabel = TMath::Abs(gPad->XtoPixel(x2) - gPad->XtoPixel(x1));
160  latex.GetTextExtent(w1,h,GetTitle());
161  while (w > 0.99*wxlabel) {
162  labelsize *= 0.99*wxlabel/w;
163  latex.SetTextSize(labelsize);
164  latex.GetTextExtent(w,h,GetTitle());
165  if (w==w1) break;
166  else w1=w;
167  }
168  if (h < 1) h = 1;
169  if (h==1) {
170  labelsize = Double_t(h)/hh;
171  if (wh < hh) labelsize *= hh/wh;
172  latex.SetTextSize(labelsize);
173  }
174  }
175  Int_t halign = GetTextAlign()/10;
176  Int_t valign = GetTextAlign()%10;
177  Double_t x = 0.5*(x1+x2);
178  if (halign == 1) x = x1 + 0.02*(x2-x1);
179  if (halign == 3) x = x2 - 0.02*(x2-x1);
180  Double_t y = 0.5*(y1+y2);
181  if (valign == 1) y = y1 + 0.02*(y2-y1);
182  if (valign == 3) y = y2 - 0.02*(y2-y1);
183  latex.PaintLatex(x, y, GetTextAngle(),labelsize,GetLabel());
184 }
185 
186 ////////////////////////////////////////////////////////////////////////////////
187 /// Save primitive as a C++ statement(s) on output stream out
188 
189 void TPaveLabel::SavePrimitive(std::ostream &out, Option_t * /*= ""*/)
190 {
191  char quote = '"';
192  out<<" "<<std::endl;
193  if (gROOT->ClassSaved(TPaveLabel::Class())) {
194  out<<" ";
195  } else {
196  out<<" TPaveLabel *";
197  }
198  TString s = fLabel.Data();
199  s.ReplaceAll("\"","\\\"");
200  if (fOption.Contains("NDC")) {
201  out<<"pl = new TPaveLabel("<<fX1NDC<<","<<fY1NDC<<","<<fX2NDC<<","<<fY2NDC
202  <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
203  } else {
204  out<<"pl = new TPaveLabel("<<gPad->PadtoX(fX1)<<","<<gPad->PadtoY(fY1)<<","<<gPad->PadtoX(fX2)<<","<<gPad->PadtoY(fY2)
205  <<","<<quote<<s.Data()<<quote<<","<<quote<<fOption<<quote<<");"<<std::endl;
206  }
207  if (fBorderSize != 3) {
208  out<<" pl->SetBorderSize("<<fBorderSize<<");"<<std::endl;
209  }
210  SaveFillAttributes(out,"pl",19,1001);
211  SaveLineAttributes(out,"pl",1,1,1);
212  SaveTextAttributes(out,"pl",22,0,1,62,0);
213 
214  out<<" pl->Draw();"<<std::endl;
215 }
TAttText
Text Attributes class.
Definition: TAttText.h:18
TPave::fY1NDC
Double_t fY1NDC
Y1 point in NDC coordinates.
Definition: TPave.h:23
TPave::GetBorderSize
Int_t GetBorderSize() const
Definition: TPave.h:54
TPave::Copy
void Copy(TObject &pave) const
Copy this pave to pave.
Definition: TPave.cxx:186
Option_t
const char Option_t
Definition: RtypesCore.h:66
TPaveLabel::GetTitle
const char * GetTitle() const
Returns title of object.
Definition: TPaveLabel.h:36
TPaveLabel::fLabel
TString fLabel
Label written at the center of Pave.
Definition: TPaveLabel.h:23
TString::Data
const char * Data() const
Definition: TString.h:369
ClassImp
#define ClassImp(name)
Definition: Rtypes.h:364
TAttText::GetTextColor
virtual Color_t GetTextColor() const
Return the text color.
Definition: TAttText.h:34
TPave::ConvertNDCtoPad
virtual void ConvertNDCtoPad()
Convert pave coordinates from NDC to Pad coordinates.
Definition: TPave.cxx:139
TAttText::SetTextColor
virtual void SetTextColor(Color_t tcolor=1)
Set the text color.
Definition: TAttText.h:43
TAttText::SaveTextAttributes
virtual void SaveTextAttributes(std::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)
Save text attributes as C++ statement(s) on output stream out.
Definition: TAttText.cxx:344
TPaveLabel::PaintPaveLabel
virtual void PaintPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option="")
Draw this pavelabel with new coordinates.
Definition: TPaveLabel.cxx:110
TPaveLabel::Paint
virtual void Paint(Option_t *option="")
Paint this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:99
TBox::fX2
Double_t fX2
X of 2nd point.
Definition: TBox.h:30
Float_t
float Float_t
Definition: RtypesCore.h:57
TStyle.h
TGeant4Unit::s
static constexpr double s
Definition: TGeant4SystemOfUnits.h:162
Int_t
int Int_t
Definition: RtypesCore.h:45
TAttText::Copy
void Copy(TAttText &atttext) const
Copy this text attributes to a new TAttText.
Definition: TAttText.cxx:291
TObject::AppendPad
virtual void AppendPad(Option_t *option="")
Append graphics object to current pad.
Definition: TObject.cxx:107
TBox::fX1
Double_t fX1
X of 1st point.
Definition: TBox.h:28
TString::Contains
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Definition: TString.h:624
TAttText::SetTextSize
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
TAttLine::SaveLineAttributes
virtual void SaveLineAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1, Int_t widdef=1)
Save line attributes as C++ statement(s) on output stream out.
Definition: TAttLine.cxx:270
x
Double_t x[n]
Definition: legend1.C:17
TLatex
To draw Mathematical Formula.
Definition: TLatex.h:18
TMath::Abs
Short_t Abs(Short_t d)
Definition: TMathBase.h:120
TAttText::GetTextSize
virtual Float_t GetTextSize() const
Return the text size.
Definition: TAttText.h:36
TString
Basic string class.
Definition: TString.h:136
TAttFill::SaveFillAttributes
virtual void SaveFillAttributes(std::ostream &out, const char *name, Int_t coldef=1, Int_t stydef=1001)
Save fill attributes as C++ statement(s) on output stream out.
Definition: TAttFill.cxx:234
TPaveLabel::SavePrimitive
virtual void SavePrimitive(std::ostream &out, Option_t *option="")
Save primitive as a C++ statement(s) on output stream out.
Definition: TPaveLabel.cxx:189
TPaveLabel::Draw
virtual void Draw(Option_t *option="")
Draw this pavelabel with its current attributes.
Definition: TPaveLabel.cxx:77
TPaveLabel.h
x1
static const double x1[5]
Definition: RooGaussKronrodIntegrator1D.cxx:346
TROOT.h
TAttText::GetTextAngle
virtual Float_t GetTextAngle() const
Return the text angle.
Definition: TAttText.h:33
gStyle
R__EXTERN TStyle * gStyle
Definition: TStyle.h:412
TAttText::GetTextFont
virtual Font_t GetTextFont() const
Return the text font.
Definition: TAttText.h:35
TPave::fX1NDC
Double_t fX1NDC
X1 point in NDC coordinates.
Definition: TPave.h:22
h
#define h(i)
Definition: RSha256.hxx:106
TObject::SetBit
void SetBit(UInt_t f, Bool_t set)
Set or unset the user status bits as specified in f.
Definition: TObject.cxx:696
TPave::fY2NDC
Double_t fY2NDC
Y2 point in NDC coordinates.
Definition: TPave.h:25
TPaveLabel::Copy
void Copy(TObject &pavelabel) const
Copy this pavelabel to pavelabel.
Definition: TPaveLabel.cxx:67
TVirtualPad.h
y
Double_t y[n]
Definition: legend1.C:17
TPave
A TBox with a bordersize and a shadow option.
Definition: TPave.h:19
TAttText::GetTextAlign
virtual Short_t GetTextAlign() const
Return the text alignment.
Definition: TAttText.h:32
TPaveLabel::DrawPaveLabel
virtual void DrawPaveLabel(Double_t x1, Double_t y1, Double_t x2, Double_t y2, const char *label, Option_t *option="")
Draw this pavelabel with new coordinates.
Definition: TPaveLabel.cxx:89
TBox::fY1
Double_t fY1
Y of 1st point.
Definition: TBox.h:29
TObject::kCanDelete
@ kCanDelete
if object in a list can be deleted
Definition: TObject.h:58
unsigned int
TPaveLabel
A Pave (see TPave) with a text centered in the Pave.
Definition: TPaveLabel.h:20
TPave::fBorderSize
Int_t fBorderSize
window box bordersize in pixels
Definition: TPave.h:26
TPaveLabel::~TPaveLabel
virtual ~TPaveLabel()
Pavelabel default destructor.
Definition: TPaveLabel.cxx:52
TPave::fX2NDC
Double_t fX2NDC
X2 point in NDC coordinates.
Definition: TPave.h:24
TAttText::SetTextAlign
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
Double_t
double Double_t
Definition: RtypesCore.h:59
TPaveLabel::TPaveLabel
TPaveLabel()
Pavelabel default constructor.
Definition: TPaveLabel.cxx:32
TText::GetTextExtent
virtual void GetTextExtent(UInt_t &w, UInt_t &h, const char *text) const
Return text extent for string text.
Definition: TText.cxx:587
TLatex.h
TPave::fOption
TString fOption
Pave style.
Definition: TPave.h:30
TLatex::PaintLatex
virtual void PaintLatex(Double_t x, Double_t y, Double_t angle, Double_t size, const char *text)
Main drawing function.
Definition: TLatex.cxx:2051
TObject
Mother of all ROOT objects.
Definition: TObject.h:37
TBox::fY2
Double_t fY2
Y of 2nd point.
Definition: TBox.h:31
x2
static const double x2[5]
Definition: RooGaussKronrodIntegrator1D.cxx:364
gPad
#define gPad
Definition: TVirtualPad.h:287
Class
void Class()
Definition: Class.C:29
TAttText::SetTextAngle
virtual void SetTextAngle(Float_t tangle=0)
Set the text angle.
Definition: TAttText.h:42
TAttText::SetTextFont
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
TPave::PaintPave
virtual void PaintPave(Double_t x1, Double_t y1, Double_t x2, Double_t y2, Int_t bordersize=4, Option_t *option="br")
Draw this pave with new coordinates.
Definition: TPave.cxx:313
TPaveLabel::GetLabel
const char * GetLabel() const
Definition: TPaveLabel.h:35
TPave::GetOption
Option_t * GetOption() const
Definition: TPave.h:57
gROOT
#define gROOT
Definition: TROOT.h:406
int