Logo ROOT  
Reference Guide
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.
9/// \macro_image
10/// \macro_code
12/// \author Olivier Couet
14void graphtext() {
15 TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
16 c->SetGrid();
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();");
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);
31 }
32 gr->Draw("ALP");
35void drawtext()
37 Int_t i,n;
38 Double_t x,y;
39 TLatex l;
41 l.SetTextSize(0.025);
42 l.SetTextFont(42);
43 l.SetTextAlign(21);
44 l.SetTextColor(kBlue);
46 auto g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
47 n = g->GetN();
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 }
#define c(i)
Definition: RSha256.hxx:101
int Int_t
Definition: RtypesCore.h:45
double Double_t
Definition: RtypesCore.h:59
@ kBlue
Definition: Rtypes.h:66
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t g
char * Form(const char *fmt,...)
Formats a string in a circular formatting buffer.
Definition: TString.cxx:2452
#define gPad
Definition: TVirtualPad.h:288
virtual void SetMarkerStyle(Style_t mstyle=1)
Set the marker style.
Definition: TAttMarker.h:40
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:28
A TGraph is an object made of two arrays X and Y with npoints each.
Definition: TGraph.h:41
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2281
TList * GetListOfFunctions() const
Definition: TGraph.h:120
void Draw(Option_t *chopt="") override
Draw this graph with its current attributes.
Definition: TGraph.cxx:806
void SetTitle(const char *title="") override
Change (i.e.
Definition: TGraph.cxx:2336
To draw Mathematical Formula.
Definition: TLatex.h:18
void Add(TObject *obj) override
Definition: TList.h:81
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:1775
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