创建包含纵向和横向页面的报告
此示例显示如何创建包含 11 x 8.5 英寸横向页面和 8.5 x 11 英寸纵向页面的报告。该报告使用横向页面来容纳纵向页面无法容纳的太宽的内容,例如这些并排的图像。
导入 DOM 和报告 API 包,这样您就不必使用长类名。
import mlreportgen.dom.*; import mlreportgen.report.*;
创建一个容器来保存报告内容。
% To create a Word report, change the output type from "pdf" to "docx". rpt = mlreportgen.report.Report("PortraitAndLandscapeReport", "pdf");
创建一个章节,标题为“具有随机噪声的余弦值图类型”。
chapter = Chapter("Title", "Types of Cosine Value Plots with Random Noise");
将章节横向布局设置为 true。这会将整个章节布局设置为横向。
chapter.Layout.Landscape = true;
创建要绘图的变量。将 x 创建为 0 到 3pi 之间的 200 个等距值。使用随机噪声创建 y 作为余弦值。
x = linspace(0,3*pi,200); y = cos(x) + rand(1,200);
创建 x 和 y 值的图窗对象:条形图(图 1)、散点图(图 2)和二维线图(图 3)。
创建包裹图窗快照图像文件的图像对象。设置图窗对象的比例以使其适合表格条目。
imgStyle = {ScaleToFit(true)}; fig1 = Figure(bar(x, y)); fig1Img = Image(getSnapshotImage(fig1, rpt)); fig1Img.Style = imgStyle; delete(gcf); fig2 = Figure(scatter(x,y));
fig2Img = Image(getSnapshotImage(fig2, rpt)); fig2Img.Style = imgStyle; delete(gcf); fig3 = Figure(plot(x,y)); fig3Img = Image(getSnapshotImage(fig3, rpt)); fig3Img.Style = imgStyle; delete(gcf);
将图像插入 1x5 不可见布局表(lo_table)的唯一行中(图窗之间有 2 个空表格条目作为空间)。如果未定义表格的边框或其中的任何表格条目,则认为表格是看不见的。仅当指定了表格条目的高度和宽度时,图像的大小才会适合表格条目。
lo_table = Table({fig1Img, ' ', fig2Img, ' ',fig3Img}); lo_table.entry(1,1).Style = {Width('3.2in'), Height('3in')}; lo_table.entry(1,2).Style = {Width('.2in'), Height('3in')}; lo_table.entry(1,3).Style = {Width('3.2in'), Height('3in')}; lo_table.entry(1,4).Style = {Width('.2in'), Height('3in')}; lo_table.entry(1,5).Style = {Width('3in'), Height('3in')};
将表格添加到章节中,将章节添加到报告中。
add(chapter, lo_table); add(rpt, chapter);
创建一个标题为“表面图”的章节。章节的默认布局是纵向的。
chapter1 = Chapter("Title", "Surface Plot");
为表面图创建一个图窗对象(图 4)。创建包裹图窗快照图像文件的图像对象。
fig4 = Figure(surf(peaks(20))); fig4Img = Image(getSnapshotImage(fig4, rpt)); fig4Img.Style = imgStyle; delete(gcf);
将生成的图像对象添加到章节中,并将章节添加到报告中。
add(chapter1, fig4Img); add(rpt, chapter1);
生成并显示报告
close(rpt); rptview(rpt);
生成的报告包括横向页面上的并排图窗快照和纵向页面上的表面图。