import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ChartAndPie {
    public static void main(String[] args) {
        JFrame f = new JFrame("Balken- und Tortendiagramm");
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        TrendReport trendReport = new TrendReport();
        f.getContentPane().add(trendReport);
        f.setSize(1000, 600);
        f.setLocationRelativeTo(null);
        f.setVisible(true);
    }
}
class TrendReport extends JPanel implements ActionListener {
    int width;
    int height;
    JButton button;
    int[] bar = new int[4];
    float[] flote = { 2677, 1236, 1234, 2322 };
    String[] valuesInput = { "2677", "1236", "1234", "2322" };
    String str = "", title = "Einige Zahlen grafisch aufbereitet ...";
    String[] barLabels = { "1. Woche Nov. 2004", "2. Woche Nov. 2004",
            "3. Woche Nov. 2004", "4. Woche Nov. 2004" };
    String[] percent = { "", "", "", "" };
    JLabel[] JLab = new JLabel[4];
    JTextField titletxt;
    JTextField[] Text = new JTextField[5];
    JTextField[] labeltxt = new JTextField[5];
    boolean pieChart;
    public TrendReport() {
        setOpaque(false);
        setLayout(new FlowLayout());
        button = new JButton("Balkendiagramm");
        button.addActionListener(this);
        add(button);
    }
    public void paintComponent(Graphics g) {
        width = getWidth();
        height = getHeight();
        Graphics2D g2 = (Graphics2D) g;
        g2.setColor(new Color(223, 222, 224));
        g2.fillRect(0, 0, width, height);
        g2.setColor(Color.orange);
        if (pieChart) {
            g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90, -bar[0]);
            g2.fillRect(getW(270), getH(170), getW(30), getH(20));
        } else
            g2.fillRect(getW(30), getH(150), getW(bar[0]), getH(30));
        g2.setColor(Color.green);
        if (pieChart) {
            g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90 - bar[0],
                    -bar[1]);
            g2.fillRect(getW(270), getH(210), getW(30), getH(20));
        } else
            g2.fillRect(getW(30), getH(190), getW(bar[1]), getH(30));
        g2.setColor(Color.red);
        if (pieChart) {
            g2.fillArc(getW(30), getH(130), getW(220), getH(220),
                    90 - (bar[0] + bar[1]), -bar[2]);
            g2.fillRect(getW(270), getH(250), getW(30), getH(20));
        } else
            g2.fillRect(getW(30), getH(230), getW(bar[2]), getH(30));
        g2.setColor(Color.blue);
        if (pieChart) {
            g2.fillArc(getW(30), getH(130), getW(220), getH(220), 90 - (bar[0]
                    + bar[1] + bar[2]), -bar[3]);
            g2.fillRect(getW(270), getH(290), getW(30), getH(20));
        } else
            g2.fillRect(getW(30), getH(270), getW(bar[3]), getH(30));
        g2.setColor(Color.black);
        g2.setFont(new Font("Arial", Font.BOLD, 18));
        if (pieChart)
            g2.drawString(title, getW(220), getH(142));
        else
            g2.drawString(title, getW(50), getH(132));
        g2.setFont(new Font("Arial", Font.PLAIN, 16));
        int temp = 0;
        if (pieChart)
            temp = 185;
        else
            temp = 172;
        for (int j = 0; j < 4; j++) {
            if (pieChart)
                g2.drawString("€" + valuesInput[j] + " " + barLabels[j]
                        + percent[j], getW(305), getH(temp));
            else
                g2.drawString("€" + valuesInput[j] + " " + barLabels[j]
                        + percent[j], getW(bar[j] + 40), getH(temp));
            temp += 40;
        }
        if (!pieChart) {
            g2.drawLine(getW(30), getH(130), getW(30), getH(300));
            g2.drawLine(getW(30), getH(300), getW(430), getH(300));
            g2.setFont(new Font("Arial", Font.PLAIN, 12));
        }
        super.paintComponent(g2);
    }
    private int getW(int dx) {
        return (dx * width) / 550;
    }
    private int getH(int dy) {
        return (dy * height) / 400;
    }
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if (command.equals("Balkendiagramm")) {
            button.setText("Tortendiagramm");
            pieChart = false;
            try {
                int temp = 0;
                java.text.DecimalFormat df = new java.text.DecimalFormat(
                        "#,##0.##");
                for (int j = 0; j < 4; j++) {
                    temp += (int) ((flote[j]) + 0.5);
                }
                for (int k = 0; k < 4; k++) {
                    bar[k] = (int) (((flote[k] / temp) * 360) + 0.5);
                    flote[k] = (flote[k] / temp) * 100;
                    percent[k] = ": " + df.format(flote[k]) + "%";
                }
            } catch (Exception message) {
                title = "Hoppla! Alle Felder komplettieren. Nur Zahlen eingeben!";
            }
        }
        if (command.equals("Tortendiagramm")) {
            button.setText("Balkendiagramm");
            pieChart = true;
        }
        repaint();
    }
}