Report in java – step 5


Sum Group – Sum Columns

Hôm nay mình sẻ tiếp tục phần step 5. Thể hiện việc tình tổng giá trị trong 1 group or tổng giá trị trong column .

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 static net.sf.dynamicreports.report.builder.DynamicReports.sbt;

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.")
    	.setFixedColumns(2)
    	.setHorizontalAlignment(HorizontalAlignment.CENTER);

    	ConnectDB connect = new ConnectDB();
    	// Create new report design 
		JasperReportBuilder report = DynamicReports.report();
		report
		.setColumnTitleStyle(columnTitleStyle)// set style for column title
		.setSubtotalStyle(boldStyle)
		.highlightDetailEvenRows() // high light for line even
		.columns(rowNumberColumn,itemColumn,quantityColumn,unitPriceColumn,priceColumn,pricePercColumn)
		.groupBy(itemColumn)
		//==========================New Code======================
		.subtotalsAtSummary(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))
		.subtotalsAtFirstGroupFooter(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))
		//==========================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();
		}
    }

}

Hjx nói thật mình tìm không thấy được document cho phần này nửa
.subtotalsAtSummary(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))
.subtotalsAtFirstGroupFooter(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))

// Giải thích đơn giản như sau .
subtotalsAtSummary : được thực hiện khi groupBy được thự hiện. Nó tính tổng các giá trị trong column của group và hiển thị giá trị cuối group.
subtotalsAtFirstGroupFooter : thực hiện việc tính tổng các giá trị trong colum hiển thị cuối report

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: