This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBarChartViewer.java
121 lines (105 loc) · 4.48 KB
/
BarChartViewer.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
import javax.swing.*;
import java.awt.*;
/**
* A frame to display a bar chart representing allocation numbers of all the referees
*/
public class BarChartViewer extends JFrame {
/**
* Creates a frame where the width is scaled to the size of the referee list
* and paints a bar chart
*
* @param refereeList object which contains Referee instances
*/
public BarChartViewer(RefList refereeList) {
//Create the frame
setTitle("Allocation numbers");
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
getContentPane().setBackground(Color.WHITE);
BarChart chart = new BarChart(refereeList);
add(chart);
// calculate a window width based on bar chart width.
int width = refereeList.getRefereeCount() * (chart.BAR_WIDTH + chart.BAR_GAP) + (chart.BAR_MARGIN * 2) - chart.BAR_GAP;
setSize(width, 310);
setLocationRelativeTo(null);
setVisible(true);
}
/**
* The component that draws the bar chart
*/
private class BarChart extends JComponent {
private final int CHART_HEIGHT = 220; // The height of the chart area
private final int BAR_WIDTH = 30; // The width of each bar
private final int BAR_GAP = 5; // The width of the gap between the bars
private final int BAR_MARGIN = 20; // Margin around the bar chart area
private final RefList refereeList; // The list of the referee whose information will be displayed
private int maxValue = 0; // The highest number of referee allocations
/**
* Constructor for the BarChart class
*
* @param refereeList list of referees which contains allocation numbers
*/
public BarChart(RefList refereeList) {
this.refereeList = refereeList;
// set the max allocation value, used to scale the bar chart
for (Referee ref : this.refereeList) {
if (ref.getNumAllocs() > maxValue) {
maxValue = ref.getNumAllocs();
}
}
}
/**
* The paintComponent method draws a bar for each referee allocation
*/
public void paintComponent(Graphics g) {
Graphics2D g2 = (Graphics2D) g;
//scale the chart depending on the highest number of match allocations
int unit = Math.round((float) CHART_HEIGHT / maxValue);
//draw the grey unit lines
drawAxis(g2, unit);
//initial x coordinate to start drawing bars
int barX = BAR_MARGIN;
for (Referee ref : refereeList) {
String id = ref.getRefID();
int allocNum = ref.getNumAllocs();
//calculate bar height relative to the chart area
int barHeight = unit * allocNum;
drawBar(g2, allocNum, barX, CHART_HEIGHT + 30 - barHeight, BAR_WIDTH, barHeight, id);
barX += BAR_WIDTH + BAR_GAP;
}
}
/**
* Draws a bar with the number of allocations and the referee id
*
* @param heading displays number of allocations above the bar
* @param x the x coordinate
* @param y the y coordinate
* @param BAR_WIDTH the width of the bar which is fixed
* @param barHeight the height of the bar which depends on the number of allocations
* @param id Referee ID to displayed below the bar
*/
private void drawBar(Graphics2D g, int heading, int x, int y, int BAR_WIDTH, int barHeight, String id) {
g.setColor(Color.MAGENTA);
g.fillRect(x, y, BAR_WIDTH, barHeight);
g.setColor(Color.BLACK);
g.draw(new Rectangle(x, y, BAR_WIDTH, barHeight));
g.drawString("" + heading, x + 5, y - 5);
g.drawString(id, x, y + barHeight + 15);
}
/**
* Draws gray lines behind the bar chart
*
* @param g graphics component
* @param unit the relative pixel distance between each unit
*/
private void drawAxis(Graphics2D g, int unit) {
int y = CHART_HEIGHT + 30;
g.setColor(Color.LIGHT_GRAY);
for (int i = 0; i <= maxValue; i++) {
int x1 = BAR_MARGIN / 2;
int x2 = refereeList.getRefereeCount() * (BAR_WIDTH + BAR_GAP) + BAR_GAP + BAR_MARGIN;
g.drawLine(x1, y, x2, y);
y -= unit;
}
}
}
}