Saturday, December 10, 2022

Zkoss Framework Pivottable

 


Source :

1. File Zul :

<?page title="ZK Pivottable: Airline Agency Sales" ?>
<zk>
    <window apply="demo.app.pivottable.PivotDemoBaseController" hflex="max">
        <hlayout id="preDef" spacing="5px" sclass="z-valign-middle">Predefined scenario :</hlayout>
        <separator height="20px" />
        <pivottable id="pivot" hflex="max">
            <div></div>
            <div>Columns</div>
            <div>Rows</div>
        </pivottable>
        <separator height="20px" />
        <groupbox closable="false" mold="3d">
            <caption label="Field Control" />
            <vlayout style="padding: 10px">
                <pivot-field-control id="pfc" height="300px" />
                <separator />
                <hlayout spacing="10px" sclass="z-valign-middle">
                    Data field orientation :
                    <radiogroup id="dataOrient">
                        <vlayout>
                            <radio id="colOrient" label="column" />
                            <radio id="rowOrient" label="row" />
                        </vlayout>
                    </radiogroup>
                    <checkbox id="colGrandTotal" label="Enable grand total for columns" />
                    <checkbox id="rowGrandTotal" label="Enable grand total for rows" />
                    <checkbox id="autowrap" label="Enable Autowrap" />
                </hlayout>
            </vlayout>
        </groupbox>
    </window>
</zk>


2. Controller :

2.1. pivotbasecontroller.java

package demo.app.pivottable;
 
import java.text.SimpleDateFormat;
import java.util.Date;
 
import org.zkoss.pivot.Pivottable;
import org.zkoss.pivot.impl.TabularPivotModel;
import org.zkoss.pivot.ui.PivotFieldControl;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.event.CheckEvent;
import org.zkoss.zk.ui.event.Event;
import org.zkoss.zk.ui.event.EventListener;
import org.zkoss.zk.ui.select.SelectorComposer;
import org.zkoss.zk.ui.select.annotation.Listen;
import org.zkoss.zk.ui.select.annotation.Wire;
import org.zkoss.zul.Button;
import org.zkoss.zul.Checkbox;
import org.zkoss.zul.Hlayout;
import org.zkoss.zul.Radio;
import org.zkoss.zul.Vlayout;
 
public class PivotDemoBaseController extends SelectorComposer<Component> {
 
    private static final long serialVersionUID = 1L;
    @Wire
    private Pivottable pivot;
    @Wire
    private PivotFieldControl pfc;
    @Wire
    private Checkbox colGrandTotal, rowGrandTotal;
    @Wire
    private Radio colOrient, rowOrient;
    @Wire
    private Vlayout rawDataLayout;
    @Wire
    private Hlayout preDef;
 
    private TabularPivotModel pivotModel;
 
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
 
        StaticPivotModelFactory pmf = StaticPivotModelFactory.INSTANCE;
        pivotModel = pmf.build();
        pivot.setModel(pivotModel);
        pfc.setModel(pivotModel);
        loadConfiguration(pmf.getDefaultConfigurator());
 
        // load predefined scenario
        for (PivotConfigurator conf : pmf.getConfigurators())
            preDef.appendChild(getPreDefDiv(conf));
    }
 
    @Listen("onCheck = #colGrandTotal")
    public void enableColumnGrandTotal(CheckEvent event) {
        pivot.setGrandTotalForColumns(event.isChecked());
    }
 
    @Listen("onCheck = #rowGrandTotal")
    public void enableRowGrandTotal(CheckEvent event) {
        pivot.setGrandTotalForRows(event.isChecked());
    }
 
    @Listen("onCheck = #dataOrient")
    public void enableDataOrient(CheckEvent event) {
        pivot.setDataFieldOrient(((Radio) event.getTarget()).getLabel());
    }
 
    @Listen("onCheck = #autowrap")
    public void enableAutowrap(CheckEvent event) {
        pivot.setAutowrap(event.isChecked());
    }
     
    private void initControls() {
        // grand totals
        colGrandTotal.setChecked(pivot.isGrandTotalForColumns());
        rowGrandTotal.setChecked(pivot.isGrandTotalForRows());
 
        // data orientation
        ("column".equals(pivot.getDataFieldOrient()) ? colOrient : rowOrient).setChecked(true);
 
        pfc.syncModel(); // field control
    }
 
    private String renderRawData(Object object, String fname) {
        if ("Agent".equals(fname) || "Customer".equals(fname)) {
            String[] names = ((String) object).split(" ", 2);
            return Character.toUpperCase(names[0].charAt(0)) + ". " + names[1];
        } else if ("Date".equals(fname)) {
            SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
            return format.format((Date) object);
        }
        return object == null ? "(null)" : object.toString();
    }
 
    private Component getPreDefDiv(final PivotConfigurator conf) {
        Button scenarioBtn = new Button(conf.getTitle());
        scenarioBtn.setSclass("predef");
        scenarioBtn.addEventListener("onClick", new EventListener<Event>() {
            public void onEvent(Event event) throws Exception {
                loadConfiguration(conf);
            }
        });
        return scenarioBtn;
    }
 
    private void loadConfiguration(PivotConfigurator conf) {
        pivotModel.clearAllFields(true);
        conf.configure(pivotModel);
        conf.configure(pivot);
        pivot.setPivotRenderer(conf.getRenderer());
        initControls();
    }
 
}


2.2. pivotconfigurator.java

package demo.app.pivottable;
 
import org.zkoss.pivot.PivotRenderer;
import org.zkoss.pivot.Pivottable;
import org.zkoss.pivot.impl.TabularPivotModel;
 
public abstract class PivotConfigurator {
 
    private final String title;
 
    public PivotConfigurator(String title) {
        this.title = title;
    }
 
    public String getTitle() {
        return title;
    }
     
    public abstract void configure(TabularPivotModel model);
 
    public abstract void configure(Pivottable table);
 
    public abstract PivotRenderer getRenderer();
}


2.3. staticpivotmodelfactory.java

package demo.app.pivottable;
 
import java.text.SimpleDateFormat;
import java.util.Comparator;
import java.util.Date;
 
import org.zkoss.pivot.GroupHandler;
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.SimplePivotRenderer;
import org.zkoss.pivot.impl.StandardCalculator;
import org.zkoss.pivot.impl.TabularPivotModel;
 
public class StaticPivotModelFactory {
 
 
    public static final StaticPivotModelFactory INSTANCE = new StaticPivotModelFactory();
 
    private StaticPivotModelFactory() {
    }
 
    public TabularPivotModel build() {
        return new TabularPivotModel(PivotData.getData(), PivotData.getColumns());
    }
 
    // configurator //
 
    public PivotConfigurator getDefaultConfigurator() {
        return CONFIG_CITY_SALES;
    }
 
    public PivotConfigurator[] getConfigurators() {
        return new PivotConfigurator[] { CONFIG_PERFORMANCE, CONFIG_CITY_SALES, CONFIG_SALES_RACE };
    }
 
    public static final PivotConfigurator CONFIG_PERFORMANCE = new PivotConfigurator("Performance") {
        public void configure(TabularPivotModel model) {
            model.setFieldType("Airline", PivotField.Type.COLUMN);
            model.setFieldType("Flight", PivotField.Type.COLUMN);
            model.setFieldType("Agent", PivotField.Type.ROW);
            model.setFieldType("Customer", PivotField.Type.ROW);
            model.setFieldType("Price", PivotField.Type.DATA);
            model.setFieldType("Mileage", PivotField.Type.DATA);
 
            model.setFieldSubtotals("Airline", new StandardCalculator[] {
                    StandardCalculator.AVERAGE, StandardCalculator.COUNT
            });
            model.setFieldSubtotals("Agent", new StandardCalculator[] {
                    StandardCalculator.AVERAGE, StandardCalculator.COUNT
            });
        }
 
        public void configure(Pivottable table) {
            table.setDataFieldOrient("column");
        }
         
        public PivotRenderer getRenderer() {
            return null;//use default
        }
    };
 
    public static final PivotConfigurator CONFIG_CITY_SALES = new PivotConfigurator("Sales by City") {
        public void configure(TabularPivotModel model) {
            model.setFieldType("Origin", PivotField.Type.COLUMN);
            model.setFieldType("Destination", PivotField.Type.COLUMN);
            model.setFieldType("Airline", PivotField.Type.ROW);
            model.setFieldType("Flight", PivotField.Type.ROW);
            model.setFieldType("Customer", PivotField.Type.DATA);
            model.setFieldType("Price", PivotField.Type.DATA);
        }
 
        public void configure(Pivottable table) {
            table.setDataFieldOrient("row");
        }
         
        public PivotRenderer getRenderer() {
            return null;//use default
        }
    };
 
    public static final PivotConfigurator CONFIG_SALES_RACE = new PivotConfigurator("Sales Race!") {
        public void configure(TabularPivotModel model) {
            model.setFieldType("Agent", PivotField.Type.COLUMN);
            model.setFieldType("Date", PivotField.Type.ROW);
            model.setFieldType("Customer", PivotField.Type.DATA);
            model.setFieldType("Price", PivotField.Type.DATA);
 
            // sort by last name, then first name
            model.setFieldKeyComparator("Agent", new Comparator<Object>() {
                public int compare(Object k1, Object k2) {
                    String s1 = (String) k1;
                    String s2 = (String) k2;
                    int i1 = s1.lastIndexOf(' ');
                    int i2 = s2.lastIndexOf(' ');
                    int cmp = s1.substring(i1 + 1).compareTo(s2.substring(i2 + 1));
                    if (cmp != 0)
                        return cmp;
                    String fname1 = i1 < 0 ? "" : s1.substring(0, i1).trim();
                    String fname2 = i2 < 0 ? "" : s2.substring(0, i2).trim();
                    return fname1.compareTo(fname2);
                }
            });
 
            // sort date by descending order
            model.getField("Date").setGroupHandler(new GroupHandler() {
                public Object getGroup(Object data) {
                    SimpleDateFormat format = new SimpleDateFormat("yyyy/MM/dd");
                    return format.format((Date) data);
                }
            });
            model.setFieldKeyOrder("Date", false);
        }
 
        public void configure(Pivottable table) {
            table.setDataFieldOrient("column");
        }
 
        public PivotRenderer getRenderer() {
            return SALES_RACE_RENDERER;
        }
    };
 
    private static final PivotRenderer SALES_RACE_RENDERER = new SimplePivotRenderer() {
 
        public int getColumnSize(Pivottable table, PivotHeaderContext colc, PivotField dataField) {
            if (dataField != null && "Price".equals(dataField.getFieldName()))
                return 200;
            return colc.isGrandTotal() && dataField != null ? 150 : 100;
        }
 
        public String renderCellSClass(Number data, Pivottable table, PivotHeaderContext rowContext,
                PivotHeaderContext columnContext, PivotField dataField) {
            if (dataField != null && "Price".equals(dataField.getFieldName())) {
                String sclass = "highlight";
                if (!rowContext.isGrandTotal() && !columnContext.isGrandTotal() && data != null
                        && data.doubleValue() > 300)
                    sclass += " important";
                return sclass;
            }
            return null;
        }
 
        public String renderCellStyle(Number data, Pivottable table, PivotHeaderContext rowContext,
                PivotHeaderContext columnContext, PivotField dataField) {
            if (columnContext.isGrandTotal())
                return "color: #11EE11; font-weight: bold";
            return null;
        }
    };
 
}


3. Model :




Hasil :




Reference : https://www.zkoss.org/zkdemo/chart

Memunculkan Simbol & Emoji Pada OS Mac

  Memunculkan Simbol & Emoji  1. Buka aplikasi Pages / Notes pada Macbook. 2. Klik pada Menubar Edit --> Pilih Emoji and Symbols a...