Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
rlegend.cxx
Go to the documentation of this file.
1/// \file
2/// \ingroup tutorial_rcanvas
3///
4/// This macro generates two TH1D objects and build RLegend
5/// In addition use of auto colors are shown
6///
7/// \macro_image (rcanvas_js)
8/// \macro_code
9///
10/// \date 2019-10-09
11/// \warning This is part of the ROOT 7 prototype! It will change without notice. It might trigger earthquakes. Feedback is welcome!
12/// \author Sergey Linev <s.linev@gsi.de>
13
14/*************************************************************************
15 * Copyright (C) 1995-2019, Rene Brun and Fons Rademakers. *
16 * All rights reserved. *
17 * *
18 * For the licensing terms see $ROOTSYS/LICENSE. *
19 * For the list of contributors see $ROOTSYS/README/CREDITS. *
20 *************************************************************************/
21
24#include "ROOT/RCanvas.hxx"
25#include "ROOT/RLegend.hxx"
26#include "TRandom.h"
27
28// macro must be here while cling is not capable to load
29// library automatically for outlined function see ROOT-10336
30R__LOAD_LIBRARY(libROOTHistDraw)
31R__LOAD_LIBRARY(libROOTGraphicsPrimitives)
32
33using namespace ROOT::Experimental;
34
35void rlegend()
36{
37 // Create the histograms.
38 RAxisConfig xaxis(25, 0., 10.);
39 auto pHist = std::make_shared<RH1D>(xaxis);
40 auto pHist2 = std::make_shared<RH1D>(xaxis);
41
42 for (int n=0;n<1000;n++) {
43 pHist->Fill(gRandom->Gaus(3,0.8));
44 pHist2->Fill(gRandom->Gaus(7,1.2));
45 }
46
47 // Create a canvas to be displayed.
48 auto canvas = RCanvas::Create("RLegend example");
49
50 // add palette to canvas, it will not be seen on the canvas but used for colors
51 canvas->Draw<RPaletteDrawable>(RPalette({{0., RColor::kWhite}, {.3, RColor::kRed}, {.7, RColor::kBlue}, {1., RColor::kBlack}}), false);
52
53 // draw first histogram
54 auto draw1 = canvas->Draw(pHist);
55 draw1->line.width = 2.f;
56 draw1->line.color = .3f; // should be red color
57
58 // draw second histogram
59 auto draw2 = canvas->Draw(pHist2);
60 draw2->line.width = 4.f;
61 draw2->line.color = .7f; // should be blue color
62
63 auto legend = canvas->Draw<RLegend>("Legend title");
64 legend->fill.color = RColor(0, 0, 120, 25);
65 legend->fill.style = RAttrFill::k3019;
66 legend->border.color = RColor::kRed;
67 legend->border.width = 2;
68 legend->AddEntry(draw1, "histo1", "l");
69 legend->AddEntry(draw2, "histo2", "l");
70
71 // add custom entry, showing line, fill and marker attributes
72 auto custom = legend->AddEntry("test", "lfm");
73 custom->line.color = RColor::kGreen;
74 custom->line.width = 5.;
75 custom->line.style = RAttrLine::kSolid;
76 custom->fill.color = RColor::kBlue;
77 custom->fill.style = RAttrFill::k3004;
78 custom->marker.color = RColor::kRed;
79 custom->marker.size = 0.03;
80 custom->marker.style = RAttrMarker::kOpenCross;
81
82 canvas->SetSize(1000, 700);
83 canvas->Show();
84}
#define R__LOAD_LIBRARY(LIBRARY)
Definition Rtypes.h:472
R__EXTERN TRandom * gRandom
Definition TRandom.h:62
Objects used to configure the different axis types.
The color class.
Definition RColor.hxx:33
A color palette draw near the frame.
virtual Double_t Gaus(Double_t mean=0, Double_t sigma=1)
Samples a random number from the standard Normal (Gaussian) Distribution with the given mean and sigm...
Definition TRandom.cxx:274
const Int_t n
Definition legend1.C:16