-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotData.java
509 lines (380 loc) · 16.4 KB
/
PlotData.java
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
// CSE 360: Group Assignment
// Group Members: Imaan Ahmed, May Lister, Cera Monson, Italo Pennella
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.io.*;
import java.net.MalformedURLException;
import java.util.StringJoiner;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.WindowConstants;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import java.net.URL;
public class PlotData extends JFrame
{
//===============================================PLOT DATA PANEL===============================================
public PlotData(String title)
{
super(title);
// Create dataset
XYDataset dataset = createDataset();
// Create chart
JFreeChart chart = ChartFactory.createTimeSeriesChart(
"Temperature vs. Date Across Various Cities",
"Date", "Temperature", dataset);
//Changes background color
XYPlot plot = (XYPlot)chart.getPlot();
plot.setBackgroundPaint(Color.LIGHT_GRAY);
// Create Panel
ChartPanel panel = new ChartPanel(chart);
setContentPane(panel);
}
static String data [][] = {{"Something", "9/21/2021", "90", "F"}};
static String column[]={"City Name","Date","Temperature","Type"};
private static XYDataset createDataset() {
TimeSeriesCollection dataset = new TimeSeriesCollection();
TimeSeries series1 = new TimeSeries("Cities");
try
{
for (int i = 0; i < data.length; i++)
{
TimeSeries series2 = new TimeSeries("Phoenix");
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("M/d/yyyy");
String date = simpleDateFormat.format(new Date());
Date d = simpleDateFormat.parse(data[i][1]);
series1.add(new Day(d), Double.parseDouble(data[i][2]));
}
} catch (ParseException e)
{
e.printStackTrace();
}
dataset.addSeries(series1);
return dataset;
}
public static void main(String[] args) throws InterruptedException, MalformedURLException, ClassNotFoundException, UnsupportedLookAndFeelException, InstantiationException, IllegalAccessException
{
UIManager.setLookAndFeel("javax.swing.plaf.nimbus.NimbusLookAndFeel"); //Theme for the Windows, feel free to change it back to windows
JProgressBar progressBar; //Progress bar variable
/*URL FOR SPLASHSCREEN*/
String splashURL = "https://th.bing.com/th/id/R3d8064758e54ec662e076b6ca54aa90e?rik=tZBabuOiEIiJdg&riu=http%3a%2f%2f38.media.tumblr.com%2f439a4606156abed8c2d04944d4efd12f%2ftumblr_n6kholb70q1sd011wo1_1280.gif&ehk=nahe56kJGROkVr9BULvIZzIWyumeW1pbVTYTXEGRoXw%3d&risl=&pid=ImgRaw";
String splashURL1 = "https://th.bing.com/th/id/R.38c10d9ad1341f6413b28834ee8a5c69?rik=zdZb85N2fijuZA&riu=http%3a%2f%2ffreefrontend.com%2fassets%2fimg%2fcss-loaders%2filluminati-rainbow-loading.gif&ehk=XppS1B5%2f1AVwp4YXYb0ssOEKaCFYfaYvBLCno3d7it8%3d&risl=&pid=ImgRaw";
String splashURL2 = "https://drive.google.com/file/d/1yRiN0QkHIoiQf9NBKiqXZtE0o3hPTLLC/view?usp=sharing";
/******SPLASH SCREEN******/
JWindow splashScreen = new JWindow();
splashScreen.getContentPane().add(new JLabel("", new ImageIcon(new URL(splashURL1)), SwingConstants.CENTER));
splashScreen.setBounds(290, 160, 400, 300);
splashScreen.setVisible(true);
splashScreen.setSize(390, 310);
splashScreen.setLayout(null);
splashScreen.setLocationRelativeTo(null); //Sets in middle of user screen
progressBar = new JProgressBar();
progressBar.setMaximum(100);
progressBar.setMinimum(0);
splashScreen.add(progressBar);
progressBar.setBounds(-10, 300, 404, 14); //Bounds of Progress Bar
progressBar.setPreferredSize(new Dimension(310, 30)); //Size of Progress BAr
//Runnable inner class allows thread to move along bar
Thread thread = new Thread()
{
public void run()
{
int i = 0;
while (i <= 100) //Percentage by which bar goes up
{
progressBar.setValue(i);
try {
sleep(40);
} catch (InterruptedException e) {
e.printStackTrace();
}
i++;
}
}
};
thread.start();
Thread.sleep(5000); //Display Screen for a bit
splashScreen.setVisible(false); //Removes splashscreen load
splashScreen.dispose(); //Deletes frame
/******SPLASH SCREEN******/
/***** Create JFrame *****/
JFrame baseFrame = new JFrame(); //Create baseFrame of Application
baseFrame.setSize(700, 700);
baseFrame.setTitle("CSE 360");
baseFrame.setLocationRelativeTo(null); //sets location to center of user screen
baseFrame.setLayout(new GridLayout(1,1));
/***** Add tabs to the frame *****/
//baseFrame.add(menu, BorderLayout.EAST);
JTabbedPane tabs = new JTabbedPane(JTabbedPane.LEFT);
JPanel homePanel = new JPanel();
JPanel homePanel2 = new JPanel();
JPanel aboutPanel = new JPanel();
JPanel loadDataPanel = new JPanel();
JPanel addDataPanel = new JPanel();
JPanel saveDataPanel = new JPanel();
JPanel plotDataPanel = new JPanel();
//===============================================HOME PANEL===============================================
//JLabel Welcome = new JLabel("\n Welcome " );
//homePanel.add(Welcome);
tabs.addTab("Home", homePanel);
JTable tempor = new JTable();
JScrollPane jsp = createTables(tempor);
homePanel.add(jsp);
//===============================================ABOUT PANEL===============================================
//panel and color
JPanel pain = new JPanel();
pain.setBackground(Color.WHITE);
//creating text
JLabel group = new JLabel("\t\tTeam #28\n\n");
group.setFont(new Font("Times New Roman", Font.BOLD,18));
JLabel name1 = new JLabel("\t\t\tImaan Ahmed\n");
name1.setFont(new Font("Times New Roman", Font.PLAIN,15));
JLabel name2 = new JLabel("\t\t\tMay Lister\n");
name2.setFont(new Font("Times New Roman", Font.PLAIN,15));
JLabel name3 = new JLabel("\t\t\tCera Monson\n");
name3.setFont(new Font("Times New Roman", Font.PLAIN,15));
JLabel name4 = new JLabel("\t\t\tItalo Pennella");
name4.setFont(new Font("Times New Roman", Font.PLAIN,15));
//setting layout and adding text
BoxLayout Box = new BoxLayout(pain, BoxLayout.Y_AXIS);
pain.setLayout(Box);
pain.add(group);
pain.add(name1);
pain.add(name2);
pain.add(name3);
pain.add(name4);
//adding panel to tabs
tabs.addTab("About", pain);
//===============================================LOAD PANEL===============================================
//add load tab
tabs.addTab("Load Data", loadDataPanel);
//setting up text field for user to enter filename
JLabel selectFile = new JLabel("Select File");
loadDataPanel.add(selectFile);
JTextField fileInput = new JTextField(10);
loadDataPanel.add(fileInput);
//select file button
JButton select = new JButton();
select.setSize(400,100);
select.setText("Select File");
select.setVisible(true);
loadDataPanel.add(select);
//result of pushing button
select.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String file = fileInput.getText();
try
{
//file reader
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);
String line = br.readLine();
line = br.readLine();
//counting amount of data for new array
int count = 0;
while(line != null)
{
line = br.readLine();
count++;
}
//second file reader to
FileReader fr2 = new FileReader(file);
BufferedReader br2 = new BufferedReader(fr2);
String line2 = br2.readLine();
line2 = br2.readLine(); //to skip over column names
String[][] data2 = new String[count][4]; //creating new array
int i = 0;
//read and add data to new array
while (line2 != null)
{
String[] words = line2.split(",", 4);
data2[i][0] = words[0];
data2[i][1] = words[1];
data2[i][2] = words[2];
data2[i][3] = words[3];
i++;
line2 = br2.readLine();
}
//replace data
changer(data2);
//close readers
br.close();
br2.close();
}
catch (IOException e2)
{
System.out.println("IO Exception");
}
//update home panel with new data
homePanel.removeAll();
homePanel.add(createTables(tempor));
}
});
//===============================================ADD PANEL===============================================
tabs.addTab("Add Data", addDataPanel);
JTable jt_add =new JTable(data,column);
//jt.setBounds(30,40,200,300);
JScrollPane sp2=new JScrollPane(jt_add);
JLabel cityName = new JLabel("City");
JLabel date = new JLabel("Date");
JLabel tempBtn = new JLabel("Temperature");
JLabel degreeType = new JLabel("Degree Type(C/F)");
JTextField cityText = new JTextField("City",10);
JTextField dateText = new JTextField("Date",10);
JTextField tempText = new JTextField("Temperature",10);
JTextField degreeText = new JTextField("Degree Type",10);
addDataPanel.add(cityName);
addDataPanel.add(cityText);
addDataPanel.add(date);
addDataPanel.add(dateText);
addDataPanel.add(tempBtn);
addDataPanel.add(tempText);
addDataPanel.add(degreeType);
addDataPanel.add(degreeText);
JButton addRowBtn = new JButton("Submit");
addDataPanel.add(addRowBtn);
String cityInput = cityText.getText();
//final int[] count = {1};
final int[] tail = {0};
addRowBtn.addActionListener(new ActionListener() {
//int count=1;//helps array grow with each btn click
int i,j;
String temp = cityText.getText();
public void actionPerformed(ActionEvent e) {
int count = 1;
String cityInput = cityText.getText();
String dateInput = dateText.getText();
String tempInput = tempText.getText();
String degreeInput = degreeText.getText();
int n = data.length;//row size
int m = data[0].length;//col size
int index=0;
String[][] data2 = new String[count +data.length][4]; //new array declared
for ( i = 0; i < data.length;i++ ) {
for ( j=0; j < data[0].length;j++) { //we used num[0] because we need the length of the rows not the columns
data2[i][j] = data[i][j];
}
}
for (i = data.length; i < data.length+ count; i++ ) {//growing
for (j = 0; j < 4; j++) {
data2[i][0] = cityInput;
data2[i][1] = dateInput;
data2[i][2] = tempInput;
data2[i][3] = degreeInput;
}
}
count++;//updates data length
changer(data2);
homePanel.removeAll();
homePanel.add(createTables(tempor));
}//end of actionlistener
});
//===============================================SAVE PANEL===============================================
//add tab
tabs.addTab("Save Data", saveDataPanel);
//add text field for name of save file
JTextField userInput = new JTextField(8);
saveDataPanel.add(userInput);
JLabel label1 = new JLabel(".csv");
saveDataPanel.add(label1);
//add button to activate save
JButton b1 = new JButton();
saveDataPanel.add(b1);
b1.setSize(400,400);
b1.setText("Save data");
b1.setVisible(true);
//action listener for button
b1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
String fileName = userInput.getText();
//writing data to new file
try (BufferedWriter bw = new BufferedWriter(new FileWriter(new File(fileName +".csv"))))
{
StringJoiner joiner = new StringJoiner(",");
//writing column names to file
for (int col = 0; col < column.length; col++)
{
joiner.add(column[col]);
}
bw.write(joiner.toString());
bw.newLine();
//writing data to file
for(int r = 0; r<data.length; r++)
{
joiner = new StringJoiner(",");
for (int c = 0; c<data[r].length; c++)
{
Object obj = data[r][c];
String value = obj == null ? "null" : obj.toString();
joiner.add(value);
}
bw.write(joiner.toString());
bw.newLine();
}
}
catch (IOException exp)
{
exp.printStackTrace();
}
}
});
//Add JPanel for tables
tabs.addTab("Plot Data", plotDataPanel);
//baseFrame layout
tabs.setBorder(BorderFactory.createTitledBorder("Menu"));
baseFrame.add(tabs, BorderLayout.CENTER);
baseFrame.setVisible(true);
baseFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//stop when closed
//===============================================PLOT DATA PANEL===============================================
JButton plot = new JButton();
plot.setSize(400,400);
plot.setText("Plot data");
plot.setVisible(true);
plotDataPanel.add(plot);
//action listener for button
plot.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
XYDataset dataset = createDataset();
SwingUtilities.invokeLater(() -> {
PlotData example = new PlotData("Time Series Chart");
example.setSize(1000, 500);
example.setLocationRelativeTo(null);
example.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
example.setVisible(true);
});
}
});
}
public static void changer(String[][] temps)
{
data = temps;
}
public static JScrollPane createTables(JTable jazz)
{
jazz = new JTable(data,column);
JScrollPane sp=new JScrollPane(jazz);
return sp;
}
}//end of Main