Drawing legend zooms out x-axis to start at 0; how to reset it?

I am using JSROOT to display several TMultiGraphs online. Without legends, they look ok, but I need legends. I am reading the multigraphs from a root file and constructing the legends in JS.

All of these multigraphs have x-values starting above zero. Without the legends, the autorange works nicely and the graph automatically shows a sensible x-axis range. When I started to add legends, this changed unexpectedly.

I find that when I draw each multigraph before its legend, then just the first multigraph gets its x-axis range reset to start at 0. However if I draw each legend before its multigraph, then all of the multigraphs unexpectedly have their x-axis reset to start at 0.

It looks like the TGraphPainter’s xmin is getting overwritten but Idk if that is my fault or JSROOT’s.

How can I reset the x-axis to my preferred values?

The JSROOT example on zooming didn’t seem to be directly applicable to multigraphs. Also it was a bit too complicated. I would really like an example to modify that does something like GetXaxis()->SetRangeUser(3,5), if anyone has a code snippet to share, I’d be grateful.

Dear Naomi,

Thanks for the post and welcome to the ROOT Community!

I would defer this very specific question to the JSROOT expert, @linev

Cheers,
D

1 Like

Thanks! After a hard reload it works as intended with the 2 lines in this order:

let gpainter = await draw(divname, obj[i], 'ap;gridx;gridy;');     
if (drawlegend) await draw(divname,leg[i]);

If I swap the lines, then the x-axes all start from 0.

I would still like to know how to specify the x-axis limits, please.

Hi,

Are you drawing graphs or multigraphs?
You trying to overlap several of them?

When creating TMultiGraphs, you can specify user range in ROOT macro as:

   auto mg = new TMultiGraph();
   mg->Add(gr1);
   mg->Add(gr2);
   mg->GetXaxis()->SetRangeUser(0, 0.8);

You also can change zoom in JSROOT after multigraph and legend are drawn.
Just add at the end of your code:

gpainter.getFramePainter().zoom(0, 0.8);

To create legend for your drawings you can try to use drawLegend method as:

gpainter.getPadPainter().drawLegend();