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

Java tutorial

Introduction

Here is the source code for de.rbs90.fwdisp.settingsgui.gui.tabs.statistics.TypeStatisticsPanel.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.data.general.DefaultPieDataset;

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

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

    public TypeStatisticsPanel() {
        setName("EinsatzTyp");

        setLayout(new BorderLayout());

        DefaultPieDataset dataset = new DefaultPieDataset();

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

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

            while (resultSet.next()) {
                String type = resultSet.getString("TYPE");
                if (type.isEmpty())
                    type = "unbekannt";

                int count = resultSet.getInt("COUNT");
                type += " (" + count + ")";

                alarmCount.put(type, count);
            }

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

        for (String key : alarmCount.keySet()) {
            dataset.setValue(key, alarmCount.get(key));
        }

        JFreeChart chart = ChartFactory.createPieChart3D("Einsatztypen", dataset, false, false, false);

        chart.setBackgroundPaint(getBackground());

        ChartPanel panel = new ChartPanel(chart);

        panel.setPopupMenu(null);
        panel.setBackground(getBackground());

        add(panel, BorderLayout.CENTER);

    }

}