When I'm printing the bottom right frame the text some text
is partly covered by the highcharts because they don't resize before printing. Is there a solution to configure a printing layout with @media print
for the website and force highcharts to redraw/resize to container size when website is printed?
HTML
<script src="http://code.highcharts.com/highcharts.js"></script>
<script src="http://code.highcharts.com/modules/exporting.js"></script>
<div id="container" style="height: 400px"></div>
<div id="container" style="height: 400px"></div>
JavaScript
$(function () {
Highcharts.setOptions({ // Apply to all charts
chart: {
events: {
beforePrint: function () {
this.oldhasUserSize = this.hasUserSize;
this.resetParams = [this.chartWidth, this.chartHeight, false];
this.setSize(600, 400, false);
},
afterPrint: function () {
this.setSize.apply(this, this.resetParams);
this.hasUserSize = this.oldhasUserSize;
}
}
}
});
$('#container').highcharts({
title: {
text: 'Rescale to print'
},
subtitle: {
text: 'Click the context menu and choose "Print chart".<br>The chart size is set to 600x400 and restored after print.'
},
xAxis: {
categories: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
},
series: [{
data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]
}]
});
});
Here is a mix of different solutions, with no need of exporting.js (which adds exporting button to all graphs on page...)
The size is automatically rescaled, then reset after print.
Tested mostly on IE
UPDATE: I now use a different version, fixing the size of each chart instead of using clientWidth. It works on the 3 browsers I tested (IE 11, FF & Chrome)
Taking these suggestions, I wanted to offer a 3rd approach that should solve some of the shortcomings of the previous answers in conjunction with other solutions I have seen elsewhere, resulting in what worked for me (that the above did not).
Setting
window.onbeforeprint
is fine, unless you have otheronbeforeprint
handlers and don't want to blow them away.window.addEventListener('onbeforeprint', function)
allows you to both set numerous separate event listeners, AND remove them when you're done. Additionally, Safari does not support these specific event listeners anyways, so it is recommended to usewindow.matchMedia
instead, which is supported on all evergreen browsers.Update:
Runnable example sans jQuery because it's 2020. Use the "expand snippet" link to test the print scenario.
Using listener that will trigger reflow for a chart and media queries in CSS chart should be printed in set size when printing a website.
JS:
CSS:
HTML:
JSFiddle: http://jsfiddle.net/w4ro5xb7/13/
Test in full screen result for better effect: http://jsfiddle.net/w4ro5xb7/13/show/