图表生成器Stimulsoft Reports.Java示例:在运行时创建具有关系的 告

此示例项目展示了在运行时使用关系和自定义对象数据库创建 告的可能性。

此示例项目展示了在运行时使用关系和自定义对象数据库创建 告的可能性。

首先,创建一个将存储数据的对象类:

public class ObjectCell {public String val;@Overridepublic boolean equals(Object obj) {return val.equals(((ObjectCell) obj).val);}public String toString() {return val;}public ObjectCell(String val) {this.val = val;}}

接下来,创建从 StiDatabase 类扩展的父数据库并在连接事件上填充数据:

public class ParentDatabase extends StiDatabase {public ParentDatabase() {super("Demo.Parent"); // Database name}public void connect(StiDataStoreSource stiDataStoreSource, Boolean fillTable) throws StiException {DataTable dataTable = stiDataStoreSource.createNewTable();for (int i = 0; i < 5; i++) { DataRow dataRow = dataTable.createNewRow(); dataRow.addCell("cId", new ObjectCell("Object" + i)); dataRow.addCell("cName", "Parent cId: " + i); } stiDataStoreSource.setDataTable(dataTable); } public void disconnect() { } public void connect(StiDataStoreSource stiDataStoreSource) throws StiException { connect(stiDataStoreSource, true); } }

接下来,创建相同的子数据库:

public class ChildDatabase extends StiDatabase {public ChildDatabase() {super("Demo.Child"); // Database name}public void connect(StiDataStoreSource stiDataStoreSource, Boolean fillTable) throws StiException {DataTable dataTable = stiDataStoreSource.createNewTable();for (int i = 0; i < 5; i++) { for (int k = 0; k < 5; k++) { DataRow dataRow = dataTable.createNewRow(); dataRow.addCell("cId", new ObjectCell("Object" + i)); for (int j = 1; j < dataRow.getColumns().size(); j++) { // fill row wiht my data dataRow.addCell(dataRow.getColumns().get(j).getColumnName(), "Child cId: " + i + " value: " + j); } } } stiDataStoreSource.setDataTable(dataTable); } public void disconnect() { } public void connect(StiDataStoreSource stiDataStoreSource) throws StiException { connect(stiDataStoreSource, true); } }

要显示 告,我们需要创建 Java 查看器。创建 JFrame,设置必要的选项并添加查看器控件:

public class CreateRelationsReport extends JPanel {private static final long serialVersionUID = 330448692680237867L;private static final Dimension FRAME_SIZE = new Dimension(800, 800);public static void main(final String[] args) {SwingUtilities.invokeLater(new Runnable() {public void run() {try {JFrame frame = new JFrame();frame.add(new CreateRelationsReport(frame));frame.setSize(FRAME_SIZE);frame.setLocationRelativeTo(null);frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setVisible(true);} catch (Throwable e) {StiExceptionProvider.show(e, null);}}});}public CreateRelationsReport(final JFrame parentFrame) throws IOException {setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));setPreferredSize(FRAME_SIZE);StiViewerFx viewerPanel = new StiViewerFx(parentFrame);add(viewerPanel);...

接下来,创建新的 表对象,然后向其中添加父数据库和子数据库:

...StiReport report = new StiReport();StiPage page = new StiPage(report);report.getPages().add(page);page.setName(StiNameCreation.createName(report, StiNameCreation.generateName(page)));report.setDictionary(new StiDictionary(report));report.getDictionary().getDatabases().add(new ParentDatabase());report.getDictionary().getDatabases().add(new ChildDatabase());...

接下来,在对象表源中设置自定义列:

...ListtableSources = new ArrayList();// parentStiDataTableSource tSource = new StiDataTableSource("Demo.Parent", "Parent", "Parent");tSource.setColumns(new StiDataColumnsCollection());tSource.getColumns().add(new StiDataColumn("cId", "cId", StiSystemType.getSystemType("System.Object")));tSource.getColumns().add(new StiDataColumn("cName", "cName", StiSystemType.getSystemType("System.String")));tSource.setDictionary(report.getDictionary());report.getDictionary().getDataSources().add(tSource);tableSources.add(tSource);// childtSource = new StiDataTableSource("Demo.Child", "Child", "Child");tSource.setColumns(new StiDataColumnsCollection());tSource.getColumns().add(new StiDataColumn("cId", "cId", StiSystemType.getSystemType("System.Object")));for (int i = 0; i < 4; i++) { tSource.getColumns().add( new StiDataColumn("cData" + i, "cData" + i, StiSystemType.getSystemType("System.String"))); } tSource.setDictionary(report.getDictionary()); report.getDictionary().getDataSources().add(tSource); tableSources.add(tSource); ...

将带有文本框的标题栏和数据栏添加到 告中:

...// Create TitleBandStiHeaderBand titleBand = new StiHeaderBand();titleBand.setHeight(0.85);titleBand.setName("TitleBand");page.getComponents().add(titleBand);// Create Title text on headerStiText headerText = new StiText(new StiRectangle(0, 0, page.getWidth(), 0.85));headerText.setTextInternal("Tacticdescription");headerText.setHorAlignment(StiTextHorAlignment.Left);headerText.setName("TitleHeader");headerText.setFont(new StiFont("Arial", 12F, StiFontStyle.Bold));titleBand.getComponents().add(headerText);Integer nameIndex = 1;ListdataBands = new ArrayList();for (StiDataTableSource tableSource : tableSources) {// Create DatabandStiDataBand dataBand = new StiDataBand();dataBand.setDataSourceName(tableSource.getName());dataBand.setHeight(0.5);dataBand.setName("DataBand" + nameIndex);nameIndex++;page.getComponents().add(dataBand);double pos = 0;double columnWidth = page.getWidth() / tableSource.getColumns().size();for (StiDataColumn dataColumn : tableSource.getColumns()) {StiText dataText = new StiText(new StiRectangle(pos, 0, columnWidth, 0.5));dataText.setText("{" + tableSource.getName() + "." + dataColumn.getName() + "}");dataText.setName("DataText" + nameIndex.toString());dataText.getBorder().setSide(StiBorderSides.All);dataBand.getComponents().add(dataText);pos = pos + columnWidth;nameIndex++;}dataBands.add(dataBand);}...

告的所有组件都已创建,我们现在可以配置数据关系:

...dataBands.get(1).setDataRelationName("Relation");dataBands.get(1).setMasterComponent(dataBands.get(0));ArrayListcols = new ArrayList();cols.add("cId");StiDataRelation rel = new StiDataRelation("Relation", tableSources.get(0), tableSources.get(1), cols, cols);report.getDictionary().getRelations().add(rel);...

最后,呈现 告并将其显示在 Java 查看器中:

...report.Render();viewerPanel.getStiViewModel().getEventDispatcher().dispatchStiEvent(new StiViewCommonEvent(StiViewCommonEvent.DOCUMENT_FILE_LOADED,new StiDocument(report), null));}

在下面的屏幕截图中,您可以看到示例代码的结果:

 表生成器Stimulsoft Reports.Java示例:在运行时创建具有关系的 告

Aspose、E-iceblue、FastReport、Stimulsoft等文档/ 表图表类开发工具享超低折扣,如有需要可直接联系在线客服。

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2021年7月20日
下一篇 2021年7月20日

相关推荐

发表回复

登录后才能评论