import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/
* Plots trend data over time.
* @author Rick Halterman
* @date 2021-03-10
*/
public class Plotter {
@SuppressWarnings("serial")
private static class PlotPanel extends JPanel {
private Dimension dimension;
private double[] dataSet;
public PlotPanel(double[] data, int width, int height) {
dataSet = data;
dimension = new Dimension(width, height);
setBackground(Color.WHITE);
}
@Override
public Dimension getPreferredSize() {
return dimension;
}
private static double minimum(double[] data) {
double min = Double.MAX_VALUE;
for (double elem : data) {
if (elem < min) {
min = elem;
}
}
return min;
}
private static double maximum(double[] data) {
double max = Double.MIN_VALUE;
for (double elem : data) {
if (elem > max) {
max = elem;
}
}
return max;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int height = getHeight();
int width = getWidth();
// Compute the vertical size of the data
double min = minimum(dataSet);
double max = maximum(dataSet);
double dataHeight = max - min; //range(dataSet);
double widthScale = (double) width/(dataSet.length - 1);
double heightScale = height/dataHeight;
// Draw baseline at y = 0
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0, (int)(height + min*heightScale),
width, (int)(height + min*heightScale));
// Plot data
g.setColor(Color.RED);
for (int i = 1; i < dataSet.length; i++) {
int x1 = (int) Math.round((i - 1) * widthScale),
y1 = height - (int) Math.round((dataSet[i - 1] - min) * heightScale),
x2 = (int) Math.round(i * widthScale),
y2 = height - (int) Math.round((dataSet[i] - min) * heightScale);
if (y1 < 1) {
y1 = 1;
}
if (y2 < 1) {
y2 = 1;
}
g.drawLine(x1, y1, x2, y2);
// Original line drawing:
// g.drawLine((int) Math.round((i - 1) * widthScale),
// height - (int) Math.round((dataSet[i - 1] - min) * heightScale),
// (int) Math.round(i * widthScale),
// height - (int) Math.round((dataSet[i] - min) * heightScale));
}
}
}
/
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically.
* @param data the data set to plot
* @param left the x position of the plot window's left-top corner
* @param top the y position of the plot window's left-top corner
* @param width the width of the graphical window, in pixels
* @param width the width of the graphical window, in pixels
* @param height the height of the graphical window, in pixels
*/
public static void plot(double[] data, int left, int top, int width, int height) {
JFrame window = new JFrame("Plot");
JPanel panel = new PlotPanel(data, width, height);
window.add(panel);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(left, top);
window.setVisible(true);
}
/
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically.
* @param data th.
1. import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JFrame;
import javax.swing.JPanel;
/
* Plots trend data over time.
* @author Rick Halterman
* @date 2021-03-10
*/
public class Plotter {
@SuppressWarnings("serial")
private static class PlotPanel extends JPanel {
private Dimension dimension;
private double[] dataSet;
public PlotPanel(double[] data, int width, int height) {
dataSet = data;
dimension = new Dimension(width, height);
setBackground(Color.WHITE);
}
@Override
public Dimension getPreferredSize() {
return dimension;
}
private static double minimum(double[] data) {
double min = Double.MAX_VALUE;
for (double elem : data) {
if (elem < min) {
min = elem;
2. }
}
return min;
}
private static double maximum(double[] data) {
double max = Double.MIN_VALUE;
for (double elem : data) {
if (elem > max) {
max = elem;
}
}
return max;
}
@Override
public void paintComponent(Graphics g) {
super.paintComponent(g);
int height = getHeight();
int width = getWidth();
// Compute the vertical size of the data
double min = minimum(dataSet);
double max = maximum(dataSet);
double dataHeight = max - min; //range(dataSet);
double widthScale = (double) width/(dataSet.length - 1);
double heightScale = height/dataHeight;
// Draw baseline at y = 0
g.setColor(Color.LIGHT_GRAY);
g.drawLine(0, (int)(height + min*heightScale),
width, (int)(height + min*heightScale));
// Plot data
g.setColor(Color.RED);
for (int i = 1; i < dataSet.length; i++) {
int x1 = (int) Math.round((i - 1) * widthScale),
y1 = height - (int) Math.round((dataSet[i - 1] - min) * heightScale),
x2 = (int) Math.round(i * widthScale),
3. y2 = height - (int) Math.round((dataSet[i] - min) * heightScale);
if (y1 < 1) {
y1 = 1;
}
if (y2 < 1) {
y2 = 1;
}
g.drawLine(x1, y1, x2, y2);
// Original line drawing:
// g.drawLine((int) Math.round((i - 1) * widthScale),
// height - (int) Math.round((dataSet[i - 1] - min) * heightScale),
// (int) Math.round(i * widthScale),
// height - (int) Math.round((dataSet[i] - min) * heightScale));
}
}
}
/
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically.
* @param data the data set to plot
* @param left the x position of the plot window's left-top corner
* @param top the y position of the plot window's left-top corner
* @param width the width of the graphical window, in pixels
* @param width the width of the graphical window, in pixels
* @param height the height of the graphical window, in pixels
*/
public static void plot(double[] data, int left, int top, int width, int height) {
JFrame window = new JFrame("Plot");
JPanel panel = new PlotPanel(data, width, height);
window.add(panel);
window.pack();
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setLocation(left, top);
window.setVisible(true);
}
4. /
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically.
* @param data the data set to plot
* @param width the width of the graphical window, in pixels
* @param height the height of the graphical window, in pixels
*/
public static void plot(double[] data, int width, int height) {
plot(data, 50, 50, width, height);
}
/
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically. This overloaded method accepts an array of integers.
* @param data the data set to plot
* @param width the width of the graphical window, in pixels
* @param height the height of the graphical window, in pixels
*/
public static void plot(int[] data, int width, int height) {
plot(data, 100, 50, width, height);
}
/**
* Plots the data stored in an array in a graphical window
* with a given size. The plot fills the window horizontally
* and vertically. This overloaded method accepts an array of integers.
* @param data the data set to plot
* @param left the x position of the plot window's left-top corner
* @param top the y position of the plot window's left-top corner
* @param width the width of the graphical window, in pixels
* @param height the height of the graphical window, in pixels
*/
public static void plot(int[] data, int left, int top, int width, int height) {
5. // Works, but not as efficient
//plot(Arrays.stream(data).asDoubleStream().toArray(), width, height);
// More efficient for this application
double[] doubleData = new double[data.length];
for (int i = 0; i < doubleData.length; i++) {
doubleData[i] = data[i];
}
plot(doubleData, left, top, width, height);
}
}
754
1
1
0
1
0
0
0
0
1
1
3
3
0
0
0
1
0
0
0
0
26. 619937
288621
309443
341127
275480
88672
87869
320700
192196
201974
166542
173369
52076
38761
203533 Also known as moving average, a rolling average smooths out minor short-term
fluctuations in a data set (see hhttps:/len.wikipedia.org/wiki/Moving. average for more
information). Your task is to write a Java program that creates an array containing data from a
text file. Your program then will create a new array containing the five-element rolling average
of the data in the first array. Use a JFilechooser object as demonstrated in class to allow a user to
select the file to load. The text file contains a sequence of integer values. The first integer
indicates the number of integers that follow. Your program should create the array with a size
equal to the first value in the file and then populate the array with the remaining values. Include
the file plotter. java in your project with the source file you write. It defines a class named P
lotter a that provides a method named p lot. The call Plotter.plot(data,800,300) creates and
displays a window 800 wide and 300 high that plots the data in the integer array named data.
Given the file new-cases-US.text that contains the number of new COVID-19 cases each day in
the United States from January 23, 2020 to February 14, 2022 (source:
https:l/covid.cdc.gov/covid-data-tracker-Centers for Disease Control). After plotting the raw
data, your program should create a new array from the existing array. The new array should
contain the five-element rolling average of the first array. The new array will contain four fewer
elements than the original array. Suppose the original array contains n elements. The following
table shows how to compute the five-element rolling average in the new array:
rawData We wish to compute the five-element rolling average. The following array named
ro11ingAvg will contain the rolling average: rollingAvg Note that the new ro11ingAvg array
contains four fewer elements than the original rawData array. We need to iterate over each
27. position with the ro11ingAvg array, assigning the proper value to each element. This requires
four iterations for this example: Position 0 in ro11ingAvg must be assigned 16 since the
arithmetic mean of 34,10,6,12, and 17 is 15.8 , and this rounds to 16 . Note that you will need to
round properly, not just truncate the answer. rawdata rollingAvg
Position 1 in ro11ingAvg is assigned 12 by a similar process: - Position 2 in ro17ingAvg 12
assigned 11: Position 3 in ro1lingAvg is assigned 11: rawData begin{tabular}{|c|c|c|c|c|c|c|c|}
hline 34 & 10 & 6 & 12 & 17 & 13 & 8 & 4 hline 0 & 1 & 2 & 3 & 4 & 5 & 6 & 7 hline
end{tabular} rollingAvg begin{tabular}{|c|c|c|c|} hline 16 & 12 & 11 & 11 hline 0 & 1 & 2
& 3 hline end{tabular}
Note that the curve in this new plot has the same overall shape of the original, but it is smoother
than the curve in the original plot. Your program should work properly with any supplied data
file that contains at least six data points.