Logo ROOT  
Reference Guide
graphtext.C File Reference

Detailed Description

View in nbviewer Open in SWAN Draw a graph with text attached to each point. The text is drawn in a TExec function attached to the TGraph, therefore if the a graph's point is moved interactively, the text will be automatically updated.

void graphtext() {
TCanvas *c = new TCanvas("c","A Simple Graph Example with Text",700,500);
c->SetGrid();
const Int_t n = 10;
auto gr = new TGraph(n);
gr->SetTitle("A Simple Graph Example with Text");
gr->SetMarkerStyle(20);
auto ex = new TExec("ex","drawtext();");
for (Int_t i=0;i<n;i++) {
x = i*0.1;
y = 10*sin(x+0.2);
gr->SetPoint(i,x,y);
}
gr->Draw("ALP");
}
void drawtext()
{
Int_t i,n;
l.SetTextSize(0.025);
l.SetTextFont(42);
l.SetTextAlign(21);
l.SetTextColor(kBlue);
auto g = (TGraph*)gPad->GetListOfPrimitives()->FindObject("Graph");
n = g->GetN();
for (i=0; i<n; i++) {
g->GetPoint(i,x,y);
l.PaintText(x,y+0.2,Form("(%4.2f,%4.2f)",x,y));
}
}
Author
Olivier Couet

Definition in file graphtext.C.

c
#define c(i)
Definition: RSha256.hxx:119
l
auto * l
Definition: textangle.C:4
TGraph::GetListOfFunctions
TList * GetListOfFunctions() const
Definition: TGraph.h:117
ex
Double_t ex[n]
Definition: legend1.C:17
n
const Int_t n
Definition: legend1.C:16
TGraph::SetTitle
virtual void SetTitle(const char *title="")
Change (i.e.
Definition: TGraph.cxx:2324
TGraph::Draw
virtual void Draw(Option_t *chopt="")
Draw this graph with its current attributes.
Definition: TGraph.cxx:760
Form
char * Form(const char *fmt,...)
Int_t
int Int_t
Definition: RtypesCore.h:45
drawtext
void drawtext(double x, double y, int f, const char *s)
Definition: fonts.C:1
sin
double sin(double)
x
Double_t x[n]
Definition: legend1.C:17
TLatex
Definition: TLatex.h:18
TExec
Definition: TExec.h:28
gr
TGraphErrors * gr
Definition: legend1.C:25
y
Double_t y[n]
Definition: legend1.C:17
TGraph::SetPoint
virtual void SetPoint(Int_t i, Double_t x, Double_t y)
Set x and y values for point number i.
Definition: TGraph.cxx:2269
Double_t
double Double_t
Definition: RtypesCore.h:59
TGraph
Definition: TGraph.h:41
TCanvas
Definition: TCanvas.h:23
TList::Add
virtual void Add(TObject *obj)
Definition: TList.h:87
kBlue
@ kBlue
Definition: Rtypes.h:66
gPad
#define gPad
Definition: TVirtualPad.h:287
g
#define g(i)
Definition: RSha256.hxx:123