Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
TAttAxis.cxx
Go to the documentation of this file.
1 // @(#)root/base:$Id$
2 // Author: Rene Brun 12/12/94
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 "Riostream.h"
13 #include "TAttAxis.h"
14 #include "TBuffer.h"
15 #include "TStyle.h"
16 #include "TVirtualPad.h"
17 #include "TColor.h"
18 #include "TClass.h"
19 #include "TMathBase.h"
20 #include <stdlib.h>
21 
22 ClassImp(TAttAxis);
23 
24 /** \class TAttAxis
25 \ingroup Base
26 \ingroup GraphicsAtt
27 
28 Manages histogram axis attributes.
29 
30 They are:
31 
32  - The number of divisions
33  - The line axis' color
34  - The labels' color
35  - The labels' font
36  - The labels' offset
37  - The labels' size
38  - The tick marks'
39  - The axis title's offset
40  - The axis title's size
41  - The axis title's color
42  - The axis title's font
43 */
44 
45 TAttAxis::TAttAxis()
46 {
47  // Constructor.
48  ResetAttAxis();
49 }
50 
51 ////////////////////////////////////////////////////////////////////////////////
52 /// Destructor.
53 
54 TAttAxis::~TAttAxis()
55 {
56 }
57 
58 ////////////////////////////////////////////////////////////////////////////////
59 /// Copy of the object.
60 
61 void TAttAxis::Copy(TAttAxis &attaxis) const
62 {
63  attaxis.fNdivisions = fNdivisions;
64  attaxis.fAxisColor = fAxisColor;
65  attaxis.fLabelColor = fLabelColor;
66  attaxis.fLabelFont = fLabelFont;
67  attaxis.fLabelOffset = fLabelOffset;
68  attaxis.fLabelSize = fLabelSize;
69  attaxis.fTickLength = fTickLength;
70  attaxis.fTitleOffset = fTitleOffset;
71  attaxis.fTitleSize = fTitleSize;
72  attaxis.fTitleColor = fTitleColor;
73  attaxis.fTitleFont = fTitleFont;
74 }
75 
76 ////////////////////////////////////////////////////////////////////////////////
77 /// Reset axis attributes.
78 
79 void TAttAxis::ResetAttAxis(Option_t *option)
80 {
81  if (gStyle) {
82  fNdivisions = gStyle->GetNdivisions(option);
83  fAxisColor = gStyle->GetAxisColor(option);
84  fLabelColor = gStyle->GetLabelColor(option);
85  fLabelFont = gStyle->GetLabelFont(option);
86  fLabelOffset = gStyle->GetLabelOffset(option);
87  fLabelSize = gStyle->GetLabelSize(option);
88  fTickLength = gStyle->GetTickLength(option);
89  fTitleOffset = gStyle->GetTitleOffset(option);
90  fTitleSize = gStyle->GetTitleSize(option);
91  fTitleColor = gStyle->GetTitleColor(option);
92  fTitleFont = gStyle->GetTitleFont(option);
93  } else {
94  fNdivisions = 510;
95  fAxisColor = 1;
96  fLabelColor = 1;
97  fLabelFont = 62;
98  fLabelOffset = 0.005;
99  fLabelSize = 0.04;
100  fTickLength = 0.03;
101  fTitleOffset = 1;
102  fTitleSize = fLabelSize;
103  fTitleColor = 1;
104  fTitleFont = 62;
105  }
106 }
107 
108 ////////////////////////////////////////////////////////////////////////////////
109 /// Save axis attributes as C++ statement(s) on output stream out.
110 
111 void TAttAxis::SaveAttributes(std::ostream &out, const char *name, const char *subname)
112 {
113  if (fNdivisions != 510) {
114  out<<" "<<name<<subname<<"->SetNdivisions("<<fNdivisions<<");"<<std::endl;
115  }
116  if (fAxisColor != 1) {
117  if (fAxisColor > 228) {
118  TColor::SaveColor(out, fAxisColor);
119  out<<" "<<name<<subname<<"->SetAxisColor(ci);" << std::endl;
120  } else
121  out<<" "<<name<<subname<<"->SetAxisColor("<<fAxisColor<<");"<<std::endl;
122  }
123  if (fLabelColor != 1) {
124  if (fLabelColor > 228) {
125  TColor::SaveColor(out, fLabelColor);
126  out<<" "<<name<<subname<<"->SetLabelColor(ci);" << std::endl;
127  } else
128  out<<" "<<name<<subname<<"->SetLabelColor("<<fLabelColor<<");"<<std::endl;
129  }
130  if (fLabelFont != 62) {
131  out<<" "<<name<<subname<<"->SetLabelFont("<<fLabelFont<<");"<<std::endl;
132  }
133  if (TMath::Abs(fLabelOffset-0.005) > 0.0001) {
134  out<<" "<<name<<subname<<"->SetLabelOffset("<<fLabelOffset<<");"<<std::endl;
135  }
136  if (TMath::Abs(fLabelSize-0.035) > 0.001) {
137  out<<" "<<name<<subname<<"->SetLabelSize("<<fLabelSize<<");"<<std::endl;
138  }
139  if (TMath::Abs(fTitleSize-0.035) > 0.001) {
140  out<<" "<<name<<subname<<"->SetTitleSize("<<fTitleSize<<");"<<std::endl;
141  }
142  if (TMath::Abs(fTickLength-0.03) > 0.001) {
143  out<<" "<<name<<subname<<"->SetTickLength("<<fTickLength<<");"<<std::endl;
144  }
145  if (TMath::Abs(fTitleOffset) > 0.001) {
146  out<<" "<<name<<subname<<"->SetTitleOffset("<<fTitleOffset<<");"<<std::endl;
147  }
148  if (fTitleColor != 1) {
149  if (fTitleColor > 228) {
150  TColor::SaveColor(out, fTitleColor);
151  out<<" "<<name<<subname<<"->SetTitleColor(ci);" << std::endl;
152  } else
153  out<<" "<<name<<subname<<"->SetTitleColor("<<fTitleColor<<");"<<std::endl;
154  }
155  if (fTitleFont != 62) {
156  out<<" "<<name<<subname<<"->SetTitleFont("<<fTitleFont<<");"<<std::endl;
157  }
158 }
159 
160 ////////////////////////////////////////////////////////////////////////////////
161 /// Set color of the line axis and tick marks.
162 
163 void TAttAxis::SetAxisColor(Color_t color, Float_t alpha)
164 {
165  if (alpha<1.) fAxisColor = TColor::GetColorTransparent(color, alpha);
166  else fAxisColor = color;
167  if (gPad) gPad->Modified();
168 }
169 
170 ////////////////////////////////////////////////////////////////////////////////
171 /// Set color of labels.
172 
173 void TAttAxis::SetLabelColor(Color_t color, Float_t alpha)
174 {
175  if (alpha<1.) fLabelColor = TColor::GetColorTransparent(color, alpha);
176  else fLabelColor = color;
177  if (gPad) gPad->Modified();
178 }
179 
180 ////////////////////////////////////////////////////////////////////////////////
181 /// Set labels' font.
182 
183 void TAttAxis::SetLabelFont(Style_t font)
184 {
185  fLabelFont = font;
186  if (gPad) gPad->Modified();
187 }
188 
189 ////////////////////////////////////////////////////////////////////////////////
190 /// Set distance between the axis and the labels.
191 /// The distance is expressed in per cent of the pad width.
192 
193 void TAttAxis::SetLabelOffset(Float_t offset)
194 {
195  fLabelOffset = offset;
196  if (gPad) gPad->Modified();
197 }
198 
199 
200 ////////////////////////////////////////////////////////////////////////////////
201 /// Set size of axis labels.
202 /// The size is expressed in per cent of the pad size.
203 
204 void TAttAxis::SetLabelSize(Float_t size)
205 {
206  fLabelSize = size;
207  if (gPad) gPad->Modified();
208 }
209 
210 ////////////////////////////////////////////////////////////////////////////////
211 /// Set the number of divisions for this axis.
212 ///
213 /// - if optim = kTRUE (default), the number of divisions will be
214 /// optimized around the specified value.
215 /// - if optim = kFALSE, or n < 0, the axis will be forced to use
216 /// exactly n divisions.
217 ///~~~ {.cpp}
218 /// n = n1 + 100*n2 + 10000*n3
219 ///~~~
220 /// Where n1 is the number of primary divisions,
221 /// n2 is the number of second order divisions and
222 /// n3 is the number of third order divisions.
223 ///
224 /// e.g. 512 means 12 primary and 5 secondary divisions.
225 ///
226 /// If the number of divisions is "optimized" (see above) n1, n2, n3 are
227 /// maximum values.
228 
229 void TAttAxis::SetNdivisions(Int_t n, Bool_t optim)
230 {
231  Int_t ndiv = (n%1000000);
232  Bool_t isOptimized = optim && (ndiv>0);
233  Int_t current_maxDigits = abs(fNdivisions)/1000000;
234  fNdivisions = abs(ndiv) + current_maxDigits*1000000;
235  if (!isOptimized) fNdivisions = -fNdivisions;
236 
237  if (gPad) gPad->Modified();
238 }
239 
240 ////////////////////////////////////////////////////////////////////////////////
241 /// Set the number of divisions for this axis using one `int` per division level.
242 
243 void TAttAxis::SetNdivisions(Int_t n1, Int_t n2, Int_t n3, Bool_t optim)
244 {
245  SetNdivisions(n1+100*n2+10000*n3, optim);
246 }
247 
248 ////////////////////////////////////////////////////////////////////////////////
249 /// This function sets the maximum number of digits permitted for the axis labels
250 /// above which the notation with 10^N is used.
251 ///
252 /// For example, to accept 6 digits number like 900000 on the X axis of the
253 /// histogram `h` call:
254 ///
255 /// ~~~ {.cpp}
256 /// h->GetXaxis()->SetMaxDigits(6);
257 /// ~~~
258 ///
259 /// The default value is 5.
260 ///
261 /// The default value for all axis can be set with the static function
262 /// `TGaxis::SetMaxDigits`.
263 
264 void TAttAxis::SetMaxDigits(Int_t maxDigits)
265 {
266  Bool_t isOptimized = fNdivisions>0;
267  Int_t absDiv = abs(fNdivisions);
268  Int_t current_maxDigits = absDiv/1000000;
269  Int_t current_Ndivisions = absDiv - (current_maxDigits*1000000);
270  fNdivisions = (current_Ndivisions + (maxDigits*1000000));
271  if (!isOptimized) fNdivisions = -fNdivisions;
272 
273  if (gPad) gPad->Modified();
274 }
275 
276 ////////////////////////////////////////////////////////////////////////////////
277 /// Set tick mark length.
278 /// The length is expressed in per cent of the pad width.
279 
280 void TAttAxis::SetTickLength(Float_t length)
281 {
282  fTickLength = length;
283  if (gPad) gPad->Modified();
284 }
285 
286 ////////////////////////////////////////////////////////////////////////////////
287 /// Set distance between the axis and the axis title.
288 /// Offset is a correction factor with respect to the "standard" value.
289 /// - offset = 1 uses standard position that is computed in function
290 /// of the label offset and size.
291 /// - offset = 1.2 will add 20 per cent more to the standard offset.
292 /// - offset = 0 automatic placement for the Y axis title (default).
293 
294 void TAttAxis::SetTitleOffset(Float_t offset)
295 {
296  fTitleOffset = offset;
297  if (gPad) gPad->Modified();
298 }
299 
300 ////////////////////////////////////////////////////////////////////////////////
301 /// Set size of axis title.
302 /// The size is expressed in per cent of the pad width
303 
304 void TAttAxis::SetTitleSize(Float_t size)
305 {
306  fTitleSize = size;
307  if (gPad) gPad->Modified();
308 }
309 
310 ////////////////////////////////////////////////////////////////////////////////
311 /// Set color of axis title.
312 
313 void TAttAxis::SetTitleColor(Color_t color)
314 {
315  fTitleColor = color;
316  if (gPad) gPad->Modified();
317 }
318 
319 ////////////////////////////////////////////////////////////////////////////////
320 /// Set the title font.
321 
322 void TAttAxis::SetTitleFont(Style_t font)
323 {
324  fTitleFont = font;
325  if (gPad) gPad->Modified();
326 }
327 
328 ////////////////////////////////////////////////////////////////////////////////
329 /// Stream an object of class TAttAxis.
330 
331 void TAttAxis::Streamer(TBuffer &R__b)
332 {
333  if (R__b.IsReading()) {
334  UInt_t R__s, R__c;
335  Version_t R__v = R__b.ReadVersion(&R__s, &R__c);
336  if (R__v > 3) {
337  R__b.ReadClassBuffer(TAttAxis::Class(), this, R__v, R__s, R__c);
338  return;
339  }
340  //====process old versions before automatic schema evolution
341  R__b >> fNdivisions;
342  R__b >> fAxisColor;
343  R__b >> fLabelColor;
344  R__b >> fLabelFont;
345  R__b >> fLabelOffset;
346  R__b >> fLabelSize;
347  R__b >> fTickLength;
348  R__b >> fTitleOffset;
349 
350  if (R__v > 1 && R__b.GetVersionOwner() > 900)
351  R__b >> fTitleSize;
352  else
353  fTitleSize = fLabelSize;
354  if (R__v > 2) {
355  R__b >> fTitleColor;
356  R__b >> fTitleFont;
357  }
358  //====end of old versions
359 
360  } else {
361  R__b.WriteClassBuffer(TAttAxis::Class(),this);
362  }
363 }