Logo ROOT   6.10/09
Reference Guide
graphtext.C
Go to the documentation of this file.
1 /// \file
2 /// \ingroup tutorial_graphs
3 /// \notebook
4 /// Draw a graph with text attached to each point.
5 /// The text is drawn in a TExec function attached to the TGraph,
6 /// therefore if the text is
7 /// moved interactively, it will be automatically updated.
8 ///
9 /// \macro_image
10 /// \macro_code
11 ///
12 /// \author Olivier Couet
13 
14 void graphtext() {
15  TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
16  c->SetGrid();
17 
18  const Int_t n = 10;
19  TGraph *gr = new TGraph(n);
20  gr->SetTitle("A Simple Graph Example with Text");
21  gr->SetMarkerStyle(20);
22  TExec *ex = new TExec("ex","drawtext();");
23  gr->GetListOfFunctions()->Add(ex);
24 
25  Double_t x, y;
26  for (Int_t i=0;i<n;i++) {
27  x = i*0.1;
28  y = 10*sin(x+0.2);
29  gr->SetPoint(i,x,y);
30 
31  }
32  gr->Draw("ALP");
33 }
34 
35 void drawtext()
36 {
37  Int_t i,n;
38  Double_t x,y;
39  TLatex *l;
40 
41  TGraph *g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
42  n = g->GetN();
43  for (i=1; i<n; i++) {
44  g->GetPoint(i,x,y);
45  l = new TLatex(x,y+0.2,Form("%4.2f",y));
46  l->SetTextSize(0.025);
47  l->SetTextFont(42);
48  l->SetTextAlign(21);
49  l->Paint();
50  }
51 }
52 
TList * GetListOfFunctions() const
Definition: TGraph.h:116
int Int_t
Definition: RtypesCore.h:41
virtual void SetTitle(const char *title="")
Set graph title.
Definition: TGraph.cxx:2180
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:745
virtual void SetTextFont(Font_t tfont=62)
Set the text font.
Definition: TAttText.h:45
Double_t x[n]
Definition: legend1.C:17
virtual void SetGrid(Int_t valuex=1, Int_t valuey=1)
Definition: TPad.h:323
To draw Mathematical Formula.
Definition: TLatex.h:18
virtual void Paint(Option_t *option="")
Paint.
Definition: TLatex.cxx:2021
double sin(double)
virtual void SetTextAlign(Short_t align=11)
Set the text alignment.
Definition: TAttText.h:41
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:1580
char * Form(const char *fmt,...)
Int_t GetN() const
Definition: TGraph.h:122
TLine * l
Definition: textangle.C:4
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
TGraphErrors * gr
Definition: legend1.C:25
The Canvas class.
Definition: TCanvas.h:31
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 void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2156
virtual void Add(TObject *obj)
Definition: TList.h:77
A Graph is a graphics object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
#define gPad
Definition: TVirtualPad.h:284
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad...
Definition: TExec.h:28
virtual void SetTextSize(Float_t tsize=1)
Set the text size.
Definition: TAttText.h:46
Double_t ex[n]
Definition: legend1.C:17
const Int_t n
Definition: legend1.C:16