1 | /************************************************************************************ |
---|
2 | * Copyright (C) 2008 by Politehnica University of Bucharest and Rutgers University |
---|
3 | * All rights reserved. |
---|
4 | * Refer to LICENSE for terms and conditions of use. |
---|
5 | ***********************************************************************************/ |
---|
6 | package vnsim.network.dsrc; |
---|
7 | |
---|
8 | import org.jfree.ui.ApplicationFrame; |
---|
9 | |
---|
10 | import java.awt.event.ActionEvent; |
---|
11 | import java.awt.event.ActionListener; |
---|
12 | import java.util.ArrayList; |
---|
13 | |
---|
14 | import javax.swing.Spring; |
---|
15 | import javax.swing.SpringLayout; |
---|
16 | import javax.swing.JPanel; |
---|
17 | import javax.swing.JTable; |
---|
18 | import javax.swing.JScrollPane; |
---|
19 | import javax.swing.JButton; |
---|
20 | import javax.swing.JLabel; |
---|
21 | import org.jfree.chart.*; |
---|
22 | import org.jfree.chart.axis.*; |
---|
23 | import org.jfree.chart.plot.*; |
---|
24 | import org.jfree.chart.renderer.xy.*; |
---|
25 | import org.jfree.data.xy.*; |
---|
26 | import org.jfree.data.time.*; |
---|
27 | import org.jfree.chart.entity.StandardEntityCollection; |
---|
28 | import org.jfree.chart.ChartUtilities; |
---|
29 | import org.jfree.chart.labels.StandardXYToolTipGenerator; |
---|
30 | import org.jfree.chart.imagemap.*; |
---|
31 | import org.jfree.chart.labels.CustomXYToolTipGenerator; |
---|
32 | import org.jfree.chart.urls.XYURLGenerator; |
---|
33 | import java.util.*; |
---|
34 | import java.text.SimpleDateFormat; |
---|
35 | |
---|
36 | public class DSRCChart extends ApplicationFrame |
---|
37 | { |
---|
38 | public DSRCChart(final String title,final ArrayList<DSRCStatisticComponent> results) |
---|
39 | { |
---|
40 | super(title); |
---|
41 | |
---|
42 | SpringLayout l = new SpringLayout(); |
---|
43 | setLayout(l); |
---|
44 | SpringLayout.Constraints constraint; |
---|
45 | |
---|
46 | XYSeries s1=new XYSeries("OK"); |
---|
47 | XYSeries s2=new XYSeries("Collision"); |
---|
48 | XYSeries s3=new XYSeries("Corrupted"); |
---|
49 | XYSeries s4=new XYSeries("Lost RX"); |
---|
50 | XYSeries s5=new XYSeries("Lost TX"); |
---|
51 | XYSeries s6=new XYSeries("Lost Per"); |
---|
52 | XYSeries s17=new XYSeries("Lost Weak"); |
---|
53 | |
---|
54 | s1.add(0,0); |
---|
55 | s2.add(0,0); |
---|
56 | s3.add(0,0); |
---|
57 | s4.add(0,0); |
---|
58 | s5.add(0,0); |
---|
59 | s6.add(0,0); |
---|
60 | s17.add(0,0); |
---|
61 | for(int i=1;i<results.size();i++) |
---|
62 | { |
---|
63 | s1.add(i,(double)100*results.get(i).getOkPackets()/results.get(i).getTotalPackets()); |
---|
64 | s2.add(i,(double)100*results.get(i).getCollisionPackets()/results.get(i).getTotalPackets()); |
---|
65 | s3.add(i,(double)100*results.get(i).getCorruptedPackets()/results.get(i).getTotalPackets()); |
---|
66 | s4.add(i,(double)100*results.get(i).getLostRxPackets()/results.get(i).getTotalPackets()); |
---|
67 | s5.add(i,(double)100*results.get(i).getLostTxPackets()/results.get(i).getTotalPackets()); |
---|
68 | s6.add(i,(double)100*results.get(i).getLostPerPackets()/results.get(i).getTotalPackets()); |
---|
69 | s17.add(i,(double)100*results.get(i).getWeakPackets()/results.get(i).getTotalPackets()); |
---|
70 | } |
---|
71 | |
---|
72 | XYSeriesCollection dataset = new XYSeriesCollection(); |
---|
73 | dataset.addSeries(s1); |
---|
74 | dataset.addSeries(s2); |
---|
75 | dataset.addSeries(s3); |
---|
76 | dataset.addSeries(s4); |
---|
77 | dataset.addSeries(s5); |
---|
78 | dataset.addSeries(s6); |
---|
79 | dataset.addSeries(s17); |
---|
80 | |
---|
81 | final JFreeChart chart = ChartFactory.createXYLineChart( |
---|
82 | "Received Packets lost/ok", // chart title |
---|
83 | "Category", // domain axis label |
---|
84 | "Value", // range axis label |
---|
85 | dataset, // data |
---|
86 | PlotOrientation.VERTICAL, |
---|
87 | true, // include legend |
---|
88 | true, |
---|
89 | false |
---|
90 | ); |
---|
91 | |
---|
92 | final XYPlot plot = chart.getXYPlot(); |
---|
93 | final NumberAxis domainAxis = new NumberAxis("Simulation minute"); |
---|
94 | final NumberAxis rangeAxis = new NumberAxis("Percentage of total packets/sim minute"); |
---|
95 | plot.setDomainAxis(domainAxis); |
---|
96 | plot.setRangeAxis(rangeAxis); |
---|
97 | |
---|
98 | /* |
---|
99 | try { |
---|
100 | ChartUtilities.saveChartAsJPEG( |
---|
101 | new java.io.File("Test.jpg"), chart, 500, 300); |
---|
102 | } catch (java.io.IOException exc) { |
---|
103 | System.err.println("Error writing image to file"); |
---|
104 | } |
---|
105 | */ |
---|
106 | |
---|
107 | final ChartPanel chartPanel = new ChartPanel(chart); |
---|
108 | chartPanel.setPreferredSize(new java.awt.Dimension(500, 270)); |
---|
109 | //setContentPane(chartPanel); |
---|
110 | |
---|
111 | constraint = l.getConstraints(chartPanel); |
---|
112 | constraint.setX(Spring.constant(0)); |
---|
113 | constraint.setY(Spring.constant(0)); |
---|
114 | constraint.setWidth(Spring.constant(500)); |
---|
115 | constraint.setHeight(Spring.constant(270)); |
---|
116 | add(chartPanel); |
---|
117 | |
---|
118 | |
---|
119 | XYSeries s7=new XYSeries("FV"); |
---|
120 | XYSeries s8=new XYSeries("NFV"); |
---|
121 | XYSeries s9=new XYSeries("LAV"); |
---|
122 | XYSeries s10=new XYSeries("RAV"); |
---|
123 | |
---|
124 | for(int i=0;i<results.size();i++) |
---|
125 | { |
---|
126 | s7.add(i,results.get(i).getReceivedFromFV()); |
---|
127 | s8.add(i,results.get(i).getReceivedFromNFV()); |
---|
128 | s9.add(i,results.get(i).getReceivedFromLAV()); |
---|
129 | s10.add(i,results.get(i).getReceivedFromRAV()); |
---|
130 | } |
---|
131 | |
---|
132 | XYSeriesCollection dataset2 = new XYSeriesCollection(); |
---|
133 | dataset2.addSeries(s7); |
---|
134 | dataset2.addSeries(s8); |
---|
135 | dataset2.addSeries(s9); |
---|
136 | dataset2.addSeries(s10); |
---|
137 | |
---|
138 | final JFreeChart chart2 = ChartFactory.createXYLineChart( |
---|
139 | "Received Packets by transmitter", // chart title |
---|
140 | "Category", // domain axis label |
---|
141 | "Value", // range axis label |
---|
142 | dataset2, // data |
---|
143 | PlotOrientation.VERTICAL, |
---|
144 | true, // include legend |
---|
145 | true, |
---|
146 | false |
---|
147 | ); |
---|
148 | |
---|
149 | final XYPlot plot2 = chart2.getXYPlot(); |
---|
150 | final NumberAxis domainAxis2 = new NumberAxis("Simulation time(min)"); |
---|
151 | final NumberAxis rangeAxis2 = new NumberAxis("Number of packets/sim minute"); |
---|
152 | plot2.setDomainAxis(domainAxis2); |
---|
153 | plot2.setRangeAxis(rangeAxis2); |
---|
154 | |
---|
155 | final ChartPanel chartPanel2 = new ChartPanel(chart2); |
---|
156 | chartPanel2.setPreferredSize(new java.awt.Dimension(500, 270)); |
---|
157 | |
---|
158 | constraint = l.getConstraints(chartPanel2); |
---|
159 | constraint.setX(Spring.constant(500)); |
---|
160 | constraint.setY(Spring.constant(0)); |
---|
161 | constraint.setWidth(Spring.constant(500)); |
---|
162 | constraint.setHeight(Spring.constant(270)); |
---|
163 | add(chartPanel2); |
---|
164 | |
---|
165 | |
---|
166 | XYSeries s11=new XYSeries("FV"); |
---|
167 | XYSeries s12=new XYSeries("NFV"); |
---|
168 | XYSeries s13=new XYSeries("LAV"); |
---|
169 | XYSeries s14=new XYSeries("RAV"); |
---|
170 | |
---|
171 | for(int i=0;i<results.size();i++) |
---|
172 | { |
---|
173 | s11.add(i,results.get(i).getIRTFromFV()); |
---|
174 | s12.add(i,results.get(i).getIRTFromNFV()); |
---|
175 | s13.add(i,results.get(i).getIRTFromLAV()); |
---|
176 | s14.add(i,results.get(i).getIRTFromRAV()); |
---|
177 | } |
---|
178 | |
---|
179 | XYSeriesCollection dataset3 = new XYSeriesCollection(); |
---|
180 | dataset3.addSeries(s11); |
---|
181 | dataset3.addSeries(s12); |
---|
182 | dataset3.addSeries(s13); |
---|
183 | dataset3.addSeries(s14); |
---|
184 | |
---|
185 | final JFreeChart chart3 = ChartFactory.createXYLineChart( |
---|
186 | "IRT by transmitter", // chart title |
---|
187 | "Category", // domain axis label |
---|
188 | "Value", // range axis label |
---|
189 | dataset3, // data |
---|
190 | PlotOrientation.VERTICAL, |
---|
191 | true, // include legend |
---|
192 | true, |
---|
193 | false |
---|
194 | ); |
---|
195 | |
---|
196 | final XYPlot plot3 = chart3.getXYPlot(); |
---|
197 | final NumberAxis domainAxis3 = new NumberAxis("Simulation time (min)"); |
---|
198 | final NumberAxis rangeAxis3 = new NumberAxis("IRT (sec)/sim minute"); |
---|
199 | plot3.setDomainAxis(domainAxis3); |
---|
200 | plot3.setRangeAxis(rangeAxis3); |
---|
201 | |
---|
202 | final ChartPanel chartPanel3 = new ChartPanel(chart3); |
---|
203 | chartPanel3.setPreferredSize(new java.awt.Dimension(500, 270)); |
---|
204 | |
---|
205 | constraint = l.getConstraints(chartPanel3); |
---|
206 | constraint.setX(Spring.constant(0)); |
---|
207 | constraint.setY(Spring.constant(270)); |
---|
208 | constraint.setWidth(Spring.constant(500)); |
---|
209 | constraint.setHeight(Spring.constant(270)); |
---|
210 | add(chartPanel3); |
---|
211 | |
---|
212 | |
---|
213 | XYSeries s15=new XYSeries("Emergency Message Recv"); |
---|
214 | XYSeries s16=new XYSeries("Emergency Message Sent"); |
---|
215 | |
---|
216 | for(int i=0;i<results.size();i++) |
---|
217 | { |
---|
218 | s15.add(i,results.get(i).getEmergencyReceived()); |
---|
219 | s16.add(i,results.get(i).getEmergencySent()); |
---|
220 | } |
---|
221 | |
---|
222 | XYSeriesCollection dataset4 = new XYSeriesCollection(); |
---|
223 | dataset4.addSeries(s15); |
---|
224 | dataset4.addSeries(s16); |
---|
225 | |
---|
226 | final JFreeChart chart4 = ChartFactory.createXYLineChart( |
---|
227 | "Emergency Messages", // chart title |
---|
228 | "Category", // domain axis label |
---|
229 | "Value", // range axis label |
---|
230 | dataset4, // data |
---|
231 | PlotOrientation.VERTICAL, |
---|
232 | true, // include legend |
---|
233 | true, |
---|
234 | false |
---|
235 | ); |
---|
236 | |
---|
237 | final XYPlot plot4 = chart4.getXYPlot(); |
---|
238 | final NumberAxis domainAxis4 = new NumberAxis("Simulation time (min)"); |
---|
239 | final NumberAxis rangeAxis4 = new NumberAxis("Number of messages/sim minute"); |
---|
240 | plot4.setDomainAxis(domainAxis4); |
---|
241 | plot4.setRangeAxis(rangeAxis4); |
---|
242 | |
---|
243 | final ChartPanel chartPanel4 = new ChartPanel(chart4); |
---|
244 | chartPanel4.setPreferredSize(new java.awt.Dimension(500, 270)); |
---|
245 | |
---|
246 | constraint = l.getConstraints(chartPanel4); |
---|
247 | constraint.setX(Spring.constant(500)); |
---|
248 | constraint.setY(Spring.constant(270)); |
---|
249 | constraint.setWidth(Spring.constant(500)); |
---|
250 | constraint.setHeight(Spring.constant(270)); |
---|
251 | add(chartPanel4); |
---|
252 | |
---|
253 | //number of cars per every simulation minute |
---|
254 | |
---|
255 | if(results.size()!=0) |
---|
256 | { |
---|
257 | String[] columnNames= { "Sim minute","No of cars"}; |
---|
258 | Object data[][]=new Object[results.size()][2]; |
---|
259 | |
---|
260 | for(int j=0;j<results.size();j++) |
---|
261 | { |
---|
262 | data[j][0]=j; |
---|
263 | data[j][1]=new Integer(results.get(j).getNumberOfCars()); |
---|
264 | } |
---|
265 | |
---|
266 | |
---|
267 | final JTable carsTable=new JTable(data,columnNames); |
---|
268 | JScrollPane scrollPane = new JScrollPane(carsTable); |
---|
269 | |
---|
270 | constraint=l.getConstraints(scrollPane); |
---|
271 | constraint.setX(Spring.constant(1020)); |
---|
272 | constraint.setY(Spring.constant(0)); |
---|
273 | constraint.setWidth(Spring.constant(180)); |
---|
274 | constraint.setHeight(Spring.constant(550)); |
---|
275 | add(scrollPane); |
---|
276 | } |
---|
277 | |
---|
278 | JButton close=new JButton("Close"); |
---|
279 | |
---|
280 | constraint=l.getConstraints(close); |
---|
281 | constraint.setX(Spring.constant(10)); |
---|
282 | constraint.setY(Spring.constant(550)); |
---|
283 | constraint.setWidth(Spring.constant(70)); |
---|
284 | constraint.setHeight(Spring.constant(30)); |
---|
285 | |
---|
286 | close.addActionListener(new ActionListener() { |
---|
287 | public void actionPerformed(ActionEvent ev) { |
---|
288 | dispose(); |
---|
289 | System.exit(0); |
---|
290 | } |
---|
291 | }); |
---|
292 | |
---|
293 | add(close); |
---|
294 | |
---|
295 | final JLabel written=new JLabel("Statistical data written to file"); |
---|
296 | |
---|
297 | constraint=l.getConstraints(written); |
---|
298 | constraint.setX(Spring.constant(220)); |
---|
299 | constraint.setY(Spring.constant(550)); |
---|
300 | constraint.setWidth(Spring.constant(350)); |
---|
301 | constraint.setHeight(Spring.constant(30)); |
---|
302 | written.setVisible(false); |
---|
303 | |
---|
304 | |
---|
305 | |
---|
306 | JButton writeToFile=new JButton("Write to file"); |
---|
307 | |
---|
308 | constraint=l.getConstraints(writeToFile); |
---|
309 | constraint.setX(Spring.constant(90)); |
---|
310 | constraint.setY(Spring.constant(550)); |
---|
311 | constraint.setWidth(Spring.constant(120)); |
---|
312 | constraint.setHeight(Spring.constant(30)); |
---|
313 | |
---|
314 | |
---|
315 | add(written); |
---|
316 | writeToFile.addActionListener(new ActionListener() { |
---|
317 | public void actionPerformed(ActionEvent ev) { |
---|
318 | |
---|
319 | |
---|
320 | try{ |
---|
321 | ExcelWriter exwriter=new ExcelWriter(); |
---|
322 | |
---|
323 | Calendar cal=Calendar.getInstance(); |
---|
324 | SimpleDateFormat formatter=new SimpleDateFormat("dd_MM_yyyy"); |
---|
325 | String time=formatter.format(cal.getTime()); |
---|
326 | |
---|
327 | Calendar cal2=Calendar.getInstance(); |
---|
328 | SimpleDateFormat formatter2=new SimpleDateFormat("HH_mm_ss"); |
---|
329 | String date=formatter2.format(cal2.getTime()); |
---|
330 | |
---|
331 | exwriter.write(date+"_"+time+".xls",results); |
---|
332 | written.setText("Statistical data written to file "+date+"_"+time+".xls"); |
---|
333 | written.setVisible(true); |
---|
334 | } |
---|
335 | catch(Exception e){System.out.println(e.getMessage());} |
---|
336 | |
---|
337 | |
---|
338 | } |
---|
339 | }); |
---|
340 | |
---|
341 | add(writeToFile); |
---|
342 | } |
---|
343 | |
---|
344 | } |
---|