在JavaScript中打印图表报表通常涉及以下步骤:
setTimeout
延迟打印。<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>打印图表示例</title>
<script src="https://cdn.jsdelivr.net/npm/echarts/dist/echarts.min.js"></script>
</head>
<body>
<div id="chart" style="width: 600px; height: 400px;"></div>
<button onclick="printChart()">打印图表</button>
<script>
var myChart = echarts.init(document.getElementById('chart'));
var option = {
title: {
text: '示例图表'
},
tooltip: {},
xAxis: {
data: ['A', 'B', 'C', 'D', 'E']
},
yAxis: {},
series: [{
name: '销量',
type: 'bar',
data: [5, 20, 36, 10, 10]
}]
};
myChart.setOption(option);
function printChart() {
var chartContainer = document.getElementById('chart');
var printWindow = window.open('', '', 'height=400,width=600');
printWindow.document.write('<html><head><title>打印图表</title></head><body>');
printWindow.document.write(chartContainer.outerHTML);
printWindow.document.write('</body></html>');
printWindow.document.close();
printWindow.print();
}
</script>
</body>
</html>
通过以上步骤和示例代码,你可以在JavaScript中实现图表的打印功能。
领取专属 10元无门槛券
手把手带您无忧上云