view.PnStatistic.java Source code

Java tutorial

Introduction

Here is the source code for view.PnStatistic.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import Utils.PnIncomeFindMonth;
import Utils.PnIncomeFindQuarter;
import Utils.PnIncomeFindYear;
import common.CommonFunction;
import common.Constant;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.sql.Date;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.text.ParsePosition;
import java.util.Calendar;
import model.ModelCustomerService;
import model.ModelRoomStatus;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
 *
 * @author GloomySunday
 */
public class PnStatistic extends javax.swing.JPanel {

    private final ModelCustomerService mModelCustomerService;
    private final ModelRoomStatus mModelRoomStatus;
    private final PnIncomeFindMonth mPnIncomeFindMonth;
    private final PnIncomeFindQuarter mPnIncomeFindQuarter;
    private final PnIncomeFindYear mPnIncomeFindYear;

    /**
     * Creates new form PnHotel
     */
    public PnStatistic() {
        initComponents();
        mModelCustomerService = new ModelCustomerService();
        mModelRoomStatus = new ModelRoomStatus();
        mPnIncomeFindMonth = new PnIncomeFindMonth();
        mPnIncomeFindQuarter = new PnIncomeFindQuarter();
        mPnIncomeFindYear = new PnIncomeFindYear();

        pnFind.add(mPnIncomeFindMonth, BorderLayout.CENTER);
        drawChart(createBarChart(createDatasetMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                (int) mPnIncomeFindMonth.getCbYear().getSelectedItem())));

        mPnIncomeFindMonth.getCbMonth().addActionListener((ActionEvent e) -> {
            drawChart(createBarChart(createDatasetMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                    (int) mPnIncomeFindMonth.getCbYear().getSelectedItem())));
        });
        mPnIncomeFindMonth.getCbYear().addActionListener((ActionEvent e) -> {
            drawChart(createBarChart(createDatasetMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                    (int) mPnIncomeFindMonth.getCbYear().getSelectedItem())));
        });

        mPnIncomeFindQuarter.getCbQuarter().addActionListener((ActionEvent e) -> {
            drawChart(createBarChart(createDatasetQuarter(
                    Integer.parseInt((String) mPnIncomeFindQuarter.getCbQuarter().getSelectedItem()),
                    (int) mPnIncomeFindQuarter.getCbYear().getSelectedItem())));
        });
        mPnIncomeFindQuarter.getCbYear().addActionListener((ActionEvent e) -> {
            drawChart(createBarChart(createDatasetQuarter(
                    Integer.parseInt((String) mPnIncomeFindQuarter.getCbQuarter().getSelectedItem()),
                    (int) mPnIncomeFindQuarter.getCbYear().getSelectedItem())));
        });

        mPnIncomeFindYear.getCbYear().addActionListener((ActionEvent e) -> {
            drawChart(createBarChart(createDatasetYear((int) mPnIncomeFindYear.getCbYear().getSelectedItem())));
        });
    }

    private void drawChart(ChartPanel chartPanel) {
        pnCenter.removeAll();
        pnCenter.add(chartPanel, BorderLayout.CENTER);
        pnCenter.repaint();
    }

    private ChartPanel createBarChart(CategoryDataset dataset) {
        JFreeChart barChart = ChartFactory.createBarChart3D("Thng k", "Th?i gian", "Thu nhp", dataset,
                PlotOrientation.VERTICAL, true, true, false);
        ChartPanel chartPanel = new ChartPanel(barChart);
        CategoryPlot plot = barChart.getCategoryPlot();
        NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
        numberAxis.setNumberFormatOverride(new NumberFormat() {
            @Override
            public StringBuffer format(double number, StringBuffer toAppendTo, FieldPosition pos) {
                return new StringBuffer(CommonFunction.convertDoubleToMoney(number));
            }

            @Override
            public StringBuffer format(long number, StringBuffer toAppendTo, FieldPosition pos) {
                return new StringBuffer(CommonFunction.convertDoubleToMoney(number));
            }

            @Override
            public Number parse(String source, ParsePosition parsePosition) {
                return null;
            }
        });
        return chartPanel;
    }

    private CategoryDataset createDatasetMonth(int month, int year) {
        String categotyRoom = "Phng";
        String categoryService = "Dch v";
        String categoryAll = "Tng cng";
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        Calendar calendar = Calendar.getInstance();
        calendar.set(year, month - 1, calendar.get(Calendar.DATE));
        int maxDay = CommonFunction.getDateOfMonth(new Date(calendar.getTimeInMillis()));
        for (int i = 1; i <= maxDay; i++) {
            calendar.set(Calendar.DATE, i);
            double incomeRoom = mModelRoomStatus.getIncomeRoomByDay(new Date(calendar.getTimeInMillis()));
            double incomeService = mModelCustomerService.getIncomeByDate(new Date(calendar.getTimeInMillis()));
            dataset.addValue(incomeRoom, categotyRoom, "Ngy " + i);
            dataset.addValue(incomeService, categoryService, "Ngy " + i);
            dataset.addValue(incomeRoom + incomeService, categoryAll, "Ngy " + i);
        }
        return dataset;
    }

    private CategoryDataset createDatasetQuarter(int quarter, int year) {
        int[][] monthOfQuarter = Constant.MONTH_OF_QUARTER;
        String categotyRoom = "Phng";
        String categoryService = "Dch v";
        String categoryAll = "Tng cng";
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < 3; i++) {
            double incomeRoom = mModelRoomStatus.getIncomeRoomByMonth(monthOfQuarter[quarter - 1][i], year);
            double incomeService = mModelCustomerService.getIncomeByMonth(monthOfQuarter[quarter - 1][i], year);
            dataset.addValue(incomeRoom, categotyRoom, "Thng " + monthOfQuarter[quarter - 1][i]);
            dataset.addValue(incomeService, categoryService, "Thng " + monthOfQuarter[quarter - 1][i]);
            dataset.addValue(incomeRoom + incomeService, categoryAll, "Thng " + monthOfQuarter[quarter - 1][i]);
        }
        return dataset;
    }

    private CategoryDataset createDatasetYear(int year) {
        String categotyRoom = "Phng";
        String categoryService = "Dch v";
        String categoryAll = "Tng cng";
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        for (int i = 0; i < 4; i++) {
            double incomeRoom = mModelRoomStatus.getIncomeRoomByQuarter(i + 1, year);
            double incomeService = mModelCustomerService.getIncomeByQuarter(i + 1, year);
            dataset.addValue(incomeRoom, categotyRoom, "Qu " + String.valueOf(i));
            dataset.addValue(incomeService, categoryService, "Qu " + String.valueOf(i));
            dataset.addValue(incomeRoom + incomeService, categoryAll, "Qu " + String.valueOf(i));
        }
        return dataset;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnFunction = new javax.swing.JPanel();
        jPanel2 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        cbTime = new javax.swing.JComboBox<>();
        pnFind = new javax.swing.JPanel();
        pnCenter = new javax.swing.JPanel();

        setBackground(new java.awt.Color(251, 250, 202));
        setLayout(new java.awt.BorderLayout());

        pnFunction.setBackground(new java.awt.Color(251, 250, 202));

        jPanel2.setBackground(new java.awt.Color(251, 250, 202));
        jPanel2.setBorder(javax.swing.BorderFactory.createCompoundBorder(
                javax.swing.BorderFactory.createEmptyBorder(5, 5, 5, 5),
                javax.swing.BorderFactory.createTitledBorder(
                        new javax.swing.border.LineBorder(new java.awt.Color(0, 255, 204), 1, true),
                        "Khong th?i gian", javax.swing.border.TitledBorder.LEFT,
                        javax.swing.border.TitledBorder.TOP, new java.awt.Font("Tahoma", 1, 11),
                        new java.awt.Color(0, 255, 204)))); // NOI18N

        jLabel1.setText("Th?i gian");

        cbTime.setModel(new javax.swing.DefaultComboBoxModel<>(new String[] { "Thng", "Qu", "Nm" }));
        cbTime.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cbTimeActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(jPanel2Layout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(jPanel2Layout.createSequentialGroup()
                        .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jLabel1).addComponent(cbTime, javax.swing.GroupLayout.PREFERRED_SIZE,
                                        190, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 0, Short.MAX_VALUE)));
        jPanel2Layout.setVerticalGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 7, Short.MAX_VALUE)
                        .addComponent(cbTime, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap()));

        pnFind.setBackground(new java.awt.Color(251, 250, 202));
        pnFind.setLayout(new java.awt.BorderLayout());

        javax.swing.GroupLayout pnFunctionLayout = new javax.swing.GroupLayout(pnFunction);
        pnFunction.setLayout(pnFunctionLayout);
        pnFunctionLayout.setHorizontalGroup(pnFunctionLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addComponent(pnFind, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE,
                        Short.MAX_VALUE));
        pnFunctionLayout.setVerticalGroup(pnFunctionLayout
                .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                .addGroup(pnFunctionLayout.createSequentialGroup()
                        .addComponent(jPanel2, javax.swing.GroupLayout.PREFERRED_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(0, 0, 0).addComponent(pnFind, javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));

        add(pnFunction, java.awt.BorderLayout.LINE_END);

        pnCenter.setBackground(new java.awt.Color(251, 250, 202));
        pnCenter.setLayout(new java.awt.BorderLayout());
        add(pnCenter, java.awt.BorderLayout.CENTER);
    }// </editor-fold>//GEN-END:initComponents

    private void cbTimeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbTimeActionPerformed
        pnFind.removeAll();
        switch (cbTime.getSelectedIndex()) {
        case 0:
            pnFind.add(mPnIncomeFindMonth, BorderLayout.CENTER);
            drawChart(createBarChart(createDatasetMonth((int) mPnIncomeFindMonth.getCbMonth().getSelectedItem(),
                    (int) mPnIncomeFindMonth.getCbYear().getSelectedItem())));
            break;
        case 1:
            pnFind.add(mPnIncomeFindQuarter, BorderLayout.CENTER);
            drawChart(createBarChart(createDatasetQuarter(
                    Integer.parseInt((String) mPnIncomeFindQuarter.getCbQuarter().getSelectedItem()),
                    (int) mPnIncomeFindQuarter.getCbYear().getSelectedItem())));
            break;
        case 2:
            pnFind.add(mPnIncomeFindYear, BorderLayout.CENTER);
            drawChart(createBarChart(createDatasetYear((int) mPnIncomeFindYear.getCbYear().getSelectedItem())));
            break;
        }
        pnFind.repaint();
    }//GEN-LAST:event_cbTimeActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JComboBox<String> cbTime;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel pnCenter;
    private javax.swing.JPanel pnFind;
    private javax.swing.JPanel pnFunction;
    // End of variables declaration//GEN-END:variables
}