I’m having a problem embedding a chart from observable - specifically I need to control the width of the chart because it’s too far to the right and getting cut off in the browser (see screenshot below).
When I’m viewing the chart in my observable notebook - the chart itself appears smaller and is aligned to the left of the notebook, but when I embed the chart I have the issue above.
Below is my embed code -
<div id="observablehq-f0b76e48">
<div class="observablehq-viewof-season"></div>
<div class="observablehq-chart"></div>
</div>
<script type="module">
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js";
import define from "https://api.observablehq.com/d/03213a1995d8572c.js?v=3";
(new Runtime).module(define, name => {
if (name === "viewof season") return Inspector.into("#observablehq-f0b76e48 .observablehq-viewof-season")();
if (name === "chart") return Inspector.into("#observablehq-f0b76e48 .observablehq-chart")();
});
</script>
First, it seems like your chart is not completely responsive. Could you use a height proportional to Observable responsive width? such as
height = width*2/3
Then, what are you using for your layout?
Simple flex box container with two columns seems to have the chart aligned to left, besides the non responsive chart.
I think the layout could be causing some of my problem - it’s a hugo theme blog I deploy through R Markdown and probably has it’s own margin next to where the post content appears. I take a look into the theme documentation to find some more details.
The reactive Observable width variable is designed to reflect the width of the entire page. Unfortunately, this means that it doesn’t play too nicely with embedding by default.
I’d recommend that you either embed a version of your notebook that doesn’t rely on width (easy), or use the Observable Runtime API to redefinewidth to be a value that works better for your embed (slightly more work).
<script type="module">
import {Runtime, Inspector} from "https://cdn.jsdelivr.net/npm/@observablehq/runtime@4/dist/runtime.js";
import define from "https://api.observablehq.com/d/4e870377f17fc889.js?v=3";
new Runtime().module(define, name => {
if (name === "chart5") return new Inspector(document.querySelector("#observablehq-chart5-d2b0e014"));
});
</script>
I still didn’t get how I should do it. Any help is appreciated.