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 package com.buckosoft.fibs.BuckoFIBS.gui;
58
59 import java.awt.Color;
60 import java.awt.Dimension;
61 import java.awt.Font;
62 import java.awt.FontMetrics;
63 import java.awt.Graphics;
64
65 import javax.swing.JPanel;
66
67 import com.buckosoft.fibs.domain.config.RatingGraphConfig;
68
69
70
71
72
73
74
75 public class RatingGraphPanel extends JPanel {
76 private final static boolean DEBUG = false;
77 private static final long serialVersionUID = 1L;
78 private Font font = new Font("Arial", Font.PLAIN, 10);
79 private Color lineColor = new Color(213,215,5);
80
81 protected class Specs {
82 int w;
83 int h;
84 }
85 private Specs bs = new Specs();
86
87 private double data[] = new double[] { 1500.0, 1503.0, 1505.0 };
88 private int xpoints[] = new int[data.length];
89 private int ypoints[] = new int[data.length];
90
91 private RatingGraphConfig config = new RatingGraphConfig();
92
93
94
95 public RatingGraphPanel() {
96 super();
97 initialize();
98 }
99
100
101
102
103
104
105 public void setData(double[] data) {
106 this.data = data;
107 xpoints = new int[data.length];
108 ypoints = new int[data.length];
109 this.repaint();
110 }
111
112
113
114
115 public void redraw() {
116 this.repaint();
117 }
118
119 public void setConfig(RatingGraphConfig ratingGraphConfig) {
120 this.config = ratingGraphConfig;
121 }
122
123
124
125
126
127 private void initialize() {
128 this.setSize(new Dimension(216, 61));
129 }
130
131
132
133
134 public void paint(Graphics g) {
135 super.paint(g);
136
137 int i,j;
138 int x,y;
139 double high, low, range;
140 double d;
141 bs.w = this.getWidth();
142 bs.h = this.getHeight();
143 g.setColor(Color.black);
144 g.fillRect(0, 0, bs.w, bs.h);
145
146 if (data == null || data.length == 0)
147 return;
148
149
150 int first = 0;
151 int last = data.length;
152 if (config.getType() == RatingGraphConfig.Type.displayLastXMatches) {
153 if (last > config.getMatchCount())
154 first = last - config.getMatchCount();
155 }
156
157
158 int pps = bs.w / (last-first);
159 if (DEBUG)
160 System.out.println("RatingGraph: w=" + bs.w + " numPoints=" + (last-first) + " pps=" + pps);
161 g.setColor(lineColor);
162 if (last == 1) {
163 g.drawLine(0, bs.h/2, bs.w, bs.h/2);
164 low = -100000;
165 high = 20000;
166 range = high - low;
167 } else {
168 xpoints = new int[last-first];
169 ypoints = new int[last-first];
170 low = data[first];
171 high = low;
172 for (i=first; i<last; i++) {
173 if (low > data[i])
174 low = data[i];
175 if (high < data[i])
176 high = data[i];
177 }
178 range = high - low;
179 for (i=first, j=0; i<last; i++, j++) {
180
181 if (i == 0)
182 xpoints[j] = 0;
183 else if (last-first-1 == 0)
184 xpoints[j] = bs.w;
185 else
186 xpoints[j] = j * bs.w / (last-first-1);
187 ypoints[j] = (int)(bs.h-((data[i]-low)*bs.h / range));
188 }
189 g.drawPolyline(xpoints, ypoints, xpoints.length);
190
191 g.setColor(Color.white);
192 g.setFont(font);
193 FontMetrics fm = g.getFontMetrics();
194 String s = "" + low;
195
196 x = 0;
197 y = bs.h-1;
198 g.drawString(s, x, y);
199 s = "" + high;
200 y = 0 + fm.getAscent()-1;
201 g.drawString(s, x, y);
202 }
203 g.setColor(Color.white);
204 g.setFont(font);
205 FontMetrics fm = g.getFontMetrics();
206 d = data[last-1];
207 String s = "" + d;
208 int sw = fm.stringWidth(s);
209 x = bs.w - sw;
210 double mid = low + range/2;
211 if (d > mid)
212 y = bs.h*3/4 + fm.getAscent()/2;
213 else
214 y = bs.h*1/4 + fm.getAscent()/2;
215 g.drawString(s, x, y);
216
217 if (data.length > 1) {
218 for (i=first, j=0; i<last; i++, j++) {
219 Color c = Color.green;
220 if (i>0 && data[i] < data[i-1])
221 c = Color.red;
222 g.setColor(c);
223 if (pps > 7)
224 g.drawRect(xpoints[j]-1, ypoints[j]-1, 3, 3);
225 else if (pps > 3)
226 g.drawRect(xpoints[j]-1, ypoints[j]-1, 2, 2);
227 else
228 g.drawRect(xpoints[j]-1, ypoints[j]-1, 1, 1);
229
230 }
231 }
232 }
233
234 }