I have a D3.js chart on my website.
I'd like it to redraw itself for printing. The only way I know of to do this is to use a combination of window.matchMedia('print')
and 'onbeforeprint' to redraw the chart based on the size of the window, since the browser will make the window the size of the page in that event.
However, D3 does not draw the chart fast enough in Safari and Firefox. The result is that D3 re-renders the charts, but it occurs too late. This does, however, work in Chrome:
let beforePrint = () => {
this.handleResize();
};
let afterPrint = () => {
this.handleResize();
};
if (window.matchMedia) {
window.matchMedia('print').addListener(mql => {
if (mql.matches) {
beforePrint();
} else {
afterPrint();
}
});
}
window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;
Are there any other options for re-drawing D3/SVG charts for printing?