2015-09-28 10 views
5

當我打印右下框架時,文本some text部分被高圖覆蓋,因爲它們在打印前不調整大小。有沒有一種解決方案可以爲網站配置@media print的打印佈局,並在打印網站時強制highcharts重新繪製/調整容器大小?打印網站時重繪/調整高度圖

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] 
     }] 

    }); 

}); 

JSFiddle

+0

歡迎StackOverflow上。我編輯了您的帖子,以包含來自JSFiddle內聯的代碼。如果鏈接曾經死亡,那麼問題將變得毫無用處,因此將內聯代碼包括在內的問題更適合。 – IKavanagh

+0

非常感謝您 – user3665396

回答

8

使用監聽器將觸發一個圖表和媒體查詢的CSS圖表迴流應設置大小打印網頁時打印。

JS:

$(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] 
     }] 
    }); 

    var printUpdate = function() { 
     $('#container').highcharts().reflow(); 
    }; 

    if (window.matchMedia) { 
     var mediaQueryList = window.matchMedia('print'); 
     mediaQueryList.addListener(function (mql) { 
      printUpdate(); 
     }); 
    } 
}); 

CSS:

@page { 
    size: A4; 
    margin: 0; 
} 
@media print { 
    html, body { 
     padding:0px; 
     margin:0px; 
     width: 210mm; 
     height: 297mm; 
    } 
    #container { 
     float:left; 
     padding: 0px; 
     margin: 0px; 
     width: 80%; 
    } 
} 

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> 

的jsfiddle:爲更好的效果全屏結果http://jsfiddle.net/w4ro5xb7/13/

測試:http://jsfiddle.net/w4ro5xb7/13/show/

+0

Hey Kacper, 它是否支持最新的Chrome版本? 我們試圖在打印之前進行檢測,但它看起來像在Chrome中,當您使用系統「打印」按鈕時,它不會調整大小/重新放置圖表。 – plusz

+0

嗨@plusz - 這裏試試這段代碼(這在Chrome中似乎工作正常):http://jsfiddle.net/uzp1vmLb/35/ –

0

這裏是不同的解決方案的混合,而不需要exporting.js的(添加導出按鈕,將所有的圖表頁...)

大小自動重新調整,打印後復位。

主要是測試在IE

window.onbeforeprint = function() { 
    $(Highcharts.charts).each(function(i,chart){ 
     chart.oldhasUserSize = chart.hasUserSize; 
     chart.resetParams = [chart.chartWidth, chart.chartHeight, false]; 

     var height = chart.renderTo.clientHeight; 
     var width = chart.renderTo.clientWidth; 
     chart.setSize(width, height); 
    }); 
} 
window.onafterprint = function() { 
    $(Highcharts.charts).each(function(i,chart){ 
     chart.setSize.apply(chart, chart.resetParams); 
     chart.hasUserSize = chart.oldhasUserSize; 
    }); 
} 
+0

你測試過哪個版本?我在打印問題上遇到了一些麻煩,但是這個片段在Firefox和Internet Explorer上都不起作用,儘管它執行時沒有錯誤。 IE版本:11.0.96 | Firefox版本:51.0 | Highcharts:3.0 – JorgeGRC