Jpp  17.2.1-pre0
the software that should make you happy
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
JLegend.hh
Go to the documentation of this file.
1 #ifndef __JROOT__JLEGEND__
2 #define __JROOT__JLEGEND__
3 
4 #include <string>
5 
6 #include "TLegend.h"
7 #include "TPad.h"
8 #include "TStyle.h"
9 
10 
11 /**
12  * \author mdejong
13  */
14 
15 namespace JROOT {}
16 namespace JPP { using namespace JROOT; }
17 
18 namespace JROOT {
19 
20  /**
21  * Get legend.
22  *
23  * \param width width [characters]
24  * \param height height [lines]
25  * \param option position [TR TL BL BR]
26  * \param factor factor font size
27  * \return legend
28  */
29  inline TLegend* getLegend(const Int_t width,
30  const Int_t height,
31  const std::string option,
32  const Double_t factor = 1.0)
33  {
34  using namespace std;
35 
36  gPad->Update();
37 
38  const Double_t X1 = 0.0 + gPad->GetLeftMargin();
39  const Double_t X2 = 1.0 - gPad->GetRightMargin();
40  const Double_t Y1 = 0.0 + gPad->GetBottomMargin();
41  const Double_t Y2 = 1.0 - gPad->GetTopMargin();
42  const Double_t w = gPad->XtoPixel(gPad->GetX2()) * (X2 - X1) / 1.0;
43  const Double_t h = gPad->YtoPixel(gPad->GetY1()) * (Y2 - Y1) / 1.0;
44  const Double_t ch = gStyle->GetStatFontSize() * (w < h ? w : h);
45 
46  const Double_t fx = 1.00 * factor * ch/w; // estimated font size [NDC]
47  const Double_t fy = 1.00 * factor * ch/h; // estimated font size [NDC]
48  const Double_t eps = 0.005; // extra space [NDC]
49  const Int_t nc = 2; // number of characters reserved for margin
50 
51  Int_t wd = width;
52 
53  if (wd < 3) {
54  wd = 3;
55  }
56 
57  Double_t W = (wd) * fx; // width [NDC]
58  Double_t H = (height) * fy; // height [NDC]
59 
60  Double_t fc = factor; // scaling factor
61 
62  if (fc * W > X2 - X1 - 2*eps) {
63  fc *= (X2 - X1 - 2*eps) / W;
64  }
65 
66  if (fc * H > Y2 - Y1 - 2*eps) {
67  fc *= (Y2 - Y1 - 2*eps) / H;
68  }
69 
70  W *= fc;
71  H *= fc;
72 
73  // default position is top-right
74 
75  Double_t x1 = X2 - W - eps;
76  Double_t y1 = Y2 - H - eps;
77  Double_t x2 = X2 - eps;
78  Double_t y2 = Y2 - eps;
79 
80  int halign = 1;
81  int valign = 2;
82 
83  if (option.find('T') != string::npos && option.find('B') == string::npos) { // top
84 
85  y1 = Y2 - H - eps;
86  y2 = Y2 - eps;
87 
88  //valign = 1;
89  }
90 
91  if (option.find('B') != string::npos && option.find('T') == string::npos) { // bottom
92 
93  y1 = Y1 + eps;
94  y2 = Y1 + H + eps;
95 
96  //valign = 3;
97  }
98 
99  if (option.find('R') != string::npos && option.find('L') == string::npos) { // right
100 
101  x1 = X2 - W - eps;
102  x2 = X2 - eps;
103 
104  //halign = 3;
105  }
106 
107  if (option.find('L') != string::npos && option.find('R') == string::npos) { // left
108 
109  x1 = X1 + eps;
110  x2 = X1 + W + eps;
111 
112  //halign = 1;
113  }
114 
115  TLegend* lg = new TLegend(x1, y1, x2, y2);
116 
117  //lg->SetFillStyle(4000);
118  lg->SetFillColor(kWhite);
119  lg->SetBorderSize(0);
120  lg->SetTextAlign(halign*10 + valign);
121  lg->SetTextFont(gStyle->GetStatFont());
122  lg->SetTextSize(gStyle->GetStatFontSize() * factor);
123  lg->SetMargin((Double_t) nc / (Double_t) (wd + nc));
124 
125  return lg;
126  }
127 }
128 
129 #endif
data_type w[N+1][M+1]
Definition: JPolint.hh:778
static const double H
Planck constant [eV s].
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option, const Double_t factor=1.0)
Get legend.
Definition: JLegend.hh:29
then awk string