Skip to content

Commit d1c338c

Browse files
committed
[hist] Add SetRefPad for consistent axis scaling
1 parent 8ea9f59 commit d1c338c

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

graf2d/graf/inc/TGaxis.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class TGaxis : public TLine, public TAttText {
4747
TF1 *fFunction; ///<! Pointer to function computing axis values
4848
TAxis *fAxis; ///<! Pointer to original TAxis axis (if any)
4949
TList *fModLabs; ///< List of modified labels.
50+
Float_t fRefLength; ///<! Reference length for automatic scaling (not saved to file)
5051

5152
TGaxis(const TGaxis&);
5253
TGaxis& operator=(const TGaxis&);
@@ -137,6 +138,9 @@ class TGaxis : public TLine, public TAttText {
137138

138139
void SetLabelColor(TColorNumber lcolor);
139140

141+
void SetRefLength(Float_t len) { fRefLength = len; }
142+
Float_t GetRefLength() const { return fRefLength; }
143+
140144
ClassDefOverride(TGaxis,6) //Graphics axis
141145
};
142146

graf2d/graf/src/TGaxis.cxx

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -690,7 +690,7 @@ End_Macro
690690
////////////////////////////////////////////////////////////////////////////////
691691
/// TGaxis default constructor.
692692

693-
TGaxis::TGaxis(): TLine(), TAttText(11,0,1,62,0.040)
693+
TGaxis::TGaxis(): TLine(), TAttText(11,0,1,62,0.040), fRefLength(0)
694694
{
695695

696696
fGridLength = 0.;
@@ -721,7 +721,7 @@ TGaxis::TGaxis(): TLine(), TAttText(11,0,1,62,0.040)
721721
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
722722
Double_t wmin, Double_t wmax, Int_t ndiv, Option_t *chopt,
723723
Double_t gridlength)
724-
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040)
724+
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040), fRefLength(0)
725725
{
726726

727727
fWmin = wmin;
@@ -758,7 +758,7 @@ TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
758758
TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
759759
const char *funcname, Int_t ndiv, Option_t *chopt,
760760
Double_t gridlength)
761-
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040)
761+
: TLine(xmin,ymin,xmax,ymax), TAttText(11,0,1,62,0.040), fRefLength(0)
762762
{
763763

764764
fFunction = (TF1*)gROOT->GetFunction(funcname);
@@ -795,6 +795,7 @@ TGaxis::TGaxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t ymax,
795795
TGaxis::TGaxis(const TGaxis& ax) :
796796
TLine(ax),
797797
TAttText(ax),
798+
fRefLength(ax.fRefLength),
798799
fWmin(ax.fWmin),
799800
fWmax(ax.fWmax),
800801
fGridLength(ax.fGridLength),
@@ -830,6 +831,7 @@ TGaxis& TGaxis::operator=(const TGaxis& ax)
830831
if(this!=&ax) {
831832
TLine::operator=(ax);
832833
TAttText::operator=(ax);
834+
fRefLength = ax.fRefLength;
833835
fWmin=ax.fWmin;
834836
fWmax=ax.fWmax;
835837
fGridLength=ax.fGridLength;
@@ -977,6 +979,7 @@ void TGaxis::ImportAxisAttributes(TAxis *axis)
977979
SetBit(TAxis::kMoreLogLabels, axis->TestBit(TAxis::kMoreLogLabels));
978980
if (axis->GetDecimals()) SetBit(TAxis::kDecimals); //the bit is in TAxis::fAxis2
979981
SetTimeFormat(axis->GetTimeFormat());
982+
SetRefLength(axis->GetRefLength());
980983
}
981984

982985
////////////////////////////////////////////////////////////////////////////////
@@ -1082,6 +1085,42 @@ void TGaxis::PaintAxis(Double_t xmin, Double_t ymin, Double_t xmax, Double_t yma
10821085

10831086
Double_t rwmi = wmin;
10841087
Double_t rwma = wmax;
1088+
1089+
struct AttributeRestorer {
1090+
TGaxis *fAxis;
1091+
Float_t fLabelSize, fTitleSize, fTickSize, fLabelOffset, fTitleOffset;
1092+
AttributeRestorer(TGaxis *ax) : fAxis(ax),
1093+
fLabelSize(ax->GetLabelSize()), fTitleSize(ax->GetTitleSize()),
1094+
fTickSize(ax->GetTickSize()), fLabelOffset(ax->GetLabelOffset()),
1095+
fTitleOffset(ax->GetTitleOffset()) {}
1096+
~AttributeRestorer() {
1097+
fAxis->SetLabelSize(fLabelSize);
1098+
fAxis->SetTitleSize(fTitleSize);
1099+
fAxis->SetTickSize(fTickSize);
1100+
fAxis->SetLabelOffset(fLabelOffset);
1101+
fAxis->SetTitleOffset(fTitleOffset);
1102+
}
1103+
};
1104+
AttributeRestorer restorer(this);
1105+
1106+
Double_t scale = 1.0;
1107+
1108+
if (fRefLength > 0 && gPad) {
1109+
Double_t curH = gPad->GetWh() * gPad->GetAbsHNDC();
1110+
if (curH > 0) scale = fRefLength / curH;
1111+
}
1112+
1113+
if (scale != 1.0) {
1114+
// Only scale if precision is 2 (relative sizing). Precision 3 (pixels) ignores this.
1115+
if (GetLabelFont()%10 < 3) fLabelSize *= scale;
1116+
if (GetTextFont()%10 < 3) fTitleSize *= scale;
1117+
1118+
fTickSize *= scale;
1119+
fLabelOffset *= scale;
1120+
}
1121+
1122+
1123+
10851124
chtemp = &kchtemp[0];
10861125
label = &chlabel[0];
10871126

hist/hist/inc/TAxis.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include "TArrayD.h"
2727
#include <vector>
2828

29+
class TVirtualPad;
2930
class THashList;
3031
class TAxisModLab;
3132

@@ -44,6 +45,7 @@ class TAxis : public TNamed, public TAttAxis {
4445
TObject *fParent = nullptr; ///<! Object owning this axis
4546
THashList *fLabels = nullptr; ///< List of labels
4647
TList *fModLabs = nullptr; ///< List of modified labels
48+
Float_t fRefLength = 0; ///<! Reference length for automatic scaling (not saved to file)
4749

4850
/// TAxis extra status bits (stored in fBits2)
4951
enum {
@@ -176,6 +178,9 @@ class TAxis : public TNamed, public TAttAxis {
176178
virtual void UnZoom(); // *MENU*
177179
virtual void ZoomOut(Double_t factor=0, Double_t offset=0); // *MENU*
178180

181+
void SetRefPad(TVirtualPad *pad);
182+
Float_t GetRefLength() const { return fRefLength; }
183+
179184
ClassDefOverride(TAxis,10) //Axis class
180185
};
181186

hist/hist/src/TAxis.cxx

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ TAxis::TAxis()
5656
fLast = 0;
5757
fBits2 = 0;
5858
fTimeDisplay = false;
59+
fRefLength = 0;
5960
}
6061

6162
////////////////////////////////////////////////////////////////////////////////
@@ -103,6 +104,7 @@ TAxis::TAxis(const TAxis &axis) : TNamed(axis), TAttAxis(axis)
103104
fParent = nullptr;
104105
fLabels = nullptr;
105106
fModLabs = nullptr;
107+
fRefLength = 0;
106108

107109
axis.TAxis::Copy(*this);
108110
}
@@ -117,6 +119,21 @@ TAxis& TAxis::operator=(const TAxis &axis)
117119
return *this;
118120
}
119121

122+
////////////////////////////////////////////////////////////////////////////////
123+
/// Set the reference pad for automatic axis sizing.
124+
/// The axis will store the absolute pixel height of this pad and use it
125+
/// to scale its attributes when drawn in other pads.
126+
127+
void TAxis::SetRefPad(TVirtualPad *pad)
128+
{
129+
if (!pad) {
130+
fRefLength = 0;
131+
return;
132+
}
133+
fRefLength = pad->GetWh() * pad->GetAbsHNDC();
134+
}
135+
136+
120137

121138
////////////////////////////////////////////////////////////////////////////////
122139
/// Choose a reasonable time format from the coordinates in the active pad
@@ -223,6 +240,7 @@ void TAxis::Copy(TObject &obj) const
223240
axis.fTimeFormat = fTimeFormat;
224241
axis.fTimeDisplay = fTimeDisplay;
225242
axis.fParent = fParent;
243+
axis.fRefLength = fRefLength;
226244
if (axis.fLabels) {
227245
axis.fLabels->Delete();
228246
delete axis.fLabels;

0 commit comments

Comments
 (0)