dc-js / dc.js

Multi-Dimensional charting built to work natively with crossfilter rendered with d3.js
Apache License 2.0
7.41k stars 1.81k forks source link

Approach to resize #1853

Closed kum-deepak closed 2 years ago

kum-deepak commented 2 years ago

While checking the resize test folder, I realize that we can simplify the code using ResizeObserver. This considers padding, borders, etc.


          const handleResize = (width, height) => {
            chart.width(width);
            chart.height(width * 0.6);
            chart.configure({ innerRadius: width * 0.14 });
          };

          const withoutTransitions = callback => {
            const oldVal = chart.conf().transitionDuration;
            chart.configure({ transitionDuration: 0 });
            callback();
            chart.configure({ transitionDuration: oldVal });
          };

          const resizeObserver = new ResizeObserver(entries => {
            const entry = entries[0];
            const width = entry.contentRect.width;
            const height = entry.contentRect.height;
            withoutTransitions(() => {
              handleResize(width, height);
              chart.redraw();
            });
          });

          resizeObserver.observe(document.querySelector('#test'));

I will investigate later if this can be used as default in case height and width are not explicitly set.

gordonwoodhull commented 2 years ago

Nods, Safari finally got ResizeObserver last year, so we can get rid of these workarounds.

kum-deepak commented 2 years ago

Thanks, @gordonwoodhull for the quick feedback. I am also thinking of a single callback handling both width and height. This will make sense in most of the cases. On purpose, I picked an example where innerRadius also needs to be adjusted.

I will come back to this after working on the Upgrade guide.

kum-deepak commented 2 years ago

Closed in favor of #1858