org.karndo.graphs.CustomChartFactory.java Source code

Java tutorial

Introduction

Here is the source code for org.karndo.graphs.CustomChartFactory.java

Source

/*
     
   Copyright 2012, Shane Boulden
     
   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at
     
   http://www.apache.org/licenses/LICENSE-2.0
     
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
 */
package org.karndo.graphs;

import java.util.Calendar;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import org.apache.commons.lang3.mutable.MutableInt;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.karndo.piracy.PiracyEvent;

public class CustomChartFactory extends ChartFactory {

    private static final String[] DAYS_OF_THE_WEEK = { "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday",
            "Friday", "Saturday", };

    private static final String[] MONTHS = { "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct",
            "Nov", "Dec" };

    public CustomChartFactory() {
        super();
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by day-of-the-
     * week. Presently uses a very basic method of graphing this data by 
     * using the static CreateBarChart3D method available in class
     * org.jfree.chart.ChartFactory.     * 
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against days of the Calendar week.
     */
    public JFreeChart createPiracyHistogramDayOfWeek(LinkedList<PiracyEvent> data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        Integer[] freqs = new Integer[DAYS_OF_THE_WEEK.length];
        Calendar cal = Calendar.getInstance();

        //set up the frequencies array
        for (int i = 0; i < freqs.length; i++) {
            freqs[i] = 0;
        }

        for (PiracyEvent ev : data) {
            cal.setTime(ev.getDate());
            freqs[cal.get(Calendar.DAY_OF_WEEK) - 1]++;
        }

        //add the frequencies to a dataset for plotting
        for (int i = 0; i < freqs.length; i++) {
            dataset.addValue(freqs[i], "Piracy Incidents", DAYS_OF_THE_WEEK[i]);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by days of the week",
                "Day of the Week", "Frequency", dataset, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by vessel
     * type. Presently uses a very basic method of graphing this data by 
     * using the static CreateBarChart3D method available in class
     * org.jfree.chart.ChartFactory.     
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against vessel type.
     */
    public JFreeChart createHistogramVesselType(LinkedList<PiracyEvent> data) {
        //the data to plot
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        LinkedHashMap<String, MutableInt> freqs_cats = new LinkedHashMap<String, MutableInt>();

        for (PiracyEvent ev : data) {
            if (!freqs_cats.containsKey(ev.getVesselType())) {
                freqs_cats.put(ev.getVesselType(), new MutableInt(1));
            } else {
                freqs_cats.get(ev.getVesselType()).increment();
            }
        }

        Iterator itr = freqs_cats.keySet().iterator();
        while (itr.hasNext()) {
            String category = (String) itr.next();
            Integer i1 = freqs_cats.get(category).getValue();
            dataset.addValue(i1, "Piracy Incidents", category);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by vessel type", "Vessel Type",
                "Frequency", dataset, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by event 
     * status. Presently uses a very basic method of graphing this data by 
     * using the static CreateBarChart3D method available in class
     * org.jfree.chart.ChartFactory.    
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against event status.
     */
    public JFreeChart createHistogramStatus(LinkedList<PiracyEvent> data) {
        //the data to plot
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();
        LinkedHashMap<String, MutableInt> freqs_cats = new LinkedHashMap<String, MutableInt>();

        for (PiracyEvent ev : data) {
            if (!freqs_cats.containsKey(ev.getStatus())) {
                freqs_cats.put(ev.getStatus(), new MutableInt(1));
            } else {
                freqs_cats.get(ev.getStatus()).increment();
            }
        }

        Iterator itr = freqs_cats.keySet().iterator();
        while (itr.hasNext()) {
            String category = (String) itr.next();
            Integer i1 = freqs_cats.get(category).getValue();
            dataset.addValue(i1, "Piracy Incidents", category);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by event status", "Event Status",
                "Frequency", dataset, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by day-of-the-
     * month. Presently uses a very basic method of graphing this data by 
     * using the static CreateBarChart3D method available in class
     * org.jfree.chart.ChartFactory.   
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against days of the month.
     */
    public JFreeChart createHistogramDayofTheMonth(LinkedList<PiracyEvent> data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        Integer[] freqs = new Integer[31];

        //fill the array
        for (int i = 0; i < freqs.length; i++) {
            freqs[i] = 0;
        }

        Calendar cal = Calendar.getInstance();

        for (PiracyEvent ev : data) {
            cal.setTime(ev.getDate());
            int idx = cal.get(Calendar.DAY_OF_MONTH) - 1;
            freqs[idx]++;
        }

        //for some reason there is a method for dataset.add() for both 
        //"Number" classes and primitive doubles - queue horrible programming
        //to avoid ambiguous method calls.
        for (Integer i = 1; i < freqs.length + 1; i++) {
            dataset.addValue(freqs[i - 1], "Piracy Incidents", i);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by days of the month",
                "Day of the Month", "Frequency", dataset, PlotOrientation.VERTICAL, false, true, false);

        return chart;
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by month, using 
     * all months of the year. Presently uses a very basic method of graphing 
     * this data by using the static CreateBarChart3D method available in class
     * org.jfree.chart.ChartFactory.     * 
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against months of the year.
     */
    public JFreeChart createHistogramMonthofYear(LinkedList<PiracyEvent> data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        Integer[] freqs = new Integer[MONTHS.length];
        Calendar cal = Calendar.getInstance();

        //set up the frequencies array
        for (int i = 0; i < freqs.length; i++) {
            freqs[i] = 0;
        }

        for (PiracyEvent ev : data) {
            cal.setTime(ev.getDate());
            freqs[cal.get(Calendar.MONTH)]++;
        }

        //add the frequencies to a dataset for plotting
        for (int i = 0; i < freqs.length; i++) {
            dataset.addValue(freqs[i], "Piracy Incidents", MONTHS[i]);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by month", "Month", "Frequency",
                dataset, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }

    /**
     * Creates a chart of the selected PiracyEvent data graphed by month from
     * the available months in the data. Presently uses a very basic method of 
     * graphing this data by      * using the static CreateBarChart3D method 
     * available in class org.jfree.chart.ChartFactory.
     * 
     * @param data the selected PiracyEvent data to graph.
     * @return A JFreeChart object representing a graph of the selected 
     * PiracyEvent data against months from the available data.
     */
    public JFreeChart createHistogramMonthlyAvailableData(LinkedList<PiracyEvent> data) {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        Integer[] freqs = new Integer[MONTHS.length];
        Calendar cal = Calendar.getInstance();

        //set up the frequencies array
        for (int i = 0; i < freqs.length; i++) {
            freqs[i] = 0;
        }

        for (PiracyEvent ev : data) {
            cal.setTime(ev.getDate());
            freqs[cal.get(Calendar.MONTH)]++;
        }

        //add the frequencies to a dataset for plotting
        for (int i = 0; i < freqs.length; i++) {
            if (freqs[i] != 0) {
                dataset.addValue(freqs[i], "Piracy Incidents", MONTHS[i]);
            }
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Piracy Incidents " + "by month", "Month", "Frequency",
                dataset, PlotOrientation.VERTICAL, false, true, false);
        return chart;
    }
}