source: src/main/java/genius/gui/progress/session/ProgressChart.java

Last change on this file was 1, checked in by Wouter Pasman, 6 years ago

Initial import : Genius 9.0.0

File size: 2.6 KB
Line 
1package genius.gui.progress.session;
2
3import java.awt.BorderLayout;
4import java.awt.Dimension;
5import java.util.ArrayList;
6import java.util.List;
7
8import javax.swing.JPanel;
9import javax.swing.SwingUtilities;
10import javax.swing.event.ListDataEvent;
11import javax.swing.event.ListDataListener;
12
13import org.jfree.chart.ChartFactory;
14import org.jfree.chart.ChartPanel;
15import org.jfree.chart.JFreeChart;
16import org.jfree.chart.plot.PlotOrientation;
17import org.jfree.chart.plot.XYPlot;
18import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
19import org.jfree.data.xy.XYDataItem;
20import org.jfree.data.xy.XYSeries;
21import org.jfree.data.xy.XYSeriesCollection;
22
23import genius.core.parties.PartyWithUtility;
24
25/**
26 * Shows progress as a plot where each party's utility is set vertically against
27 * rounds horizontally. This assumes that only items are being added to the
28 * OutcomesModel, one at a time.
29 */
30@SuppressWarnings("serial")
31public class ProgressChart extends JPanel {
32
33 final XYSeriesCollection dataset = new XYSeriesCollection();
34 private OutcomesListModel model;
35 private List<XYSeries> datasets = new ArrayList<XYSeries>();
36
37 public ProgressChart(OutcomesListModel model) {
38 this.model = model;
39 setLayout(new BorderLayout());
40 setMinimumSize(new Dimension(300, 300));
41
42 for (PartyWithUtility party : model.getParties()) {
43 XYSeries series = new XYSeries(party.getID().toString());
44 datasets.add(series);
45 dataset.addSeries(series);
46 }
47
48 JFreeChart xylineChart = ChartFactory.createXYLineChart("", "Round", "Utility", dataset,
49 PlotOrientation.VERTICAL, true, true, false);
50
51 ChartPanel chartPanel = new ChartPanel(xylineChart);
52 final XYPlot plot = xylineChart.getXYPlot();
53 XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
54 plot.setRenderer(renderer);
55
56 // BAD: JFreeChart doesn't have a model so we must connect it directly
57 model.addListDataListener(new ListDataListener() {
58
59 @Override
60 public void intervalRemoved(ListDataEvent e) {
61 }
62
63 @Override
64 public void intervalAdded(ListDataEvent e) {
65 addNewOutcome();
66 }
67
68 @Override
69 public void contentsChanged(ListDataEvent e) {
70 }
71
72 });
73
74 add(chartPanel, BorderLayout.CENTER);
75 }
76
77 private void addNewOutcome() {
78 SwingUtilities.invokeLater(new Runnable() {
79
80 @Override
81 public void run() {
82 Outcome outcome = model.get(model.getSize() - 1);// last
83 for (int n = 0; n < datasets.size(); n++) {
84 // FIXME use datasets.get(n).getMaximumItemCount() or so
85 datasets.get(n).add(new XYDataItem((double) outcome.getRound(),
86 (double) outcome.getDiscountedUtilities().get(n)));
87 }
88 }
89 });
90 }
91
92}
Note: See TracBrowser for help on using the repository browser.