LineChart.java 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package psoAlgoCode;
  2. import java.awt.BorderLayout;
  3. import java.awt.FlowLayout;
  4. import java.awt.event.ActionEvent;
  5. import java.awt.event.ActionListener;
  6. import java.util.Vector;
  7. import javax.swing.JButton;
  8. import javax.swing.JDialog;
  9. import javax.swing.JPanel;
  10. import org.jfree.chart.ChartFactory;
  11. import org.jfree.chart.ChartPanel;
  12. import org.jfree.chart.JFreeChart;
  13. import org.jfree.chart.plot.PlotOrientation;
  14. import org.jfree.data.xy.XYDataset;
  15. import org.jfree.data.xy.XYSeries;
  16. import org.jfree.data.xy.XYSeriesCollection;
  17. /**
  18. * COmpletely unnecessary if not used anymore in the new Version
  19. * @author egert.rolf
  20. *
  21. */
  22. public class LineChart extends JDialog {
  23. private static final long serialVersionUID = 1L;
  24. XYSeriesCollection collections = new XYSeriesCollection();
  25. public LineChart(String applicationTitle, String chartTitle) {
  26. JPanel chartPanel = plotStuff(chartTitle);
  27. JPanel bottomPanel = new JPanel();
  28. setTitle(applicationTitle);
  29. setBounds(400, 100, 800, 800);
  30. setModal(true);
  31. getContentPane().setLayout(new BorderLayout());
  32. getContentPane().add(chartPanel, BorderLayout.CENTER);
  33. chartPanel.setLayout(null);
  34. bottomPanel.setLayout(new FlowLayout(FlowLayout.RIGHT));
  35. getContentPane().add(bottomPanel, BorderLayout.SOUTH);
  36. JButton close = new JButton("Close");
  37. close.addActionListener(new ActionListener() {
  38. @Override
  39. public void actionPerformed(ActionEvent e) {
  40. dispose();
  41. }
  42. });
  43. bottomPanel.add(close);
  44. }
  45. public JPanel plotStuff(String chartTitle) {
  46. XYDataset dataset = collections;
  47. JFreeChart lineChart = ChartFactory.createXYLineChart(chartTitle, "Iterations", "Global Best", dataset,
  48. PlotOrientation.VERTICAL, false, true, false);
  49. return new ChartPanel(lineChart);
  50. }
  51. public void createDataSet(Vector<Double> data, int round) {
  52. final XYSeries series = new XYSeries("Round " + (round), false);
  53. int steps = Constants.MAX_ITERATION;
  54. for (int i = 0; i < steps; i++) {
  55. double temp = data.get(i);
  56. series.add(i + 1, temp);
  57. }
  58. collections.addSeries(series);
  59. }
  60. }