Report in java – step 6


Chart

Trong step 6 hôm nay chúng ta cùng nhau quay lại khi chúng ta tính sum của bài trước theo từng group. Chúng ta củng có thể hiện thực điều này trên  CHART.

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

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.chart.Bar3DChartBuilder;
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;

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);

    	//====================================New Code==========================================================
    	Bar3DChartBuilder itemChart = cht.bar3DChart()
    			.setTitle("Sales by item")
    			.setCategory(itemColumn)
    			.addSerie(cht.serie(unitPriceColumn),cht.serie(priceColumn));

    	Bar3DChartBuilder itemChar2 = cht.bar3DChart()
    			.setTitle("Sales by item")
    			.setCategory(itemColumn)
    			.setUseSeriesAsCategory(true) // 1 building scheme compares all fields
    			.addSerie(cht.serie(unitPriceColumn),cht.serie(priceColumn));
    	//====================================End==========================================================    

    	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)
		.subtotalsAtSummary(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))
		.subtotalsAtFirstGroupFooter(sbt.sum(unitPriceColumn),sbt.sum(priceColumn))
		.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
		.summary(itemChart,itemChar2)
		// 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();
		}
    }

}

Document new code : Updating………………….
Giải thich đôi chút về vấn đề :
.setCategory(itemColumn)
.addSerie(cht.serie(unitPriceColumn),cht.serie(priceColumn));
2 method này sẻ chỉ định column sẻ được vẽ là itemColumn, và giá trị thể hiện cho từng column là unitPriceColumn vs priceColumn.

.setCategory(itemColumn)
.setUseSeriesAsCategory(true) // 1 building scheme compares all fields
.addSerie(cht.serie(unitPriceColumn),cht.serie(priceColumn));
3 mehthod này thể hiện sự trái ngược so với phía trên. khi chúng ta set Use Series là dòng data để vẽ. như vậy chúng ta sẻ vẽ UnitPriceColumn vs PriceColumn và giá trị thể hiện cho từng column trong lược đồ sẻ là itemcolumn

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: