import java.util.ArrayList;
import java.awt.Color;
import java.awt.Component;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.time.Day;
public class Display extends JFrame {
public static JPanel graphingPanel;
public static ChartPanel chartPanel;
public static JFreeChart firstChart;
public static JFreeChart secondChart;
public static JFreeChart thirdChart;
private JPanel panelBackground;
private JPanel myPanel;
private JComboBox cbbGraphs;
private GridBagConstraints constraints;
ArrayList<String> categories = new ArrayList<String>();
public Display()
{
//Frame details
super("Title");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(100, 100, 1200, 700);
setLocationRelativeTo(null);
panelBackground = new JPanel();
panelBackground.setBorder(new EmptyBorder(5, 5, 5, 5));
setContentPane(panelBackground);
panelBackground.setBackground(new Color(20,50,75));
GridBagLayout gbl_panelBackground = new GridBagLayout();
panelBackground.setLayout(gbl_panelBackground);
//Panel
myPanel = new JPanel();
myPanel.setBorder(null);
myPanel.setBackground(new Color(240, 255, 255));
GridBagLayout gbl_myPanel = new GridBagLayout();
gbl_myPanel.columnWidths = new int[]{50, 125, 50, 50, 50, 50, 0,50,50,50,50,50,50,50};
gbl_myPanel.rowHeights = new int[]{20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20,20};
myPanel.setLayout(gbl_myPanel);
//Constraints
constraints = new GridBagConstraints();
constraints.anchor = GridBagConstraints.EAST;
constraints.gridx=0;
constraints.gridy=0;
panelBackground.add(myPanel,constraints);
//###############################################################################
GridBagConstraints gbc_lbl = new GridBagConstraints();
gbc_lbl.anchor = GridBagConstraints.NORTHWEST;
gbc_lbl.gridx = 0;
gbc_lbl.gridy = 0;
//Graphing Panel
graphingPanel = new JPanel();
constraints.anchor = GridBagConstraints.SOUTHWEST;
constraints.insets = new Insets(0,0,5,5);
constraints.fill = GridBagConstraints.BOTH;
addComponent(graphingPanel,6,1,8,13);
//###############################################Construct first chart####################################################
TimeSeries pop = new TimeSeries("Sales", Day.class);
TimeSeries pop2 = new TimeSeries("Purchases", Day.class);
pop2.add(new Day(20, 1, 2004), 200);
pop2.add(new Day(20, 2, 2004), 250);
pop2.add(new Day(20, 3, 2004), 450);
pop2.add(new Day(20, 4, 2004), 475);
pop2.add(new Day(20, 5, 2004), 125);
pop2.add(new Day(20, 6, 2004), 100);
pop2.add(new Day(20, 7, 2004), 400);
pop2.add(new Day(20, 8, 2004), 125);
pop2.add(new Day(20, 9, 2004), 300);
pop2.add(new Day(20, 10, 2004), 223);
pop2.add(new Day(20, 11, 2004), 125);
pop2.add(new Day(20, 12, 2004), 125);
pop.add(new Day(10, 1, 2004), 100);
pop.add(new Day(10, 2, 2004), 150);
pop.add(new Day(10, 3, 2004), 250);
pop.add(new Day(10, 4, 2004), 275);
pop.add(new Day(10, 5, 2004), 325);
pop.add(new Day(20, 6, 2004), 57);
pop.add(new Day(20, 7, 2004), 125);
pop.add(new Day(20, 8, 2004), 98);
pop.add(new Day(20, 9, 2004), 200);
pop.add(new Day(20, 10, 2004), 100);
pop.add(new Day(20, 11, 2004), 200);
pop.add(new Day(20, 12, 2004), 100);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(pop2);
dataset.addSeries(pop);
firstChart = ChartFactory.createTimeSeriesChart(
"Sales and Purchases",
"Date",
"Quantity",
dataset,
true,
true,
false);
chartPanel=new ChartPanel(firstChart);
//###############################################Construct second chart####################################################
DefaultPieDataset categoryData = new DefaultPieDataset();
categoryData.setValue("capacitors", new Double(100));
categoryData.setValue("Resistors", new Double(100));
categoryData.setValue("Interface Devices", new Double(
40));
categoryData
.setValue("Transformers", new Double(100));
categoryData.setValue("Monitors", new Double(100));
categoryData.setValue("Other", new Double(100));
// create a pie chart
secondChart = ChartFactory.createPieChart(
"Stock Distribution by category", categoryData, true, true,
true);
PiePlot categoryPlot = (PiePlot) secondChart.getPlot();
categoryPlot.setSimpleLabels(true);
//###############################################Construct third chart####################################################
DefaultCategoryDataset profitAndLossData = new DefaultCategoryDataset();
profitAndLossData.setValue(123, "Purchases", "January");
profitAndLossData.setValue(213, "Sales", "January");
profitAndLossData.setValue(321, "Purchases", "February");
profitAndLossData.setValue(23, "Sales", "February");
profitAndLossData.setValue(322, "Purchases", "March");
profitAndLossData.setValue(232, "Sales", "March");
profitAndLossData.setValue(222, "Purchases", "April");
profitAndLossData.setValue(222, "Sales", "April");
profitAndLossData.setValue(111, "Purchases", "May");
profitAndLossData.setValue(222, "Sales", "May");
// create a chart...
thirdChart = ChartFactory.createBarChart(
"Purchases and Sales", "Key", "Amount (€)",
profitAndLossData, PlotOrientation.VERTICAL, true, true, false);
// ######################################################################################################
graphingPanel.add(chartPanel);
String [] graphOptions ={"Option 1","Option 2","Option 3"};
cbbGraphs = new JComboBox(graphOptions);
constraints.fill = GridBagConstraints.HORIZONTAL;
constraints.anchor = GridBagConstraints.EAST;
constraints.insets = new Insets(0, 0, 5, 5);
addComponent(cbbGraphs, 3, 4,2,1);
cbbGraphs.addItemListener(new ItemListener() {
@Override
public void itemStateChanged(ItemEvent e) {
int item = cbbGraphs.getSelectedIndex();
switch (item) {
case 0:
graphingPanel.removeAll();
graphingPanel.setVisible(false);
chartPanel = new ChartPanel(firstChart);
graphingPanel.add(chartPanel);
graphingPanel.setVisible(true);
break;
case 1:
graphingPanel.removeAll();
graphingPanel.setVisible(false);
chartPanel = new ChartPanel(secondChart);
graphingPanel.add(chartPanel);
graphingPanel.setVisible(true);
break;
case 2:
graphingPanel.removeAll();
graphingPanel.setVisible(false);
chartPanel = new ChartPanel(thirdChart);
graphingPanel.add(chartPanel);
graphingPanel.setVisible(true);
break;
default:
graphingPanel.removeAll();
graphingPanel.setVisible(false);
//chartPanel = new ChartPanel(salesAndPurchasesChart);
graphingPanel.add(chartPanel);
graphingPanel.setVisible(true);
break;
}
}
});
setContentPane(panelBackground);
panelBackground.add(myPanel);
setVisible(true);
}
//###############################################################################
//Main Method
public static void main (String[]args)
{
Display a = new Display();
}
//###############################################################################
public void addComponent(Component component, int row, int column, int width,int height ){
constraints.gridx=column;
constraints.gridy=row;
constraints.gridwidth=width;
constraints.gridheight=height;
myPanel.add(component,constraints);
}
}