Logo ROOT   6.30.04
Reference Guide
 All Namespaces Files Pages
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 a graph's point is
7 /// moved interactively, the text 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  auto gr = new TGraph(n);
20  gr->SetTitle("A Simple Graph Example with Text");
21  gr->SetMarkerStyle(20);
22  auto 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  l.SetTextSize(0.025);
42  l.SetTextFont(42);
43  l.SetTextAlign(21);
44  l.SetTextColor(kBlue);
45 
46  auto g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
47  n = g->GetN();
48 
49  for (i=0; i<n; i++) {
50  g->GetPoint(i,x,y);
51  l.PaintText(x,y+0.2,Form("(%4.2f,%4.2f)",x,y));
52  }
53 }
54