de.rbs90.fwdisp.settingsgui.gui.tabs.statistics.YearStatisticPanel.java Source code

Java tutorial

Introduction

Here is the source code for de.rbs90.fwdisp.settingsgui.gui.tabs.statistics.YearStatisticPanel.java

Source

/*******************************************************************************
 * FWDisp - https://github.com/rbs90/FWDisp
 *
 * Copyright (C) 2012 Robert Schoenherr
 *
 * FWDisp 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 2 of the License,
 * or (at your option) any later version.
 *
 * FWDisp 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.
 ******************************************************************************/

package de.rbs90.fwdisp.settingsgui.gui.tabs.statistics;

import de.rbs90.fwdisp.Starter;
import de.rbs90.fwdisp.settingsgui.gui.moduls.SecRowPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;

import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Set;

/**
 * @author rbs
 */
public class YearStatisticPanel extends SecRowPanel {

    public YearStatisticPanel() {
        setName("Jahr");
        setLayout(new BorderLayout());

        DefaultCategoryDataset dataset = new DefaultCategoryDataset();

        HashMap<Integer, Integer> alarmCount = new HashMap<>();

        try {
            ResultSet resultSet = Starter.getDatabase().getStatement().executeQuery(
                    "SELECT STARTDATE_YEAR, COUNT(*) AS COUNT FROM ALARMHISTORY GROUP BY STARTDATE_YEAR"
                            + " ORDER BY STARTDATE_YEAR ASC");

            while (resultSet.next()) {
                alarmCount.put(resultSet.getInt("STARTDATE_YEAR"), resultSet.getInt("COUNT"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }

        Set<Integer> years = alarmCount.keySet();

        for (Integer year : years) {
            Integer count = alarmCount.get(year);
            dataset.addValue(count, count, year);
        }

        JFreeChart chart = ChartFactory.createBarChart3D("Jahresbersicht", "Jahr", "Einstze", dataset,
                PlotOrientation.VERTICAL, false, false, false);

        chart.setBackgroundPaint(getBackground());
        ChartPanel panel = new ChartPanel(chart);
        panel.setPopupMenu(null);

        add(panel, BorderLayout.CENTER);
    }

}