50 @SuppressWarnings(
"serial")
53 private static final int VERTICAL = 0;
54 private static final int HORIZONTAL = 1;
55 private static final int SOLID = 0;
56 private static final int STRIPED = 1;
63 private Color colors[];
64 private String labels[];
66 private int scale = 10;
67 private int maxLabelWidth = 0;
68 private int barSpacing = 10;
69 private int maxValue = 0;
81 for (
int i = 0; i <
columns; i++) {
90 font =
new java.awt.Font(
"Monospaced", Font.BOLD, 12);
91 metrics = getFontMetrics(
font);
93 title = getParameter(
"title");
98 String temp = getParameter(
"columns");
102 columns = Integer.parseInt(temp);
105 temp = getParameter(
"scale");
109 scale = Integer.parseInt(temp);
112 temp = getParameter(
"orientation");
114 orientation = VERTICAL;
115 }
else if (temp.equalsIgnoreCase(
"horizontal")) {
116 orientation = HORIZONTAL;
118 orientation = VERTICAL;
123 String temp = getParameter(
"C" + (i + 1));
125 values[i] = Integer.parseInt(temp);
126 }
catch (NumberFormatException e) {
128 }
catch (NullPointerException e) {
131 maxValue = Math.max(maxValue, values[i]);
135 String temp = getParameter(
"C" + (i + 1) +
"_label");
141 maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);
145 String temp = getParameter(
"C" + (i + 1) +
"_style");
146 if (temp ==
null || temp.equalsIgnoreCase(
"solid")) {
148 }
else if (temp.equalsIgnoreCase(
"striped")) {
156 String temp = getParameter(
"C" + (i + 1) +
"_color");
158 temp = temp.toLowerCase();
159 if (temp.equals(
"red")) {
160 colors[i] = Color.red;
161 }
else if (temp.equals(
"green")) {
162 colors[i] = Color.green;
163 }
else if (temp.equals(
"blue")) {
164 colors[i] = Color.blue;
165 }
else if (temp.equals(
"pink")) {
166 colors[i] = Color.pink;
167 }
else if (temp.equals(
"orange")) {
168 colors[i] = Color.orange;
169 }
else if (temp.equals(
"magenta")) {
170 colors[i] = Color.magenta;
171 }
else if (temp.equals(
"cyan")) {
172 colors[i] = Color.cyan;
173 }
else if (temp.equals(
"white")) {
174 colors[i] = Color.white;
175 }
else if (temp.equals(
"yellow")) {
176 colors[i] = Color.yellow;
177 }
else if (temp.equals(
"gray")) {
178 colors[i] = Color.gray;
179 }
else if (temp.equals(
"darkgray")) {
180 colors[i] = Color.darkGray;
182 colors[i] = Color.gray;
185 colors[i] = Color.gray;
192 g.setColor(Color.black);
195 g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
197 int titleWidth = metrics.stringWidth(title);
198 int cx = Math.max((getSize().width - titleWidth) / 2, 0);
199 int cy = getSize().height - metrics.getDescent();
200 g.drawString(title, cx, cy);
203 if (orientation == HORIZONTAL) {
213 int barHeight = metrics.getHeight();
215 for (
int i = 0; i <
columns; i++) {
218 int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5
219 + metrics.stringWidth(Integer.toString(maxValue));
220 cx = Math.max((getSize().width - widthOfItems) / 2, 0);
223 cy = getSize().height - metrics.getDescent() - metrics.getHeight()
225 - ((
columns - i - 1) * (barSpacing + barHeight));
228 g.setColor(Color.black);
229 g.drawString(labels[i], cx, cy);
230 cx += maxLabelWidth + 3;
234 g.fillRect(cx + 4, cy - barHeight + 4,
235 (values[i] * scale), barHeight);
238 g.setColor(colors[i]);
239 if (styles[i] == STRIPED) {
240 for (
int k = 0; k <= values[i] * scale; k += 2) {
241 g.drawLine(cx + k, cy - barHeight, cx + k, cy);
244 g.fillRect(cx, cy - barHeight,
245 (values[i] * scale) + 1, barHeight + 1);
247 cx += (values[i] * scale) + 4;
250 g.setColor(g.getColor().darker());
251 g.drawString(Integer.toString(values[i]), cx, cy);
256 int barWidth = maxLabelWidth;
258 for (
int i = 0; i <
columns; i++) {
261 int widthOfItems = (barWidth + barSpacing) *
columns - barSpacing;
262 int cx = Math.max((getSize().width - widthOfItems) / 2, 0);
263 cx += (maxLabelWidth + barSpacing) * i;
266 int cy = getSize().height - metrics.getHeight()
267 - metrics.getDescent() - 4;
270 g.setColor(Color.black);
271 g.drawString(labels[i], cx, cy);
272 cy -= metrics.getHeight() - 3;
275 g.fillRect(cx + 4, cy - (values[i] * scale) - 4,
276 barWidth, (values[i] * scale));
279 g.setColor(colors[i]);
280 if (styles[i] == STRIPED) {
281 for (
int k = 0; k <= values[i] * scale; k += 2) {
282 g.drawLine(cx, cy - k,
283 cx + barWidth, cy - k);
286 g.fillRect(cx, cy - (values[i] * scale),
287 barWidth + 1, (values[i] * scale) + 1);
289 cy -= (values[i] * scale) + 5;
292 g.setColor(g.getColor().darker());
293 g.drawString(Integer.toString(values[i]), cx, cy);
299 return "Title: Bar Chart \n"
300 +
"Author: Sami Shaio \n"
301 +
"A simple bar chart demo.";
307 {
"title",
"string",
"The title of bar graph. Default is 'Chart'" },
308 {
"scale",
"int",
"The scale of the bar graph. Default is 10." },
309 {
"columns",
"int",
"The number of columns/rows. Default is 5." },
310 {
"orientation",
"{VERTICAL, HORIZONTAL}",
311 "The orienation of the bar graph. Default is VERTICAL." },
312 {
"c#",
"int",
"Subsitute a number for #. "
313 +
"The value/size of bar #. Default is 0." },
314 {
"c#_label",
"string",
"The label for bar #. "
315 +
"Default is an empty label." },
316 {
"c#_style",
"{SOLID, STRIPED}",
"The style of bar #. "
317 +
"Default is SOLID." },
318 {
"c#_color",
"{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
319 +
"WHITE, YELLOW, GRAY, DARKGRAY}",
320 "The color of bar #. Default is GRAY." }