package org.nchc.incident; import java.text.DecimalFormat; import org.zkoss.pivot.Calculator; import org.zkoss.pivot.PivotField; import org.zkoss.pivot.PivotHeaderContext; import org.zkoss.pivot.PivotRenderer; import org.zkoss.pivot.Pivottable; import org.zkoss.pivot.impl.util.Texts; public class CustomPivotRenderer implements PivotRenderer { private DecimalFormat _nnf = new DecimalFormat("##,###"); @Override public int getColumnSize(Pivottable table, PivotHeaderContext columnContext, PivotField field) { int columnSize = 100; // 所有欄位預設欄寬 if (columnContext.isGrandTotal() && field != null) { columnSize = 200; } else { if ("Pointer".equals(field.getTitle())) columnSize = 50; } return columnSize; } @Override public int getRowSize(Pivottable table, PivotHeaderContext rowContext, PivotField field) { return 25; } @Override public String renderCell(Object data, Pivottable table, PivotHeaderContext rowContext, PivotHeaderContext columnContext, PivotField field) { String result = data != null ? _nnf.format(data) : _nnf.format(0); return result; } @Override public String renderField(Object data, Pivottable table, PivotField field) { return field.getType() == PivotField.Type.DATA ? field.getTitle() : data == null ? "(null)" : String.valueOf(data); } @Override public String renderGrandTotalField(Pivottable table, PivotField field) { String result; if (field == null) result = "Grand Total"; else result = "Grand Total of " + field.getTitle(); return result; } @Override public String renderSubtotalField(Object data, Pivottable table, PivotField field, Calculator calculator) { String calLabel; if ("sum".equals(calculator.getType())) { calLabel = "Subtotal"; } else if ("count".equals(calculator.getType())) { calLabel = "Subtotal"; } else { calLabel = Texts.capitalize(calculator.getType()); } String dataLabel = data != null ? data.toString() : "Null"; String result = (new StringBuilder(String.valueOf(dataLabel))).append(" ").append(calLabel).toString(); return result; } }