|
1
|
|
|
|
2
|
|
|
|
3
|
|
|
|
4
|
|
|
|
5
|
|
|
|
6
|
|
|
|
7
|
|
package org.ejtools.graph.renderer;
|
|
8
|
|
|
|
9
|
|
import java.awt.Color;
|
|
10
|
|
import java.awt.FontMetrics;
|
|
11
|
|
import java.awt.Graphics;
|
|
12
|
|
import java.awt.Insets;
|
|
13
|
|
import java.awt.geom.Rectangle2D;
|
|
14
|
|
import java.text.Format;
|
|
15
|
|
|
|
16
|
|
import org.ejtools.graph.Axis;
|
|
17
|
|
import org.ejtools.graph.GraphRenderer;
|
|
18
|
|
|
|
19
|
|
|
|
20
|
|
|
|
21
|
|
|
|
22
|
|
|
|
23
|
|
|
|
24
|
|
public class GridGraphRenderer extends Axis
|
|
25
|
|
{
|
|
26
|
|
|
|
27
|
|
protected Format horizontalFormat = null;
|
|
28
|
|
|
|
29
|
|
protected Format verticalFormat = null;
|
|
30
|
|
|
|
31
|
|
|
|
32
|
|
|
|
33
|
|
|
|
34
|
|
|
|
35
|
|
|
|
36
|
|
|
|
37
|
|
|
|
38
|
0
|
public GridGraphRenderer(Format horizontalFormat, Format verticalFormat)
|
|
39
|
|
{
|
|
40
|
0
|
this.format = horizontalFormat;
|
|
41
|
0
|
this.horizontalFormat = horizontalFormat;
|
|
42
|
0
|
this.verticalFormat = verticalFormat;
|
|
43
|
|
}
|
|
44
|
|
|
|
45
|
|
|
|
46
|
|
|
|
47
|
|
|
|
48
|
|
|
|
49
|
0
|
protected void paintComponent(Graphics graphics)
|
|
50
|
|
{
|
|
51
|
0
|
double multiplier = 0.0d;
|
|
52
|
|
|
|
53
|
0
|
graphics.setFont(FONT);
|
|
54
|
0
|
FontMetrics metrics = graphics.getFontMetrics();
|
|
55
|
0
|
graphics.setColor(Color.lightGray);
|
|
56
|
|
|
|
57
|
0
|
double minX = element.getXRange().getMin();
|
|
58
|
0
|
double maxX = element.getXRange().getMax();
|
|
59
|
0
|
double minY = element.getYRange().getMin();
|
|
60
|
0
|
double maxY = element.getYRange().getMax();
|
|
61
|
|
|
|
62
|
0
|
Insets insets = this.getInsets();
|
|
63
|
0
|
int x = insets.left;
|
|
64
|
0
|
int y = insets.top;
|
|
65
|
0
|
double width = (double) this.getWidth() - 1 - insets.left - insets.right;
|
|
66
|
0
|
double height = (double) this.getHeight() - 1 - insets.top - insets.bottom;
|
|
67
|
|
|
|
68
|
0
|
double scaleX = width / (maxX - minX);
|
|
69
|
0
|
double scaleY = -height / (maxY - minY);
|
|
70
|
0
|
double offsetX = x - scaleX * minX;
|
|
71
|
0
|
double offsetY = y - scaleY * maxY;
|
|
72
|
|
|
|
73
|
0
|
if (this.horizontalScaling == GraphRenderer.ALIGN_LEFT)
|
|
74
|
|
{
|
|
75
|
0
|
scaleX = 1.0d;
|
|
76
|
0
|
offsetX = -minX;
|
|
77
|
|
}
|
|
78
|
0
|
if (this.horizontalScaling == GraphRenderer.ALIGN_RIGHT)
|
|
79
|
|
{
|
|
80
|
0
|
scaleX = 1.0d * this.horizontalScale;
|
|
81
|
0
|
offsetX = width - maxX * this.horizontalScale;
|
|
82
|
|
}
|
|
83
|
|
|
|
84
|
|
|
|
85
|
0
|
minX = (x - offsetX) / scaleX;
|
|
86
|
0
|
this.format = this.horizontalFormat;
|
|
87
|
|
|
|
88
|
0
|
String minText = this.getFormated(minX);
|
|
89
|
0
|
Rectangle2D minBounds = metrics.getStringBounds(minText, graphics);
|
|
90
|
0
|
String maxText = this.getFormated(maxX);
|
|
91
|
0
|
Rectangle2D maxBounds = metrics.getStringBounds(maxText, graphics);
|
|
92
|
|
|
|
93
|
|
|
|
94
|
0
|
double bound = Math.max(minBounds.getWidth() * 4.0d, maxBounds.getWidth() * 4.0d);
|
|
95
|
|
|
|
96
|
|
|
|
97
|
0
|
int ticks = 1;
|
|
98
|
0
|
while ((width / ticks) > bound)
|
|
99
|
|
{
|
|
100
|
0
|
ticks = ticks * 2;
|
|
101
|
|
}
|
|
102
|
|
|
|
103
|
|
|
|
104
|
0
|
multiplier = width / ticks / 2;
|
|
105
|
0
|
for (int i = 0; i <= (2 * ticks); i++)
|
|
106
|
|
{
|
|
107
|
0
|
graphics.drawLine((int) (i * multiplier), 0, (int) (i * multiplier), (int) height);
|
|
108
|
|
}
|
|
109
|
|
|
|
110
|
|
|
|
111
|
0
|
this.format = this.verticalFormat;
|
|
112
|
|
|
|
113
|
0
|
minText = this.getFormated(minY);
|
|
114
|
0
|
minBounds = metrics.getStringBounds(minText, graphics);
|
|
115
|
0
|
maxText = this.getFormated(maxY);
|
|
116
|
0
|
maxBounds = metrics.getStringBounds(maxText, graphics);
|
|
117
|
|
|
|
118
|
|
|
|
119
|
0
|
bound = Math.max(minBounds.getHeight() * 4.0d, maxBounds.getHeight() * 4.0d);
|
|
120
|
|
|
|
121
|
|
|
|
122
|
0
|
ticks = 1;
|
|
123
|
0
|
while ((height / ticks) > bound)
|
|
124
|
|
{
|
|
125
|
0
|
ticks = ticks * 2;
|
|
126
|
|
}
|
|
127
|
|
|
|
128
|
|
|
|
129
|
0
|
multiplier = height / ticks / 2;
|
|
130
|
0
|
for (int i = 0; i <= (2 * ticks); i++)
|
|
131
|
|
{
|
|
132
|
0
|
graphics.drawLine(0, (int) (i * multiplier), (int) width, (int) (i * multiplier));
|
|
133
|
|
}
|
|
134
|
|
|
|
135
|
0
|
this.element.draw(graphics, scaleX, offsetX, scaleY, offsetY);
|
|
136
|
|
}
|
|
137
|
|
}
|
|
138
|
|
|