Thanks very much for the example. I have been able to incorporate it into the application I am developing. The only trouble I am having is that I want to have 2 graphs. I have started out by adding a canvas called fEcanvas2 and defining it in a similar way to fEcanvas. However, when I do this, the plotting no longer works properly. I don’t understand what is wrong. Here is my modified code:
#include "TApplication.h"
#include "TGClient.h"
#include "TCanvas.h"
#include "TF1.h"
#include "TRandom.h"
#include "TGButton.h"
#include "TRootEmbeddedCanvas.h"
#include "TH1F.h"
#include "TFormula.h"
#include "TTimer.h"
#include "TGFrame.h"
class MyMainFrame : public TGMainFrame {
private:
TRootEmbeddedCanvas *fEcanvas;
TRootEmbeddedCanvas *fEcanvas2;
TH1F *fH1f;
TTimer *fTimer; // Timer used for update
public:
MyMainFrame(const TGWindow *p, UInt_t w, UInt_t h);
virtual ~MyMainFrame() { }
void DoDraw();
Bool_t HandleTimer(TTimer *);
ClassDef(MyMainFrame, 0)
};
//______________________________________________________________________
MyMainFrame::MyMainFrame(const TGWindow *p,UInt_t w,UInt_t h)
: TGMainFrame(p,w,h), fH1f(0), fTimer(0)
{
// Creates widgets of the example
fEcanvas = new TRootEmbeddedCanvas ("Ecanvas",this,200,200);
AddFrame(fEcanvas, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
10,10,10,1));
fEcanvas2 = new TRootEmbeddedCanvas ("Ecanvas2",this,200,200);
AddFrame(fEcanvas2, new TGLayoutHints(kLHintsExpandX | kLHintsExpandY,
10,10,10,1));
TGHorizontalFrame *hframe=new TGHorizontalFrame(this, 200,40);
TGTextButton *draw = new TGTextButton(hframe,"&Draw");
draw->Connect("Clicked()","MyMainFrame",this,"DoDraw()");
hframe->AddFrame(draw, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
TGTextButton *exit = new TGTextButton(hframe,"&Exit ", "gApplication->Terminate()");
hframe->AddFrame(exit, new TGLayoutHints(kLHintsCenterX,5,5,3,4));
AddFrame(hframe,new TGLayoutHints(kLHintsCenterX,2,2,2,2));
// Sets window name and shows the main frame
SetWindowName("Simple Example");
MapSubwindows();
Resize(GetDefaultSize());
MapWindow();
// create a timer firing every 100 ms
if (!fTimer) fTimer = new TTimer(this, 100);
}
//______________________________________________________________________
void MyMainFrame::DoDraw()
{
// Draws function graphics in randomly choosen interval
TCanvas *c1 = fEcanvas->GetCanvas();
c1->cd(1);
if (!fH1f) {
TFormula *form1 = new TFormula("form1","abs(sin(x)/x)");
TF1 *sqroot = new TF1("sqroot","x*gaus(0) + [3]*form1",0,10);
sqroot->SetParameters(10,4,1,20);
fH1f = new TH1F("h1f","Test random numbers",100,0,10);
fH1f->SetFillColor(45);
}
fH1f->Reset();
fH1f->FillRandom("sqroot",10000);
fH1f->Draw();
// TCanvas::Update() draws the frame, after which one can change it
c1->Update();
fTimer->Reset();
fTimer->TurnOn();
}
//______________________________________________________________________________
Bool_t MyMainFrame::HandleTimer(TTimer *)
{
// timer handling.
TCanvas *c1 = fEcanvas->GetCanvas();
if (fH1f) {
fH1f->Reset();
fH1f->FillRandom("sqroot",10000);
}
c1->Modified();
c1->Update();
fTimer->Reset();
return kTRUE;
}
#ifndef __CINT__
//______________________________________________________________________________
int main(int argc, char *argv[])
{
TApplication theApp( "TestDatePlot", &argc, argv );
new MyMainFrame(gClient->GetRoot(), 800, 600);
theApp.Run();
}
#endif
Could you please tell me what I have done wrong?
Thanks again.