Jpp  15.0.1-rc.2-highQE
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  * \return legend
27  */
28  inline TLegend* getLegend(const Int_t width,
29  const Int_t height,
30  const std::string option = "TR")
31  {
32  using namespace std;
33 
34  gPad->Update();
35 
36  const Double_t X1 = 0.0 + gPad->GetLeftMargin();
37  const Double_t X2 = 1.0 - gPad->GetRightMargin();
38  const Double_t Y1 = 0.0 + gPad->GetBottomMargin();
39  const Double_t Y2 = 1.0 - gPad->GetTopMargin();
40 
41  const Double_t w = gPad->XtoPixel(gPad->GetX2());
42  const Double_t h = gPad->YtoPixel(gPad->GetY1());
43  const Double_t ch = gStyle->GetStatFontSize() * (w < h ? w : h);
44 
45  const Double_t fx = 0.70 * ch/w; // estimated font size [NDC]
46  const Double_t fy = 1.10 * ch/h; // estimated font size [NDC]
47  const Double_t eps = 0.005; // extra space [NDC]
48  const Int_t nc = 3; // number of characters reserved for margin
49 
50  Int_t wd = width;
51 
52  if (wd < 2) {
53  wd = 2;
54  }
55 
56  Double_t W = (wd + nc) * fx; // width [NDC]
57  Double_t H = (height) * fy; // height [NDC]
58 
59  Double_t fc = 1.0; // scaling factor
60 
61  if (fc * W > X2 - X1 - 2*eps) {
62  fc *= (X2 - X1 - 2*eps) / W;
63  }
64 
65  if (fc * H > Y2 - Y1 - 2*eps) {
66  fc *= (Y2 - Y1 - 2*eps) / H;
67  }
68 
69  W *= fc;
70  H *= fc;
71 
72  // default position is top-right
73 
74  Double_t x1 = X2 - W - eps;
75  Double_t y1 = Y2 - H - eps;
76  Double_t x2 = X2 - eps;
77  Double_t y2 = Y2 - eps;
78 
79  int halign = 2;
80  int valign = 2;
81 
82  if (option.find('T') != string::npos && option.find('B') == string::npos) { // top
83 
84  y1 = Y2 - H - eps;
85  y2 = Y2 - eps;
86 
87  //valign = 1;
88  }
89 
90  if (option.find('B') != string::npos && option.find('T') == string::npos) { // bottom
91 
92  y1 = Y1 + eps;
93  y2 = Y1 + H + eps;
94 
95  //valign = 3;
96  }
97 
98  if (option.find('R') != string::npos && option.find('L') == string::npos) { // right
99 
100  x1 = X2 - W - eps;
101  x2 = X2 - eps;
102 
103  halign = 1;
104  }
105 
106  if (option.find('L') != string::npos && option.find('R') == string::npos) { // left
107 
108  x1 = X1 + eps;
109  x2 = X1 + W + eps;
110 
111  halign = 1;
112  }
113 
114  TLegend* lg = new TLegend(x1, y1, x2, y2);
115 
116  lg->SetTextAlign(halign*10 + valign);
117 
118  //lg->SetFillStyle(4000);
119  lg->SetFillColor(kWhite);
120  lg->SetBorderSize(0);
121  lg->SetTextAlign(12);
122  lg->SetTextFont(gStyle->GetStatFont());
123  lg->SetTextSize(gStyle->GetStatFontSize());
124  //lg->SetTextSize(lg->GetTextSize() * fc);
125  lg->SetMargin((Double_t) nc / (Double_t) (wd + nc));
126 
127  return lg;
128  }
129 }
130 
131 #endif
data_type w[N+1][M+1]
Definition: JPolint.hh:741
static const double H
Planck constant [eV s].
TLegend * getLegend(const Int_t width, const Int_t height, const std::string option="TR")
Get legend.
Definition: JLegend.hh:28