Blame view

src/mior/view/MiorGraph.java 2.57 KB
89f70c1ec   glaville   import current mc...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
  package mior.view;
  
  import javax.swing.BoxLayout;
  import javax.swing.JPanel;
  
  import mior.model.IMiorModel;
  import mior.model.IMiorUpdateListener;
  import mior.model.MiorMM;
  import mior.model.MiorOM;
  
  import org.jfree.chart.ChartFactory;
  import org.jfree.chart.ChartPanel;
  import org.jfree.chart.JFreeChart;
  import org.jfree.chart.plot.PlotOrientation;
  import org.jfree.data.category.DefaultCategoryDataset;
  
  public class MiorGraph extends JPanel implements IMiorUpdateListener {
  	
  	private static final long serialVersionUID = 1L;
  	
  	private final DefaultCategoryDataset data1;
  	private final DefaultCategoryDataset data2;
  	private int lastCO2 = 0;
  	private int step;
  	
  	public MiorGraph() {
  		setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
  		
  		data1 = new DefaultCategoryDataset();
  		data2 = new DefaultCategoryDataset();
  		
  		final JFreeChart chart1 = ChartFactory.createLineChart(
  				"Agents numbers",
  				"Step",
  				"Value",
  				data1,
  				PlotOrientation.VERTICAL,
  				true,
  				true,
  				false
  				);
  		
  		ChartPanel panel1 = new ChartPanel(chart1);
  		add(panel1);
  		
  		final JFreeChart chart2 = ChartFactory.createLineChart(
  				"Carbon summary",
  				"Step",
  				"Value (thousands)",
  				data2,
  				PlotOrientation.VERTICAL,
  				true,
  				true,
  				false
  				);
  		
  		ChartPanel panel2 = new ChartPanel(chart2);
  		add(panel2);
  		
  		clear();
  	}
  	
  	public void update(IMiorModel model) {
  		final String stepLabel = Integer.toString(step);
  		
  		data1.addValue(model.getWorld().nbMM, "MM", stepLabel);
  		
  		int nbActifs = 0;
  		int MMtotalC = 0;
  		int OMtotalC = 0;
  		final int CO2 = model.getWorld().CO2;
  			
  		for (MiorMM mm : model.getMMList()) {
  			MMtotalC += mm.getCarbone();
  			/*if (mm.getCarbone() < 0) {
  				System.out.println("MM oops");
  			}*/
  			if (mm.dormance == 0) {
  				nbActifs++;
  			}
  		}
  		
  		for (MiorOM om : model.getOMList()) {
  			OMtotalC += om.getCarbone();
  			/*if (om.getCarbone() < 0) {
  				System.out.println("OM oops");
  			}*/
  		}
  		
  		data1.addValue(nbActifs, "MM Actifs", stepLabel);
  		
  		data2.addValue(MMtotalC / 1000, "CMM", stepLabel);
  		data2.addValue(OMtotalC / 1000, "COM", stepLabel);
  		data2.addValue(CO2 / 1000, "CO2", stepLabel);
  		data2.addValue((CO2 + MMtotalC + OMtotalC) / 1000, "Total", stepLabel);
  		
  		// TODO : HACK FOR MULTIPLE EXECUTIONS
  		lastCO2 = Math.min(CO2, lastCO2);
  			
  		data2.addValue((CO2 - lastCO2) / 100, "dCO2 (x10)", stepLabel);
  		lastCO2 = CO2;
  		step++;
  	}
  	
  	public void clear() {
  		lastCO2 = 0;
  		step = 0;
  		data1.clear();
  		data2.clear();
  	}
  	
  	@Override
  	public void onMiorModelUpdated(IMiorModel model) {
  		update(model);
  		repaint();
  	}
  }