Hi,
I have a working chart (the details are not important) whose structure starts as
LinePlot = {
const container = html`<div class="tooltip-demo">
<style>
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
//const container = Container()
const width = 500
const height = 100
const svg = d3
.select(container)
.append("svg")
.attr("viewBox", `0 0 ${width} ${height}`);
....
However, I would like to modularize it. This was done in two ways. First with a block, then with a function. In the function case, everything works fine. I define:
function Container() {
const container = html`<div class="tooltip-demo">
<style>
/*
* Be sure to set the tooltip's DOM element's styles!
*/
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
return container
}
so that the original block becomes
LinePlot = {
const container = Container()
const width = 500
const height = 100
...
return container
with no other changes. Everything is clean and the chart is unmodified in all respects, as I expected. However, the following approach did not work:
Container = {
const container = html`<div class="tooltip-demo">
<style>
/*
* Be sure to set the tooltip's DOM element's styles!
*/
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
return container
}
This is a simple block. I expected the contents of this block to be isolated from other blocks, but this did not appear to be the case. The LinePlot
block is now:
Container = {
const container = html`<div class="tooltip-demo">
<style>
div.tooltip-demo > div.tooltip {
position: fixed;
display: none;
padding: 12px 6px;
background: #fff;
border: 1px solid #333;
pointer-events: none;
}
</style>
</div>`;
return container
}
The image appears on top of the Container
block, and above the LineChart
block, I get
I do not understand how this is possible. While Observable is reactive, I thought the block contents were isolated for each other. How is it that my plot shows up over the Container
block? Until I understand this, it will be difficult to make progress in my modularization and encapsulation efforts.
Thanks for any advice on this. Gordon.