-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLineGraph.java
58 lines (42 loc) · 2 KB
/
LineGraph.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
package gritnessApp;
import java.awt.*;
import javax.swing.*;
public class LineGraph extends Graph{
public LineGraph(int[] data, String xAxisLabel, String yAxisLabel, String title, int x, int y, int maxValue) {
super(data, xAxisLabel, yAxisLabel, title, x, y, maxValue);
}
public void draw(Graphics g) {
// Draw x and y axis
//X-Axis line
g.setColor(Color.black);
g.drawLine(x, y, x + AXIS_LENGTH, y);
//Y-Axis line
g.drawLine(x, y , x, y - AXIS_LENGTH);
// Draw labels
g.drawString(xAxisLabel,(2 * x + AXIS_LENGTH) / 2, y + LABEL_DISTANCE_BETWEEN_AXIS);
g.drawString(yAxisLabel, x - LABEL_DISTANCE_BETWEEN_AXIS - 50, (2 * y - AXIS_LENGTH) / 2);
g.setFont(new Font("dialog", Font.PLAIN, 25));
g.drawString(title, (2 * x + AXIS_LENGTH) / 2 - 35, y - AXIS_LENGTH );
g.setFont(new Font("dialog", Font.PLAIN, 12));
//Y-Axis Scale
for (int i = 0; i <= MAX_VALUE; i++) {
if(i % (MAX_VALUE / 10) == 0) {
//Numbers
g.drawString(Integer.toString(i), x - 35, y - (int) (i * AXIS_LENGTH / MAX_VALUE) + 5);
//Lines
g.drawLine(x - 5, y - (int) (i * AXIS_LENGTH / MAX_VALUE), x, y - (int) (i * AXIS_LENGTH / MAX_VALUE));
}
}
// Draw data points
for (int i = 0; i < data.length - 1; i++) {
g.setColor(Color.red);
double horizontalSpacing = ((double)AXIS_LENGTH / NUMBER_DAYS);
double verticalSpacing = ((double)MAX_VALUE) / AXIS_LENGTH;
g.drawLine((int)(x + i * horizontalSpacing), (int)(y - data[i] / verticalSpacing), (int)(x + (i + 1) * horizontalSpacing), (int)(y - data[i + 1] / verticalSpacing));
}
for(int i = 0; i < 7; i++) {
g.setColor(Color.black);
g.drawString(DAYS_OF_WEEK[i], x + i * AXIS_LENGTH / 7, y + 15);
}
}
}