Logo ROOT  
Reference Guide
Loading...
Searching...
No Matches
gr202_textmarkers.C
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_graphs
3/// \notebook
4/// \preview 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/// \author Olivier Couet
12
13void gr202_textmarkers() {
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 auto gr = new TGraph(n);
19 gr->SetTitle("A Simple Graph Example with Text");
20 gr->SetMarkerStyle(20);
21 auto 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 gr->Draw("ALP");
31}
32
33void drawtext()
34{
35 Int_t i,n;
36 Double_t x,y;
37 TLatex l;
38
39 l.SetTextSize(0.025);
40 l.SetTextFont(42);
41 l.SetTextAlign(21);
42 l.SetTextColor(kBlue);
43
44 auto g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
45 n = g->GetN();
46
47 for (i=0; i<n; i++) {
48 g->GetPoint(i,x,y);
49 l.PaintText(x,y+0.2,Form("(%4.2f,%4.2f)",x,y));
50 }
51}
52
#define c(i)
Definition RSha256.hxx:101
#define g(i)
Definition RSha256.hxx:105
int Int_t
Signed integer 4 bytes (int).
Definition RtypesCore.h:59
double Double_t
Double 8 bytes.
Definition RtypesCore.h:73
@ kBlue
Definition Rtypes.h:67
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition TString.cxx:2496
#define gPad
The Canvas class.
Definition TCanvas.h:23
TExec is a utility class that can be used to execute a C++ command when some event happens in a pad.
Definition TExec.h:26
void drawtext(double x, double y, int f, const char *s)
Definition fonts.C:1
RVec< PromoteType< T > > sin(const RVec< T > &v)
Definition RVec.hxx:1847
Double_t y[n]
Definition legend1.C:17
Double_t x[n]
Definition legend1.C:17
const Int_t n
Definition legend1.C:16
TGraphErrors * gr
Definition legend1.C:25
Double_t ex[n]
Definition legend1.C:17
TLine l
Definition textangle.C:4