-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDrawHistogram.java
More file actions
62 lines (51 loc) · 1.63 KB
/
DrawHistogram.java
File metadata and controls
62 lines (51 loc) · 1.63 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
package statisticstools;
import java.awt.*;
import javax.swing.*;
public class DrawHistogram extends JPanel {
private String _title = "";
private double[] _values;
private double _x_start, _x_end, _x_interval;
private int _y_start, _y_end, _y_interval;
public DrawHistogram(String title, double[] values, double x_start, double x_end, double x_interval, int y_start, int y_end, int y_interval) {
_title = title;
_values = values;
_x_start = x_start;
_x_end = x_end;
_x_interval = x_interval;
_y_start = y_start;
_y_end = y_end;
_y_interval = y_interval;
}
public void paintComponent(Graphics g) {
int[] calcArray = calculateArray();
int calcArraySize = calcArray.length;
int graphInterval = (int) Math.ceil(800 / (double) calcArraySize);
super.paintComponent(g);
this.setBackground(Color.WHITE);
for (int i = 0; i < calcArraySize; i++) {
g.setColor(Color.BLACK);
g.fillRect(100 + graphInterval * i, 400 - calcArray[i] * 400 / ((_y_end - _y_start) / _y_interval), graphInterval, calcArray[i] * 400 / ((_y_end - _y_start) / _y_interval));
}
g.setColor(Color.BLACK);
g.drawString(_title, 50, 30);
}
public int[] calculateArray() {
int[] result = new int[(int) ((_x_end - _x_start) / _x_interval)];
double currentValue, currentX;
int order;
for (int i = 0; i < _values.length; i++) {
currentValue = _values[i];
currentX = _x_start;
order = 0;
while (currentX < _x_end) {
if(currentValue >= currentX && currentValue < (currentX + _x_interval)) {
result[order] += 1;
break;
}
currentX += _x_interval;
order++;
}
}
return result;
}
}