Added 'print' button to print the current chart

This commit is contained in:
Przemek Wiech
2019-02-21 22:38:22 +01:00
parent 61b65eb1ac
commit a920147ba8
5 changed files with 70 additions and 5 deletions

View File

@@ -133,4 +133,27 @@ export class Chart extends React.PureComponent<ChartProps, {}> {
</div>
);
}
/** Shows the print dialog to print the currently displayed chart. */
print() {
const printWindow = document.createElement('iframe');
printWindow.style.position = 'absolute';
printWindow.style.top = '-1000px';
printWindow.style.left = '-1000px';
printWindow.onload = () => {
const svg = document.getElementById('chart')!.cloneNode(true) as Element;
svg.removeAttribute('transform');
const contents = svg.outerHTML;
printWindow.contentDocument!.open();
printWindow.contentDocument!.write(contents);
printWindow.contentDocument!.close();
// Doesn't work on Firefox without the setTimeout.
setTimeout(() => {
printWindow.contentWindow!.focus();
printWindow.contentWindow!.print();
printWindow.parentNode!.removeChild(printWindow);
}, 500);
};
document.body.appendChild(printWindow);
}
}