com.opensourcestrategies.financials.reports.JFreeFinancialCharts.java Source code

Java tutorial

Introduction

Here is the source code for com.opensourcestrategies.financials.reports.JFreeFinancialCharts.java

Source

/*
 * Copyright (c) Open Source Strategies, Inc.
 *
 * Opentaps is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Affero General Public License as published
 * by the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Opentaps is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with Opentaps.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.opensourcestrategies.financials.reports;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.opentaps.common.util.UtilMessage;

import java.awt.*;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;
import java.util.Locale;
import java.util.Map;

/**
 * Charts for Financials generated by the JFree API.
 */
public class JFreeFinancialCharts {

    /**
     * Liquidity snapshot chart.  Documentation is at http://www.opentaps.org/docs/index.php/Financials_Home_Screen
     *
     * Because a user might not have permission to view balances in all areas, this method takes into consideration
     * the ability to view various bars in the chart.
     *
     * @param accountsMap Map of accounts keyed by the glAccountType
     * @return String filename pointing to the chart, to be used with showChart URI request
     */
    public static String createLiquiditySnapshotChart(Map<String, GenericValue> accountsMap,
            List<GenericValue> creditCardAccounts, Locale locale, boolean hasReceivablesPermission,
            boolean hasPayablesPermission, boolean hasInventoryPermission)
            throws GenericEntityException, IOException {
        Map<String, Object> uiLabelMap = UtilMessage.getUiLabels(locale);

        // create the dataset
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        // compile the four bars
        if (hasReceivablesPermission) {
            double cashBalance = 0.0;
            cashBalance += getPostedBalance(accountsMap.get("UNDEPOSITED_RECEIPTS"));
            cashBalance += getPostedBalance(accountsMap.get("BANK_STLMNT_ACCOUNT"));
            dataset.addValue(cashBalance, "", (String) uiLabelMap.get("FinancialsCashEquivalents"));

            double receivablesBalance = 0.0;
            receivablesBalance += getPostedBalance(accountsMap.get("ACCOUNTS_RECEIVABLE"));
            // merchant account settlement balances are receivable
            receivablesBalance += getPostedBalance(accountsMap.get("MRCH_STLMNT_ACCOUNT"));
            dataset.addValue(receivablesBalance, "", (String) uiLabelMap.get("FinancialsReceivables"));
        }
        if (hasInventoryPermission) {
            double inventoryBalance = 0.0;
            inventoryBalance += getPostedBalance(accountsMap.get("INVENTORY_ACCOUNT"));
            inventoryBalance += getPostedBalance(accountsMap.get("RAWMAT_INVENTORY"));
            inventoryBalance += getPostedBalance(accountsMap.get("WIP_INVENTORY"));
            dataset.addValue(inventoryBalance, "", (String) uiLabelMap.get("WarehouseInventory"));
        }
        if (hasPayablesPermission) {
            double payablesBalance = 0.0;
            payablesBalance += getPostedBalance(accountsMap.get("ACCOUNTS_PAYABLE"));
            payablesBalance += getPostedBalance(accountsMap.get("COMMISSIONS_PAYABLE"));
            payablesBalance += getPostedBalance(accountsMap.get("UNINVOICED_SHIP_RCPT"));
            dataset.addValue(payablesBalance, "", (String) uiLabelMap.get("FinancialsPayables"));
        }

        // set up the chart
        JFreeChart chart = ChartFactory.createBarChart((String) uiLabelMap.get("FinancialsLiquiditySnapshot"), // chart title
                null, // domain axis label
                null, // range axis label
                dataset, // data
                PlotOrientation.VERTICAL, // orientation
                false, // include legend
                true, // tooltips
                false // urls
        );
        chart.setBackgroundPaint(Color.white);
        chart.setBorderVisible(true);
        chart.setPadding(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

        // get a reference to the plot for further customisation...
        final CategoryPlot plot = chart.getCategoryPlot();
        plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);

        // get the bar renderer to put effects on the bars
        final BarRenderer renderer = (BarRenderer) plot.getRenderer();
        renderer.setDrawBarOutline(false); // disable bar outlines

        // set up gradient paint on bar
        final GradientPaint gp = new GradientPaint(0.0f, 0.0f, Color.GREEN, 0.0f, 0.0f, Color.GRAY);
        renderer.setSeriesPaint(0, gp);

        // change the auto tick unit selection to integer units only...
        final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());

        // tilt the category labels so they fit
        CategoryAxis domainAxis = plot.getDomainAxis();
        domainAxis.setCategoryLabelPositions(CategoryLabelPositions.createUpRotationLabelPositions(Math.PI / 6.0));

        // save as a png and return the file name
        return ServletUtilities.saveChartAsPNG(chart, 400, 300, null);
    }

    // helper method to make the code more concise
    // the Map is a map of string -> BigDecimal, so return the double value of the BigDecimal
    private static double getPostedBalance(Object accountBalance) {
        if (accountBalance == null)
            return 0.0;
        return ((BigDecimal) accountBalance).doubleValue();
    }

}