ROOT  6.07/01
Reference Guide
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
graphtext.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// Draw a graph with text attached to each point.
4 /// The text is drawn in a TExec function attached to the TGraph,
5 /// therefore if the text is
6 /// moved interactively, it will be automatically updated.
7 ///
8 /// \macro_image
9 /// \macro_code
10 ///
11 /// \author Olivier Couet
12 
13 void graphtext() {
14  TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
15  c->SetGrid();
16 
17  const Int_t n = 10;
18  TGraph *gr = new TGraph(n);
19  gr->SetTitle("A Simple Graph Example with Text");
20  gr->SetMarkerStyle(20);
21  TExec *ex = new TExec("ex","drawtext();");
22  gr->GetListOfFunctions()->Add(ex);
23 
24  Double_t x, y;
25  for (Int_t i=0;i<n;i++) {
26  x = i*0.1;
27  y = 10*sin(x+0.2);
28  gr->SetPoint(i,x,y);
29 
30  }
31  gr->Draw("ALP");
32 }
33 
34 void drawtext()
35 {
36  Int_t i,n;
37  Double_t x,y;
38  TLatex *l;
39 
40  TGraph *g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
41  n = g->GetN();
42  for (i=1; i<n; i++) {
43  g->GetPoint(i,x,y);
44  l = new TLatex(x,y+0.2,Form("%4.2f",y));
45  l->SetTextSize(0.025);
46  l->SetTextFont(42);
47  l->SetTextAlign(21);
48  l->Paint();
49  }
50 }
51 
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2153
Int_t GetN() const
Definition: TGraph.h:132
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:740
virtual void SetTextFont(Font_t tfont=62)
Definition: TAttText.h:59
Double_t x[n]
Definition: legend1.C:17
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:326
TList * GetListOfFunctions() const
Definition: TGraph.h:126
To draw Mathematical Formula.
Definition: TLatex.h:33
virtual void Paint(Option_t *option="")
Paint.
Definition: TLatex.cxx:2004
double sin(double)
virtual void SetTextAlign(Short_t align=11)
Definition: TAttText.h:55
char * Form(const char *fmt,...)
TLine * l
Definition: textangle.C:4
virtual void SetMarkerStyle(Style_t mstyle=1)
Definition: TAttMarker.h:53
The Canvas class.
Definition: TCanvas.h:48
double Double_t
Definition: RtypesCore.h:55
void drawtext(double x, double y, int f, const char *s)
Definition: fonts.C:1
Double_t y[n]
Definition: legend1.C:17
virtual Int_t GetPoint(Int_t i, Double_t &x, Double_t &y) const
Get x and y values for point number i.
Definition: TGraph.cxx:1551
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2127
virtual void Add(TObject *obj)
Definition: TList.h:81
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:53
#define gPad
Definition: TVirtualPad.h:288
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad...
Definition: TExec.h:30
virtual void SetTextSize(Float_t tsize=1)
Definition: TAttText.h:60
const Int_t n
Definition: legend1.C:16