Report in java – Step 4


Fix Width Column and Group by Column

Tiếp theo chúng ta sẻ đi qua 2 đối tượng.

Chúng ta sẻ fix độ rộng của 1 cột nào đó, và phân nhóm các line theo từ group khác nhau.

package thaihoanghai.wordpress.com.demo;

import static net.sf.dynamicreports.report.builder.DynamicReports.col;
import static net.sf.dynamicreports.report.builder.DynamicReports.stl;
import static net.sf.dynamicreports.report.builder.DynamicReports.type;

import java.awt.Color;
import java.io.FileOutputStream;
import java.math.BigDecimal;

import net.sf.dynamicreports.jasper.builder.JasperReportBuilder;
import net.sf.dynamicreports.report.builder.DynamicReports;
import net.sf.dynamicreports.report.builder.column.PercentageColumnBuilder;
import net.sf.dynamicreports.report.builder.column.TextColumnBuilder;
import net.sf.dynamicreports.report.builder.component.Components;
import net.sf.dynamicreports.report.builder.style.StyleBuilder;
import net.sf.dynamicreports.report.constant.HorizontalAlignment;

/**
 * Hello world!
 *
 */
public class App
{
    public static void main( String[] args ) throws Exception
    {
    	// Create variables for work set style for border, background
    	StyleBuilder boldStyle = stl.style().bold();
    	StyleBuilder boldCenteredStyle  = stl.style(boldStyle).setHorizontalAlignment(HorizontalAlignment.CENTER);
    	StyleBuilder columnTitleStyle = stl.style(boldCenteredStyle)
    									   .setBorder(stl.pen1Point())
    									   .setBackgroundColor(new Color(255, 210, 210));

    	/**
    	 * ====================New Code ===============================================================
    	 */

    	TextColumnBuilder<String> itemColumn = col.column("Item", "item", type.stringType()).setStyle(boldStyle);
    	TextColumnBuilder<Integer> quantityColumn = col.column("Quantity", "quantity", type.integerType());
    	TextColumnBuilder<BigDecimal> unitPriceColumn = col.column("Unit Price", "unitprice", type.bigDecimalType());
    	// price = unitPrice * quantity
    	TextColumnBuilder<BigDecimal> priceColumn = unitPriceColumn.multiply(quantityColumn).setTitle("Price");
    	PercentageColumnBuilder pricePercColumn = col.percentageColumn("Price %", priceColumn);
    	TextColumnBuilder<Integer> rowNumberColumn = col.reportRowNumberColumn("No.")
    	/**
    	* ===================New Code=====================
    	*/
    	.setFixedColumns(2)
    	.setHorizontalAlignment(HorizontalAlignment.CENTER);
    	/**
		 * ===================End=====================
		 * */

    	ConnectDB connect = new ConnectDB();
    	// Create new report design
		JasperReportBuilder report = DynamicReports.report();
		report
		.setColumnTitleStyle(columnTitleStyle)// set style for column title
		.highlightDetailEvenRows() // high light for line even
		.columns(rowNumberColumn,itemColumn,quantityColumn,unitPriceColumn,priceColumn,pricePercColumn)
		/**
		 * ===================New Code=====================
		 * */
		.groupBy(itemColumn)
		/**
		 * ===================End=====================
		 * */
		.title(Components.text("Simple Report")// Show Report title
			   .setHorizontalAlignment(HorizontalAlignment.CENTER))// Set position for title
		.pageFooter(Components.pageXofY().setStyle(boldCenteredStyle)) // show number of page at page footer
		// set datasource with specific query from database
		.setDataSource("select id, item, quantity, unitprice from product",connect.openConnect());

		try {
			report.show();
			report.toPdf(new FileOutputStream("d:/report.pdf"));
		} catch (Exception e) {
			e.printStackTrace();
		}
    }

}

Các bạn chú ý các method như sau trong các đoạn New code:

  • setFixedColumns : thiết lập độ rộng cho column

    public T setFixedColumns(Integer columns)
    This method is used to define the fixed width of a column.
    The width is set to the columns multiplied by width of the
    character m for the font used
    Parameters:
    columns – the number of fixed columns >= 0
    Returns:
    a column builder
    Throws:
    IllegalArgumentException – if columns is < 0
  • groupBy : Chúng ta group theo column

    public T groupBy(ValueColumnBuilder<?,?>... groupColumns)
  • groupBy : Chúng ta group theo nhiều column

    public T groupBy(GroupBuilder<?>... groups)

 

Kết quả sau khi run :

d1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: