简体   繁体   English

JasperReports:分组报告元素

[英]JasperReports: Grouping Report Elements

I have ran into 2 situations already that feel like they could be solved if JasperReports had some kind of JRDesignElementGroup . 我已经跑进2分的情况下已经是觉得如果JasperReports的有某种它们可以解决JRDesignElementGroup I've checked the net.sf.jasperreports.engine.design. 我已经检查了net.sf.jasperreports.engine.design. package high and low but can't find anything like it, although I was able to find a JRDesignGroup which looks like it sort of accomplishes what I'm looking for, but I'm not very sure of that. 高低打包,但找不到类似的东西,尽管我能够找到一个看起来很像我想要的JRDesignGroup 东西,但是我不太确定。

Here are the siutations where my issue is cropping up: 这是我的问题浮出水面的几种选择:

(1) Grouping multiple text fields together: (1)将多个文本字段分组在一起:
I'd like to have a report where I can print out the names and values of a bunch of metrics; 我想要一份报告,可以在其中打印出一系列指标的名称和值; something that looks like: 看起来像这样:

Name: John Smith
Email: john.smith@example.com

I was hoping to accomplish this by creating 1 JRDesignElement subclass instance, and adding it to a band in my JasperDesign object. 我希望通过创建1个JRDesignElement子类实例并将其添加到我的JasperDesign对象中的band中来完成此操作。 However, after thinking it over, that setup requires 3 separate JRDesignElement s: 2 JRDesignTextField s (for the metric name and value) and 1 JRDesignStaticText for the colon (":") and space between them. 但是, JRDesignElement考虑之后,该设置需要3个单独的JRDesignElement :2个JRDesignTextField (用于度量标准名称和值)和1个JRDesignStaticText用于冒号(“:”))以及它们之间的空间。

Is there a way to append these 3 items to a group and then just add the group to a band? 有没有一种方法可以将这3个项目附加到组中,然后将组添加到乐队中?

(2) Group an image and its title/caption (2)分组图像及其标题/标题
I also would like to do the same as above, except using JRDesignImage and JRDesignStaticText elements, where the image is an image to be displayed on my report, and the static text will be a title or caption to be placed above the image. 除了使用JRDesignImageJRDesignStaticText元素外,我还想执行上述操作,其中图像是要在我的报表上显示的图像,而静态文本将是放置在图像上方的标题或标题。

Is there any way to append these 2 items to a group and then just add the group to a band? 有什么方法可以将这两个项目添加到组中,然后将组添加到乐队中吗?

If so, can anyone provide JRXML, or even more preferably, some Java examples for how to do this appending? 如果是这样,任何人都可以提供JRXML,或者甚至最好提供一些Java示例来实现此附加吗? And if not, what's the solution/work-around? 如果没有,解决方案/解决方案是什么?

Thanks in advance! 提前致谢!

(1) Grouping multiple text fields together: (1)将多个文本字段分组在一起:

  • The first solution - using single JRDesignTextField element 第一个解决方案-使用单个JRDesignTextField元素
    //Detail
    band = new JRDesignBand();
    band.setHeight(40);

    textField = new JRDesignTextField();
    textField.setX(0);
    textField.setY(0);
    textField.setWidth(200);
    textField.setHeight(40);
    textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    textField.setStyle(normalStyle);
    textField.setMarkup(JRCommonText.MARKUP_HTML);
    expression = new JRDesignExpression();
    expression.setText("\"<b>Name:   </b>\" + $F{Name} + \"<br/><b>Email: </b>\" + $F{Email}");
    textField.setExpression(expression);
    textField.getLineBox().getLeftPen().setLineWidth(1);
    textField.getLineBox().getTopPen().setLineWidth(1);
    textField.getLineBox().getRightPen().setLineWidth(1);
    textField.getLineBox().setLeftPadding(10);
    band.addElement(textField);

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);
  • The second solution - using two JRDesignStaticText and two JRDesignTextField elements together 第二种解决方案-一起使用两个JRDesignStaticText和两个JRDesignTextField元素
    //Detail
    band = new JRDesignBand();
    band.setHeight(40);

    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(0);
    staticText.setWidth(60);
    staticText.setHeight(20);
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setStyle(boldStyle);
    staticText.setText("Name: ");
    staticText.getLineBox().getLeftPen().setLineWidth(1);
    staticText.getLineBox().getTopPen().setLineWidth(1);
    staticText.getLineBox().setLeftPadding(10);
    band.addElement(staticText);

    textField = new JRDesignTextField();
    textField.setX(60);
    textField.setY(0);
    textField.setWidth(200);
    textField.setHeight(20);
    textField.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setText("$F{Name}");
    textField.setExpression(expression);
    textField.getLineBox().getTopPen().setLineWidth(1);
    textField.getLineBox().getRightPen().setLineWidth(1);
    textField.getLineBox().setLeftPadding(10);
    band.addElement(textField);

    staticText = new JRDesignStaticText();
    staticText.setX(0);
    staticText.setY(20);
    staticText.setWidth(60);
    staticText.setHeight(20);
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setStyle(boldStyle);
    staticText.setText("Email: ");
    staticText.getLineBox().getLeftPen().setLineWidth(1);
    staticText.getLineBox().getBottomPen().setLineWidth(1);
    staticText.getLineBox().setLeftPadding(10);
    band.addElement(staticText);

    textField = new JRDesignTextField();
    textField.setStretchWithOverflow(true);
    textField.setX(60);
    textField.setY(20);
    textField.setWidth(200);
    textField.setHeight(20);
    textField.setPositionType(PositionTypeEnum.FLOAT);
    textField.setStyle(normalStyle);
    expression = new JRDesignExpression();
    expression.setText("$F{Email}");
    textField.setExpression(expression);
    textField.getLineBox().getRightPen().setLineWidth(1);
    textField.getLineBox().getBottomPen().setLineWidth(1);
    textField.getLineBox().setLeftPadding(10);
    band.addElement(textField);

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

(2) Group an image and its title/caption (2)分组图像及其标题/标题

Using three JRDesignImage and single JRDesignStaticText elements 使用三个JRDesignImage和单个JRDesignStaticText元素

    band = new JRDesignBand();
    band.setHeight(110);

    String imgPath1 = "\"jasperreports.png\"";
    String imgPath2 = "\"js_logo.png\"";
    int img1Width = 105;
    int img2Width = 200;
    int distance = 20;

    expression = new JRDesignExpression();
    expression.setText(imgPath1);

    JRDesignImage image = new JRDesignImage(jasperDesign);
    image.setX(0);
    image.setY(0);
    image.setWidth(img1Width);
    image.setHeight(26);
    image.setScaleImage(ScaleImageEnum.FILL_FRAME);
    image.setExpression(expression);

    band.addElement(image);

    expression = new JRDesignExpression();
    expression.setText(imgPath2);

    image = new JRDesignImage(jasperDesign);
    image.setX(distance + img1Width);
    image.setY(0);
    image.setWidth(img2Width);
    image.setHeight(87);
    image.setScaleImage(ScaleImageEnum.FILL_FRAME);
    image.setExpression(expression);

    band.addElement(image);

    expression = new JRDesignExpression();
    expression.setText(imgPath1);

    image = new JRDesignImage(jasperDesign);
    image.setX(2*distance + img1Width + img2Width);
    image.setY(0);
    image.setWidth(img1Width);
    image.setHeight(26);
    image.setScaleImage(ScaleImageEnum.FILL_FRAME);
    image.setExpression(expression);

    band.addElement(image);

    JRDesignStaticText staticText = new JRDesignStaticText();
    staticText.setX(200);
    staticText.setY(90);
    staticText.setWidth(60);
    staticText.setHeight(20);
    staticText.setMode(ModeEnum.OPAQUE);
    staticText.setHorizontalAlignment(HorizontalAlignEnum.LEFT);
    staticText.setStyle(boldStyle);
    staticText.setText("The title above images");

    band.addElement(staticText);

    ((JRDesignSection) jasperDesign.getDetailSection()).addBand(band);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM