TMultigraph only works when I hover over it with the mouse

building a multigraph together with other separated graphs like this :

void Graph::make_graph()
{

    gr4 = std::make_shared<TGraph>();
    gr4->SetLineColorAlpha(kGreen - 2, 0.8);
    canvas->cd(2);
    gr4->Draw("al");
}
void Graph::make_multigraph()
{
    mg = std::make_shared<TMultiGraph>();

    gr1 = std::make_shared<TGraph>();
    gr1->SetLineColorAlpha(kGreen - 2, 0.8);
    gr1->SetLineWidth(2);

    gr2 = std::make_shared<TGraph>();
    gr2->SetLineColorAlpha(kBlue - 2, 0.8);

    gr3 = std::make_shared<TGraph>();
    gr3->SetLineColorAlpha(kRed - 2, 0.8);
    gr3->SetLineWidth(2);

    mg->Add(gr1.get());
    mg->Add(gr2.get());
    mg->Add(gr3.get());

    canvas->cd(1);
    mg->Draw("al");
}
void Graph::paint_graphs()
{

    for (int cindex = 0; cindex < buffer.size(); cindex++)
    {
        // std::cout << cindex << std::endl;

        mg->GetXaxis()->SetRange(0, buffer.size());
        mg->GetXaxis()->SetLimits(0, buffer.size());

        gr1->SetPoint(cindex, cindex, buffer[cindex].b);
        gr2->SetPoint(cindex, cindex, buffer[cindex].price_average);
        gr3->SetPoint(cindex, cindex, buffer[cindex].a);

        gr4->SetPoint(cindex, cindex, buffer[cindex].agg_updates);
    }
}
void Graph::update_canvas()
{
    canvas->cd(1);
    canvas->cd(2);
    canvas->Update();
    canvas->Modified();
    gSystem->ProcessEvents();
}
void Graph::run_grapher()
{
    Graph grapher;
    grapher.sampler_o = sampler_o;
    grapher.make_multigraph();
    grapher.make_graph();
    // grapher.make_hist2d();

    while (true)
    {
        grapher.get_data();
        grapher.paint_graphs();
        grapher.update_canvas();
    }
}

This makes one multigraph and one regular graph, and it works as usual, meaning both get updated but the multigraph only works when I hover the mouse over it , while the other regular graph does work normally ? What I’m missing on the multigraph ? Thanks

your update_canvas method should make it. May be replace:

    canvas->cd(1);
    canvas->cd(2);

by:

   canvas->cd(0);

now I have to hover over both of them for them to get updated, so that seems not to make it … it’s the first time I see this behavior

Can you provide a self-contained script I can copy/paste or download in one go?

Ok, I made a runnable script run_grapher.C:

TCanvas *canvas;
TGraph *gr1, *gr2, *gr3, *gr4;
TMultiGraph *mg;

void make_graph()
{
   gr4 = new TGraph();
   gr4->SetLineColorAlpha(kGreen - 2, 0.8);

}

void make_multigraph()
{
   mg = new TMultiGraph();

   gr1 = new TGraph();
   gr1->SetLineColorAlpha(kGreen - 2, 0.8);
   gr1->SetLineWidth(2);

   gr2 = new TGraph();
   gr2->SetLineColorAlpha(kBlue - 2, 0.8);

   gr3 = new TGraph();
   gr3->SetLineColorAlpha(kRed - 2, 0.8);
   gr3->SetLineWidth(2);

   mg->Add(gr1);
   mg->Add(gr2);
   mg->Add(gr3);


}

void fill_graphs()
{
   for (int cindex = 0; cindex < 10; cindex++) {
      gr1->SetPoint(cindex, cindex, cindex);
      gr2->SetPoint(cindex, cindex, cindex*cindex);
      gr3->SetPoint(cindex, cindex, cindex*cindex*cindex);
      gr4->SetPoint(cindex, cindex, 2*cindex);
   }
}

void paint_graphs()
{
   canvas->cd(1);
   mg->Draw("al");
   canvas->cd(2);
   gr4->Draw("al");
}

void update_canvas()
{
    canvas->cd(1);
    canvas->cd(2);
    canvas->Update();
    canvas->Modified();
    gSystem->ProcessEvents();
}

void run_grapher()
{
   canvas = new TCanvas();
   canvas->Divide(2,1);
   make_graph();
   make_multigraph();
   fill_graphs();
   paint_graphs();
   update_canvas();
}

It works for me. I do not need to update the canvas with the mouse.

1 Like

hey sorry, I just came back. Maybe it has something to do that I am making the graphs shared pointers as class members ? This is how the class looks :

class Graph
{
public:
    Graph() : theApp("App", 0, 0), canvas(std::make_shared<TCanvas>("canvas", "book", 200, 10, 2048, 1080)),buffer(1000)
    {
        gStyle->SetCanvasPreferGL(kTRUE);

        canvas->Divide(1, 3);
    };

    void update_canvas();
    void make_hist2d();
    void make_multigraph();
    void get_data();
    void paint_graphs();
    void make_graph();

    static void run_grapher(std::shared_ptr<SocketInterface> sampler_o);

private:
    //buffer
    boost::circular_buffer<sample> buffer;
    // graph pointers
    std::shared_ptr<TMultiGraph> mg;
    std::shared_ptr<TGraph> gr1;
    std::shared_ptr<TGraph> gr2;
    std::shared_ptr<TGraph> gr3;
    std::shared_ptr<TGraph> gr4;
    //
    std::shared_ptr<SocketInterface> sampler_o;
    TApplication theApp;
    std::shared_ptr<TCanvas> canvas;
    jsontools::CommonTools ojson;
};

Can you please send a consistent script I can run ?
like:

root[0] .x run_grapher.C

Thanks in advance.

Drawing after filling made it. I don’t get it, since I always placed draws when instantiating the graphs and I only had to fill & update after that … thanks

sorry for re-opening this, but how can I remove the right margin seen on this multigraph ?

thanks !

Can you post the script reproducing this?

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.