-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDay14.java
More file actions
156 lines (124 loc) · 4.14 KB
/
Day14.java
File metadata and controls
156 lines (124 loc) · 4.14 KB
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
// ---------------------------------------------------
// Author : Benjamin Kataliko Viranga
// Community : Stunt Business
// Community website : www.stuntbusiness.com
//
// 30 Days - Q&A Java basic
// Day 14 : Challenge VIII - Plot your data from your Hasmap "Dictionary" with a line chart
// Day 14 | IG : https://www.instagram.com/benjivrik/
// ----------------------------------------------------
// what would be the output of this program ?
import java.util.HashMap;
import java.util.Random;
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;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;
class Chart extends ApplicationFrame {
/**
*
*/
// https://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it
private static final long serialVersionUID = -8644250950787120854L;
private String chartTitle;
private HashMap<String,int[]> data;
public Chart( String applicationTitle , String chartTitle ) {
// initialize call the parent constructor ApplicationFrame
super(applicationTitle);
this.chartTitle = chartTitle;
// initialize your hasmap
this.data = new HashMap<String,int[]>();
}
/**
*
* @param rowKey
* @param dataArray
*/
public void addData(String rowKey, int[] dataArray)
{
this.data.put(rowKey, dataArray);
}
/**
*
* @return
*/
private DefaultCategoryDataset createDataset() {
DefaultCategoryDataset dataset = new DefaultCategoryDataset( );
for(String rowKey: this.data.keySet())
{
int index = 0; // index for positioning the number
// add the integers in your dataset
for(int value : this.data.get(rowKey))
{
dataset.addValue(value, rowKey,Integer.toString(index));
index++;
}
}
return dataset;
}
/**
*
*/
public void plot()
{
JFreeChart lineChart = ChartFactory.createLineChart(
this.chartTitle,
"Integer ID",
"Integer Value",
createDataset(),
PlotOrientation.VERTICAL,
true,false,false);
ChartPanel chartPanel = new ChartPanel( lineChart );
chartPanel.setPreferredSize( new java.awt.Dimension( 560 , 367 ) );
setContentPane( chartPanel );
}
}
public class Day14
{
static final int SIZE = 25; // size of the data required for this day
public static void main(String[] args)
{
Chart chart = new Chart(
"Data coming from the Hasmap" ,
"Random integers vs Their IDs");
// initialize random 25 Integers from 0 to 20
String rowKey = "first-set";
Random rand = new Random();
int[] firstSet = new int[SIZE];
// for the XY data you will have x = i and y = rand.randInt()
for(int i =0; i < 25 ;i++)
{
firstSet[i] = rand.nextInt(21);
}
//add data in the chart
chart.addData(rowKey,firstSet);
// initialize random 25 Integers from 15 to 30
rowKey = "second-set";
int[] secondSet = new int[SIZE];
// for the XY data you will have x = i and y = rand.randInt()
for(int i =0; i < 25 ;i++)
{
secondSet[i] = rand.nextInt(16)+15;
}
//add data in the chart
chart.addData(rowKey,secondSet);
// initialize random 25 Integers from 10 to 42
rowKey = "third-set";
int[] thirdSet = new int[SIZE];
// for the XY data you will have x = i and y = rand.randInt()
for(int i =0; i < 25 ;i++)
{
thirdSet[i] = rand.nextInt(33)+10;
}
//add data in the chart
chart.addData(rowKey,thirdSet);
// plot the chart
chart.plot();
chart.pack();
RefineryUtilities.centerFrameOnScreen(chart); // center your frame in your screen
chart.setVisible(true);
}
}