1 | package genius.gui.progress.session;
|
---|
2 |
|
---|
3 | import java.awt.BorderLayout;
|
---|
4 | import java.awt.Dimension;
|
---|
5 | import java.util.ArrayList;
|
---|
6 | import java.util.List;
|
---|
7 |
|
---|
8 | import javax.swing.JPanel;
|
---|
9 | import javax.swing.SwingUtilities;
|
---|
10 | import javax.swing.event.ListDataEvent;
|
---|
11 | import javax.swing.event.ListDataListener;
|
---|
12 |
|
---|
13 | import org.jfree.chart.ChartFactory;
|
---|
14 | import org.jfree.chart.ChartPanel;
|
---|
15 | import org.jfree.chart.JFreeChart;
|
---|
16 | import org.jfree.chart.plot.PlotOrientation;
|
---|
17 | import org.jfree.chart.plot.XYPlot;
|
---|
18 | import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
|
---|
19 | import org.jfree.data.xy.XYDataItem;
|
---|
20 | import org.jfree.data.xy.XYSeries;
|
---|
21 | import org.jfree.data.xy.XYSeriesCollection;
|
---|
22 |
|
---|
23 | import 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")
|
---|
31 | public 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 | }
|
---|