# How to superimpose objects in JsROOT canvas?

**URL:** <https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164>\
**Category:** ROOT\
**Tags:** jsroot\
**Created:** [April 29, 2024, 11:06am UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164 "2024-04-29T11:06:43Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Serguei\_Kolos1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/serguei_kolos1/32/27305_2.png) [@Serguei\_Kolos1](https://root-forum.cern.ch/u/Serguei_Kolos1)\
**Post date:** [April 29, 2024, 11:06am UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/1 "2024-04-29T11:06:44Z")

</div>

Hello

Sorry if I missed something obvious. The README file contains an example that shows how to draw multiple objects from the same ROOT file in the same canvas, but I can’t figure out how to do the same for two objects which are taken from different sources, e.g.:

```auto
      import { draw, httpRequest } from 'https://root.cern/js/latest/modules/main.mjs';
      let obj1 = await httpRequest(URL1, 'object');
      let obj2 = await httpRequest(URL2, 'object');

      // draw('drawing', obj, 'hist'); 
      // the line above can draw one object, but how can I draw both obj1 and ob2 in the same 'drawing' div?

```

Cheers

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 29, 2024, 11:14am UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/2 "2024-04-29T11:14:17Z")

</div>

Hi,

In principle, here one uses same logic as in ROOT.  
When calling `obj->Draw()` it just append to the current canvas.

Here if `obj1` and `obj2` are histograms, one can do:

```auto
await draw('drawing', obj1, 'hist');
await draw('drawing', obj2, 'hist same');

```

If second object is primitive like `TLatex`, “same” is not required.

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![Serguei\_Kolos1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/serguei_kolos1/32/27305_2.png) [@Serguei\_Kolos1](https://root-forum.cern.ch/u/Serguei_Kolos1)\
**Post date:** [April 29, 2024, 12:23pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/3 "2024-04-29T12:23:58Z")

</div>

Hi Sergey

Thank you for the quick reply. In fact that was the first thing that I tried out. The first histogram object was successfully drawn, but after I called draw() function second time with ‘hist same’ option I got completely empty blanc canvas. If I pressed the right mouse button anywhere in this canvas I got a drop-down TCanvas menu so the canvas is there but it contains nothing.

UPD: I don’t know if this is related but I got the following error in JS console when drawing second histogram:

```auto
Error funcs.grx is not a function at TypeError: funcs.grx is not a function
    at TH1Painter.drawNormal (TH1Painter.mjs:653:33)
    at TH1Painter.draw1DBins (TH1Painter.mjs:831:19)
    at TH1Painter.mjs:1223:33
    at async ~/:17:7

```

Cheers,  
Serguei

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 29, 2024, 1:56pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/4 "2024-04-29T13:56:41Z")

</div>

Hi Serguei,

I add demo showing usage of “same” draw option. Here is it:

[https://jsroot.gsi.de/dev/demo/draw\_same.htm](https://jsroot.gsi.de/dev/demo/draw_same.htm)

In your case something wrong with the histogram.  
Can you provide JSON code for both histograms?

Regards,  
Sergey

---

<div class="post-metadata">

**Author:** ![Serguei\_Kolos1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/serguei_kolos1/32/27305_2.png) [@Serguei\_Kolos1](https://root-forum.cern.ch/u/Serguei_Kolos1)\
**Post date:** [April 29, 2024, 2:19pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/5 "2024-04-29T14:19:26Z")

</div>

Hi Sergey

Thank you for looking into this. I’m very glad that the feature works and I’ll appreciate if you could tell me what’s wrong with my histograms, which I attached to this message as a tarball (this Web page doesn’t allow me to attach json files (.

[h.tgz](https://root-forum.cern.ch/uploads/short-url/uJGa23iVc4RQ4MrqBS6qPi83EW9.tgz) (4.7 KB)

Cheers,  
Serguei

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 29, 2024, 2:31pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/6 "2024-04-29T14:31:27Z")

</div>

In your JSON files not a histograms but canvases are stored.  
It is not possible to superimpose canvas objects in this way.

Either you request from the server histograms (preferable way) or try to extract histogram  
from list of canvas primitives as:

```auto
const canv1 = await httpRequest('h1.json', 'object');
const canv2 = await httpRequest('h2.json', 'object');
const hist2 = canv2.fPrimitives.arr[1]; // first object is TFrame
await draw('drawing', canv1, '');
await draw('drawing', hist2, 'hist same');

```

---

<div class="post-metadata">

**Author:** ![Serguei\_Kolos1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/serguei_kolos1/32/27305_2.png) [@Serguei\_Kolos1](https://root-forum.cern.ch/u/Serguei_Kolos1)\
**Post date:** [April 29, 2024, 3:00pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/7 "2024-04-29T15:00:35Z")

</div>

Many thanks, this works like a charm.  
May I ask you one more question. I want to display in a Web page a few histograms, which are being constantly updated, so I’m planing to redraw them periodically. If the user of the Web page changes some visualisation parameters, e.g. set the log scale to one of the axes, changed position of the stat box, increased font size, etc., is there a way to preserve all these changes when drawing a new version of the same histogram?

---

<div class="post-metadata">

**Author:** ![linev](https://root-forum.cern.ch/letter_avatar_proxy/v4/letter/l/2bfe46/32.png) [@linev](https://root-forum.cern.ch/u/linev)\
**Post date:** [April 29, 2024, 3:13pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/8 "2024-04-29T15:13:55Z")

</div>

One can use `redraw()` function provided by JSROOT.  
But you should work with histograms - not with canvases.

In case of single histogram it is very simple. One can periodically call:

```auto
const hist1 = await httpRequest(JSON1, 'object');
redraw('drawing', hist1);

```

In such case all interactive changes (like zooming or draw options) will be preserved.  
[Here is simple demo](http://root.cern/js/latest/demo/update_draw.htm)

If you want to superimpose several histograms and update them - create `THStack` object and draw/redraw it. This is most easy way.

If you continue work with `TCanvas`, you may loose most of interactive changes by calling `redraw`

---

<div class="post-metadata">

**Author:** ![Serguei\_Kolos1](https://root-forum.cern.ch/user_avatar/root-forum.cern.ch/serguei_kolos1/32/27305_2.png) [@Serguei\_Kolos1](https://root-forum.cern.ch/u/Serguei_Kolos1)\
**Post date:** [April 29, 2024, 3:56pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/9 "2024-04-29T15:56:19Z")

</div>

Great! Thank you for a very comprehensive explanation. I’ll give it a try.

Cheers,  
Serguei

---

<div class="post-metadata">

**Author:** ![system](https://root-forum.cern.ch/uploads/default/original/3X/e/f/efe4f24398fd8ffeb76bccb6ecc46d9d9c003376.png) [@system](https://root-forum.cern.ch/u/system)\
**Post date:** [May 13, 2024, 3:56pm UTC](https://root-forum.cern.ch/t/how-to-superimpose-objects-in-jsroot-canvas/59164/10 "2024-05-13T15:56:37Z")

</div>

Closed on May 13, 2024, 3:56pm UTC

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