Charts overflow other cells in Firefox

Not sure how to describe this, so here is a picture:

Works fine in Chrome and Safari.
This is the notebook:

There are two problems here.

First, Firefox doesn’t (yet) support ResizeObserver, so there’s no easy way for Observable to tell when a cell resizes. So instead, Observable listens for load events to tell when a cell has resized.

Second, this notebook uses side effects and global DOM: the DC cell selects DOM elements created by other cells (view creates #dc-magnitude-chart etc.), and modifies their contents. (And doesn’t dispatch a load event to tell Observable that it mutated the DOM.)

Combined, Observable isn’t smart enough to detect (in Firefox) that the view cell has changed in height because it was mutated by the DC cell.

To be more idiomatic, this notebook could be rewritten to avoid side effects. Instead of creating the chart container in one cell and modifying it in another cell, create and initialize the chart in the same cell, and then return it. For example:

DC = {
  // Create a skeletal DOM for the charts.
  const view = md`<div class=container>…</div>`;

  // Create and initialize the DC charts.
  const magnitudeChart = dc.barChart(view.querySelector("#dc-magnitude-chart"));
  const depthChart = dc.barChart(view.querySelector("#dc-depth-chart"));
  const timeChart = dc.lineChart(view.querySelector("#dc-time-chart"));
  const dataTable = dc.dataTable(view.querySelector("#dc-table-graph"));

  // Return the DOM container element.
  return view;

Thank you @Evan and @mbostock

I’ve included the fix at — keeping the older version so that it might help people who stumble on this conversation.

The notebook is still a bit buggy in terms of the width of bars, but that’s a DC.js setting I haven’t had time to look up yet. (Feel free to fork and update, I’ll happily hand you the baton :))

Do Firefox support ResizeObserver already ?

I’m having trouble in the chart resize, specially when the wrapper container shrinks. I’m using the Runtime JS to embed. Everything seems fine on Chrome though.

@lucasborges Yes, see ResizeObserver - Web APIs | MDN. Please create a new topic if you still need help, and include a link to your notebook (or a simplified example).

1 Like