org.fhaes.FHRecorder.GraphicsPanel.java Source code

Java tutorial

Introduction

Here is the source code for org.fhaes.FHRecorder.GraphicsPanel.java

Source

/*******************************************************************************
 * Copyright (C) 2014 Brendan Compton, Dylan Jones, Alex Richter, Chris Wald
 * Chris Wald and Peter Brewer
 * 
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     This program 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 General Public License for more details.
 * 
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package org.fhaes.FHRecorder;

import java.awt.BorderLayout;
import java.awt.Color;
import java.util.List;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.DatasetRenderingOrder;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.renderer.xy.XYBarRenderer;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.category.SlidingCategoryDataset;

/**
 * Graphs a histogram of the number of samples per year.
 * Overlays a graph of the total number of samples per unit time.
 * @authors Alex Richter, Brendan Compton, Chris Wald, Dylan Jones
 */
public class GraphicsPanel extends ChartPanel {

    private static final long serialVersionUID = 1L;
    private JFreeChart chart;
    private CategoryPlot plot;
    private SlidingCategoryDataset numEventsDataset;
    private SlidingCategoryDataset numSamplesDataset;
    private SlidingCategoryDataset numRecordersDataset;
    private int firstYear;
    private int lastYear;
    private int savedPercent;
    final static private boolean generateLegend = false;
    final static private boolean generateTooltips = false;
    final static private boolean generateURLs = false;
    final static private int PREFERRED_WIDTH = 858;
    final static private int PREFERRED_HEIGHT = 479;
    final static private int NUM_COLUMNS = 20;

    public GraphicsPanel() {
        super(ChartFactory.createHistogram("Summary of Samples", "# of Events", "Years", null,
                PlotOrientation.VERTICAL, generateLegend, generateTooltips, generateURLs));
        chart = this.getChart();

        setMinimumSize(new java.awt.Dimension(790, 450));
        // This can be changed to be larger, and then the chart can scroll
        setPreferredSize(new java.awt.Dimension(PREFERRED_WIDTH, PREFERRED_HEIGHT));
        setLayout(new BorderLayout());

        this.setChart(updateGraph());
    }

    /**
     * Recalculates the charts based on the latest sample
     * data
     * @return
     */
    private JFreeChart updateGraph() {
        try {
            NumberAxis rangeAxis = new NumberAxis();
            XYBarRenderer renderer = new XYBarRenderer();
            renderer.setSeriesPaint(0, Color.BLUE);
            rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
            rangeAxis.configure();

            numEventsDataset = new SlidingCategoryDataset(getNumEventsDataset(), 0, NUM_COLUMNS);
            BarRenderer numEventsRenderer = new BarRenderer();
            numEventsRenderer.setSeriesPaint(0, Color.BLUE);
            plot = new CategoryPlot(numEventsDataset, new CategoryAxis("Event Years"), rangeAxis,
                    numEventsRenderer);

            numSamplesDataset = new SlidingCategoryDataset(getNumSamplesDataset(), 0, NUM_COLUMNS);
            plot.setDataset(1, numSamplesDataset);
            LineAndShapeRenderer numSamplesRenderer = new LineAndShapeRenderer();
            numSamplesRenderer.setSeriesPaint(0, Color.RED);
            plot.setRenderer(1, numSamplesRenderer);

            numRecordersDataset = new SlidingCategoryDataset(getNumRecordersDataset(), 0, NUM_COLUMNS);
            plot.setDataset(2, numRecordersDataset);
            LineAndShapeRenderer numRecordersRenderer = new LineAndShapeRenderer();
            numRecordersRenderer.setSeriesPaint(0, Color.GREEN);
            plot.setRenderer(2, numRecordersRenderer);

            plot.setDatasetRenderingOrder(DatasetRenderingOrder.FORWARD);

            updateVisibleYears(savedPercent);

            return new JFreeChart(plot);
        } catch (IllegalArgumentException e) {
            // Something was wrong with the file
        }
        return null;
    }

    /**
     * Takes the number of samples and puts it into a dataset
     * for the graph
     * @return
     */
    private CategoryDataset getNumSamplesDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        firstYear = Controller.getFile().getRequiredPart().getDatasetFirstYear();
        lastYear = Controller.getFile().getRequiredPart().getDatasetLastYear();

        List<FHX2Sample> samples = Controller.getFile().getRequiredPart().getSampleList();

        for (int i = firstYear; i <= lastYear; i++) {
            if (i != 0) {
                // Number of samples in this year
                int numSamplesInYear = 0;
                for (FHX2Sample sample : samples) {
                    if (sample.containsYear(i)) {
                        numSamplesInYear++;
                    }
                }

                dataset.addValue((Number) numSamplesInYear, "Number of Samples", i);
            }
        }

        return dataset;
    }

    /**
     * Takes the number of recorders and puts it into a dataset
     * for the graph
     * @return
     */
    private CategoryDataset getNumRecordersDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        firstYear = Controller.getFile().getRequiredPart().getDatasetFirstYear();
        lastYear = Controller.getFile().getRequiredPart().getDatasetLastYear();

        List<FHX2Sample> samples = Controller.getFile().getRequiredPart().getSampleList();

        for (int i = firstYear; i <= lastYear; i++) {
            if (i != 0) {
                // Number of recorders in this year
                int numRecordersInYear = 0;
                for (FHX2Sample sample : samples) {
                    if (sample.isYearInRecordings(i)) {
                        numRecordersInYear++;
                    }
                }

                dataset.addValue((Number) numRecordersInYear, "Number of Recorders", i);
            }
        }

        return dataset;
    }

    /**
     * Takes the number of events and puts it into a dataset
     * for the graph
     * @return
     */
    private CategoryDataset getNumEventsDataset() {
        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        firstYear = Controller.getFile().getRequiredPart().getDatasetFirstYear();
        lastYear = Controller.getFile().getRequiredPart().getDatasetLastYear();

        List<FHX2Sample> samples = Controller.getFile().getRequiredPart().getSampleList();

        for (int i = firstYear; i <= lastYear; i++) {
            if (i != 0) {
                int numEventsInYear = 0;
                for (FHX2Sample sample : samples) {
                    if (sample.hasEventInYear(i)) {
                        numEventsInYear++;
                    }
                }

                dataset.addValue((Number) numEventsInYear, "Number of Events", i);
            }
        }

        return dataset;
    }

    /**
     * Repaints the tab.
     */
    public void updateGraphics() {
        chart = updateGraph();

        if (chart != null) {
            chart.fireChartChanged();
            this.setChart(chart);
            this.repaint();
        }
    }

    public void updateVisibleYears(int percentage) {
        firstYear = Controller.getFile().getRequiredPart().getDatasetFirstYear();
        lastYear = Controller.getFile().getRequiredPart().getDatasetLastYear();

        if (lastYear - firstYear > NUM_COLUMNS) {
            int visibleYear = ((lastYear - firstYear) * (percentage) / 100);
            int yearZeroOffset = 0;
            if (firstYear < 0 && lastYear > 0) {
                yearZeroOffset = 1;
            }
            if (visibleYear > lastYear - firstYear - NUM_COLUMNS + 1 - yearZeroOffset) {
                visibleYear = lastYear - firstYear - NUM_COLUMNS + 1 - yearZeroOffset;
            }
            numEventsDataset.setFirstCategoryIndex(visibleYear);
            numSamplesDataset.setFirstCategoryIndex(visibleYear);
            numRecordersDataset.setFirstCategoryIndex(visibleYear);

            chart.fireChartChanged();
        }
        savedPercent = percentage;
    }
}