2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
*
|
|
4 |
* Redistribution and use in source and binary forms, with or without
|
|
5 |
* modification, are permitted provided that the following conditions
|
|
6 |
* are met:
|
|
7 |
*
|
|
8 |
* - Redistributions of source code must retain the above copyright
|
|
9 |
* notice, this list of conditions and the following disclaimer.
|
|
10 |
*
|
|
11 |
* - Redistributions in binary form must reproduce the above copyright
|
|
12 |
* notice, this list of conditions and the following disclaimer in the
|
|
13 |
* documentation and/or other materials provided with the distribution.
|
|
14 |
*
|
5506
|
15 |
* - Neither the name of Oracle nor the names of its
|
2
|
16 |
* contributors may be used to endorse or promote products derived
|
|
17 |
* from this software without specific prior written permission.
|
|
18 |
*
|
|
19 |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
|
|
20 |
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
21 |
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
|
22 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
|
|
23 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
|
24 |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
|
|
25 |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
|
|
26 |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
|
|
27 |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
28 |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
29 |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
30 |
*/
|
|
31 |
|
|
32 |
/*
|
|
33 |
*/
|
|
34 |
|
|
35 |
import java.awt.*;
|
|
36 |
|
|
37 |
/**
|
|
38 |
* A simple bar chart demo
|
|
39 |
* @author Sami Shaio
|
|
40 |
* @modified 06/21/00 Daniel Peek : refactored, comments
|
|
41 |
*/
|
|
42 |
public class BarChart extends java.applet.Applet {
|
|
43 |
private static final int VERTICAL = 0;
|
|
44 |
private static final int HORIZONTAL = 1;
|
|
45 |
|
|
46 |
private static final int SOLID = 0;
|
|
47 |
private static final int STRIPED = 1;
|
|
48 |
|
|
49 |
private int orientation;
|
|
50 |
private String title;
|
|
51 |
private Font font;
|
|
52 |
private FontMetrics metrics;
|
|
53 |
private int fontHeight = 15;
|
|
54 |
private int columns;
|
|
55 |
private int values[];
|
|
56 |
private Color colors[];
|
|
57 |
private String labels[];
|
|
58 |
private int styles[];
|
|
59 |
private int scale = 10;
|
|
60 |
private int maxLabelWidth = 0;
|
|
61 |
private int barSpacing = 10;
|
|
62 |
private int maxValue = 0;
|
|
63 |
|
|
64 |
public void init() {
|
|
65 |
|
|
66 |
getSettings();
|
|
67 |
|
|
68 |
values = new int[columns];
|
|
69 |
labels = new String[columns];
|
|
70 |
styles = new int[columns];
|
|
71 |
colors = new Color[columns];
|
|
72 |
|
|
73 |
for (int i=0; i < columns; i++) {
|
|
74 |
parseValue(i);
|
|
75 |
parseLabel(i);
|
|
76 |
parseStyle(i);
|
|
77 |
parseColor(i);
|
|
78 |
}
|
|
79 |
}
|
|
80 |
|
|
81 |
private void getSettings() {
|
|
82 |
font = new java.awt.Font("Monospaced", Font.BOLD, 12);
|
|
83 |
metrics = getFontMetrics(font);
|
|
84 |
|
|
85 |
title = getParameter("title");
|
|
86 |
if (title == null) {
|
|
87 |
title = "Chart";
|
|
88 |
}
|
|
89 |
|
|
90 |
String temp = getParameter("columns");
|
|
91 |
if (temp == null) {
|
|
92 |
columns = 5;
|
|
93 |
} else {
|
|
94 |
columns = Integer.parseInt(temp);
|
|
95 |
}
|
|
96 |
|
|
97 |
temp = getParameter("scale");
|
|
98 |
if (temp == null) {
|
|
99 |
scale = 10;
|
|
100 |
} else {
|
|
101 |
scale = Integer.parseInt(temp);
|
|
102 |
}
|
|
103 |
|
|
104 |
temp = getParameter("orientation");
|
|
105 |
if (temp == null) {
|
|
106 |
orientation = VERTICAL;
|
|
107 |
} else if (temp.equalsIgnoreCase("horizontal")) {
|
|
108 |
orientation = HORIZONTAL;
|
|
109 |
} else {
|
|
110 |
orientation = VERTICAL;
|
|
111 |
}
|
|
112 |
}
|
|
113 |
|
|
114 |
private void parseValue(int i) {
|
|
115 |
String temp = getParameter("C" + (i+1));
|
|
116 |
try {
|
|
117 |
values[i] = Integer.parseInt(temp);
|
|
118 |
} catch (NumberFormatException e) {
|
|
119 |
values[i] = 0;
|
|
120 |
} catch (NullPointerException e) {
|
|
121 |
values[i] = 0;
|
|
122 |
}
|
|
123 |
maxValue = Math.max(maxValue, values[i]);
|
|
124 |
}
|
|
125 |
|
|
126 |
private void parseLabel(int i) {
|
|
127 |
String temp = getParameter("C" + (i+1) + "_label");
|
|
128 |
if (temp==null) {
|
|
129 |
labels[i] = "";
|
|
130 |
} else {
|
|
131 |
labels[i] = temp;
|
|
132 |
}
|
|
133 |
maxLabelWidth = Math.max(metrics.stringWidth
|
|
134 |
((String) (labels[i])), maxLabelWidth);
|
|
135 |
}
|
|
136 |
|
|
137 |
private void parseStyle(int i) {
|
|
138 |
String temp = getParameter("C" + (i+1) + "_style");
|
|
139 |
if (temp == null || temp.equalsIgnoreCase("solid")) {
|
|
140 |
styles[i] = SOLID;
|
|
141 |
} else if (temp.equalsIgnoreCase("striped")) {
|
|
142 |
styles[i] = STRIPED;
|
|
143 |
} else {
|
|
144 |
styles[i] = SOLID;
|
|
145 |
}
|
|
146 |
}
|
|
147 |
|
|
148 |
private void parseColor(int i) {
|
|
149 |
String temp = getParameter("C" + (i+1) + "_color");
|
|
150 |
if (temp != null) {
|
|
151 |
temp = temp.toLowerCase();
|
|
152 |
if (temp.equals("red")) {
|
|
153 |
colors[i] = Color.red;
|
|
154 |
} else if (temp.equals("green")) {
|
|
155 |
colors[i] = Color.green;
|
|
156 |
} else if (temp.equals("blue")) {
|
|
157 |
colors[i] = Color.blue;
|
|
158 |
} else if (temp.equals("pink")) {
|
|
159 |
colors[i] = Color.pink;
|
|
160 |
} else if (temp.equals("orange")) {
|
|
161 |
colors[i] = Color.orange;
|
|
162 |
} else if (temp.equals("magenta")) {
|
|
163 |
colors[i] = Color.magenta;
|
|
164 |
} else if (temp.equals("cyan")) {
|
|
165 |
colors[i] = Color.cyan;
|
|
166 |
} else if (temp.equals("white")) {
|
|
167 |
colors[i] = Color.white;
|
|
168 |
} else if (temp.equals("yellow")) {
|
|
169 |
colors[i] = Color.yellow;
|
|
170 |
} else if (temp.equals("gray")) {
|
|
171 |
colors[i] = Color.gray;
|
|
172 |
} else if (temp.equals("darkgray")) {
|
|
173 |
colors[i] = Color.darkGray;
|
|
174 |
} else {
|
|
175 |
colors[i] = Color.gray;
|
|
176 |
}
|
|
177 |
} else {
|
|
178 |
colors[i] = Color.gray;
|
|
179 |
}
|
|
180 |
}
|
|
181 |
|
|
182 |
public void paint(Graphics g) {
|
|
183 |
// draw the title centered at the bottom of the bar graph
|
|
184 |
g.setColor(Color.black);
|
|
185 |
g.setFont(font);
|
|
186 |
|
|
187 |
g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
|
|
188 |
|
|
189 |
int titleWidth = metrics.stringWidth(title);
|
|
190 |
int cx = Math.max((getSize().width - titleWidth) / 2, 0);
|
|
191 |
int cy = getSize().height - metrics.getDescent();
|
|
192 |
g.drawString(title, cx, cy);
|
|
193 |
|
|
194 |
// draw the bars and their titles
|
|
195 |
if(orientation == HORIZONTAL) {
|
|
196 |
paintHorizontal(g);
|
|
197 |
} else { // VERTICAL
|
|
198 |
paintVertical(g);
|
|
199 |
}
|
|
200 |
}
|
|
201 |
|
|
202 |
private void paintHorizontal(Graphics g) {
|
|
203 |
// x and y coordinates to draw/write to
|
|
204 |
int cx, cy;
|
|
205 |
int barHeight = metrics.getHeight();
|
|
206 |
|
|
207 |
for (int i = 0; i < columns; i++) {
|
|
208 |
|
|
209 |
// set the X coordinate for this bar and label and center it
|
|
210 |
int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5
|
|
211 |
+ metrics.stringWidth(Integer.toString(maxValue));
|
|
212 |
cx = Math.max((getSize().width - widthOfItems) / 2, 0);
|
|
213 |
|
|
214 |
// set the Y coordinate for this bar and label
|
|
215 |
cy = getSize().height - metrics.getDescent() - metrics.getHeight()
|
|
216 |
- barSpacing - ((columns - i - 1) * (barSpacing + barHeight));
|
|
217 |
|
|
218 |
// draw the label
|
|
219 |
g.setColor(Color.black);
|
|
220 |
g.drawString(labels[i], cx, cy);
|
|
221 |
cx += maxLabelWidth + 3;
|
|
222 |
|
|
223 |
|
|
224 |
// draw the shadow
|
|
225 |
g.fillRect(cx + 4, cy - barHeight + 4,
|
|
226 |
(values[i] * scale), barHeight);
|
|
227 |
|
|
228 |
// draw the bar
|
|
229 |
g.setColor(colors[i]);
|
|
230 |
if (styles[i] == STRIPED) {
|
|
231 |
for (int k = 0; k <= values[i] * scale; k += 2) {
|
|
232 |
g.drawLine(cx + k, cy - barHeight, cx + k, cy);
|
|
233 |
}
|
|
234 |
} else { // SOLID
|
|
235 |
g.fillRect(cx, cy - barHeight,
|
|
236 |
(values[i] * scale) + 1, barHeight + 1);
|
|
237 |
}
|
|
238 |
cx += (values[i] * scale) + 4;
|
|
239 |
|
|
240 |
// draw the value at the end of the bar
|
|
241 |
g.setColor(g.getColor().darker());
|
|
242 |
g.drawString(Integer.toString(values[i]), cx, cy);
|
|
243 |
}
|
|
244 |
}
|
|
245 |
|
|
246 |
private void paintVertical(Graphics g) {
|
|
247 |
int barWidth = maxLabelWidth;
|
|
248 |
|
|
249 |
for (int i = 0; i < columns; i++) {
|
|
250 |
|
|
251 |
// X coordinate for this label and bar (centered)
|
|
252 |
int widthOfItems = (barWidth + barSpacing) * columns - barSpacing;
|
|
253 |
int cx = Math.max((getSize().width - widthOfItems) / 2, 0);
|
|
254 |
cx += (maxLabelWidth + barSpacing) * i;
|
|
255 |
|
|
256 |
// Y coordinate for this label and bar
|
|
257 |
int cy = getSize().height - metrics.getHeight()
|
|
258 |
- metrics.getDescent() - 4;
|
|
259 |
|
|
260 |
// draw the label
|
|
261 |
g.setColor(Color.black);
|
|
262 |
g.drawString(labels[i], cx, cy);
|
|
263 |
cy -= metrics.getHeight() - 3;
|
|
264 |
|
|
265 |
// draw the shadow
|
|
266 |
g.fillRect(cx + 4, cy - (values[i] * scale) - 4,
|
|
267 |
barWidth, (values[i] * scale));
|
|
268 |
|
|
269 |
// draw the bar
|
|
270 |
g.setColor(colors[i]);
|
|
271 |
if (styles[i] == STRIPED) {
|
|
272 |
for (int k=0; k <= values[i] * scale; k+=2) {
|
|
273 |
g.drawLine(cx, cy - k,
|
|
274 |
cx + barWidth, cy - k);
|
|
275 |
}
|
|
276 |
} else {
|
|
277 |
g.fillRect(cx, cy - (values[i] * scale),
|
|
278 |
barWidth + 1, (values[i] * scale) + 1);
|
|
279 |
}
|
|
280 |
cy -= (values[i] * scale) + 5;
|
|
281 |
|
|
282 |
// draw the value on top of the bar
|
|
283 |
g.setColor(g.getColor().darker());
|
|
284 |
g.drawString(Integer.toString(values[i]), cx, cy);
|
|
285 |
}
|
|
286 |
}
|
|
287 |
|
|
288 |
public String getAppletInfo() {
|
|
289 |
return "Title: Bar Chart \n"
|
|
290 |
+ "Author: Sami Shaio \n"
|
|
291 |
+ "A simple bar chart demo.";
|
|
292 |
}
|
|
293 |
|
|
294 |
public String[][] getParameterInfo() {
|
|
295 |
String[][] info = {
|
|
296 |
{"title", "string", "The title of bar graph. Default is 'Chart'"},
|
|
297 |
{"scale", "int", "The scale of the bar graph. Default is 10."},
|
|
298 |
{"columns", "int", "The number of columns/rows. Default is 5."},
|
|
299 |
{"orientation", "{VERTICAL, HORIZONTAL}",
|
|
300 |
"The orienation of the bar graph. Default is VERTICAL."},
|
|
301 |
{"c#", "int", "Subsitute a number for #. "
|
|
302 |
+ "The value/size of bar #. Default is 0."},
|
|
303 |
{"c#_label", "string", "The label for bar #. "
|
|
304 |
+ "Default is an empty label."},
|
|
305 |
{"c#_style", "{SOLID, STRIPED}", "The style of bar #. "
|
|
306 |
+ "Default is SOLID."},
|
|
307 |
{"c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
|
|
308 |
+ "WHITE, YELLOW, GRAY, DARKGRAY}",
|
|
309 |
"The color of bar #. Default is GRAY."}
|
|
310 |
};
|
|
311 |
return info;
|
|
312 |
}
|
|
313 |
}
|