BarChart.java revision 3846
0N/A/*
2362N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A *
0N/A * Redistribution and use in source and binary forms, with or without
0N/A * modification, are permitted provided that the following conditions
0N/A * are met:
0N/A *
0N/A * - Redistributions of source code must retain the above copyright
0N/A * notice, this list of conditions and the following disclaimer.
0N/A *
0N/A * - Redistributions in binary form must reproduce the above copyright
0N/A * notice, this list of conditions and the following disclaimer in the
0N/A * documentation and/or other materials provided with the distribution.
0N/A *
2362N/A * - Neither the name of Oracle nor the names of its
0N/A * contributors may be used to endorse or promote products derived
0N/A * from this software without specific prior written permission.
0N/A *
0N/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
0N/A * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
0N/A * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
0N/A * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
0N/A * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0N/A * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0N/A * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0N/A * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0N/A * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0N/A * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0N/A * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0N/A */
0N/A
0N/A
0N/Aimport java.awt.*;
0N/A
0N/A
0N/A/**
0N/A * A simple bar chart demo
0N/A * @author Sami Shaio
0N/A * @modified 06/21/00 Daniel Peek : refactored, comments
0N/A */
0N/A@SuppressWarnings("serial")
0N/Apublic class BarChart extends java.applet.Applet {
0N/A
0N/A private static final int VERTICAL = 0;
0N/A private static final int HORIZONTAL = 1;
0N/A private static final int SOLID = 0;
0N/A private static final int STRIPED = 1;
0N/A private int orientation;
0N/A private String title;
0N/A private Font font;
0N/A private FontMetrics metrics;
0N/A private int columns;
0N/A private int values[];
0N/A private Color colors[];
0N/A private String labels[];
0N/A private int styles[];
0N/A private int scale = 10;
0N/A private int maxLabelWidth = 0;
0N/A private int barSpacing = 10;
0N/A private int maxValue = 0;
0N/A
0N/A @Override
0N/A public void init() {
0N/A
0N/A getSettings();
0N/A
0N/A values = new int[columns];
0N/A labels = new String[columns];
0N/A styles = new int[columns];
0N/A colors = new Color[columns];
0N/A
0N/A for (int i = 0; i < columns; i++) {
0N/A parseValue(i);
0N/A parseLabel(i);
0N/A parseStyle(i);
0N/A parseColor(i);
0N/A }
0N/A }
0N/A
0N/A private void getSettings() {
0N/A font = new java.awt.Font("Monospaced", Font.BOLD, 12);
0N/A metrics = getFontMetrics(font);
0N/A
0N/A title = getParameter("title");
0N/A if (title == null) {
0N/A title = "Chart";
0N/A }
0N/A
0N/A String temp = getParameter("columns");
0N/A if (temp == null) {
0N/A columns = 5;
0N/A } else {
0N/A columns = Integer.parseInt(temp);
0N/A }
0N/A
0N/A temp = getParameter("scale");
0N/A if (temp == null) {
0N/A scale = 10;
0N/A } else {
0N/A scale = Integer.parseInt(temp);
0N/A }
0N/A
0N/A temp = getParameter("orientation");
0N/A if (temp == null) {
0N/A orientation = VERTICAL;
0N/A } else if (temp.equalsIgnoreCase("horizontal")) {
0N/A orientation = HORIZONTAL;
0N/A } else {
0N/A orientation = VERTICAL;
0N/A }
0N/A }
0N/A
0N/A private void parseValue(int i) {
0N/A String temp = getParameter("C" + (i + 1));
0N/A try {
0N/A values[i] = Integer.parseInt(temp);
0N/A } catch (NumberFormatException e) {
0N/A values[i] = 0;
0N/A } catch (NullPointerException e) {
0N/A values[i] = 0;
0N/A }
0N/A maxValue = Math.max(maxValue, values[i]);
0N/A }
0N/A
0N/A private void parseLabel(int i) {
0N/A String temp = getParameter("C" + (i + 1) + "_label");
0N/A if (temp == null) {
0N/A labels[i] = "";
0N/A } else {
0N/A labels[i] = temp;
0N/A }
0N/A maxLabelWidth = Math.max(metrics.stringWidth(labels[i]), maxLabelWidth);
0N/A }
0N/A
0N/A private void parseStyle(int i) {
0N/A String temp = getParameter("C" + (i + 1) + "_style");
0N/A if (temp == null || temp.equalsIgnoreCase("solid")) {
0N/A styles[i] = SOLID;
0N/A } else if (temp.equalsIgnoreCase("striped")) {
0N/A styles[i] = STRIPED;
0N/A } else {
0N/A styles[i] = SOLID;
0N/A }
0N/A }
0N/A
0N/A private void parseColor(int i) {
0N/A String temp = getParameter("C" + (i + 1) + "_color");
0N/A if (temp != null) {
0N/A temp = temp.toLowerCase();
0N/A if (temp.equals("red")) {
0N/A colors[i] = Color.red;
0N/A } else if (temp.equals("green")) {
0N/A colors[i] = Color.green;
0N/A } else if (temp.equals("blue")) {
0N/A colors[i] = Color.blue;
0N/A } else if (temp.equals("pink")) {
0N/A colors[i] = Color.pink;
0N/A } else if (temp.equals("orange")) {
0N/A colors[i] = Color.orange;
0N/A } else if (temp.equals("magenta")) {
0N/A colors[i] = Color.magenta;
0N/A } else if (temp.equals("cyan")) {
0N/A colors[i] = Color.cyan;
0N/A } else if (temp.equals("white")) {
0N/A colors[i] = Color.white;
0N/A } else if (temp.equals("yellow")) {
0N/A colors[i] = Color.yellow;
0N/A } else if (temp.equals("gray")) {
0N/A colors[i] = Color.gray;
0N/A } else if (temp.equals("darkgray")) {
0N/A colors[i] = Color.darkGray;
0N/A } else {
0N/A colors[i] = Color.gray;
0N/A }
0N/A } else {
0N/A colors[i] = Color.gray;
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public void paint(Graphics g) {
0N/A // draw the title centered at the bottom of the bar graph
0N/A g.setColor(Color.black);
0N/A g.setFont(font);
0N/A
0N/A g.drawRect(0, 0, getSize().width - 1, getSize().height - 1);
0N/A
0N/A int titleWidth = metrics.stringWidth(title);
0N/A int cx = Math.max((getSize().width - titleWidth) / 2, 0);
0N/A int cy = getSize().height - metrics.getDescent();
0N/A g.drawString(title, cx, cy);
0N/A
0N/A // draw the bars and their titles
0N/A if (orientation == HORIZONTAL) {
0N/A paintHorizontal(g);
0N/A } else { // VERTICAL
0N/A paintVertical(g);
0N/A }
0N/A }
0N/A
0N/A private void paintHorizontal(Graphics g) {
0N/A // x and y coordinates to draw/write to
0N/A int cx, cy;
0N/A int barHeight = metrics.getHeight();
0N/A
0N/A for (int i = 0; i < columns; i++) {
0N/A
0N/A // set the X coordinate for this bar and label and center it
0N/A int widthOfItems = maxLabelWidth + 3 + (maxValue * scale) + 5
0N/A + metrics.stringWidth(Integer.toString(maxValue));
0N/A cx = Math.max((getSize().width - widthOfItems) / 2, 0);
0N/A
0N/A // set the Y coordinate for this bar and label
0N/A cy = getSize().height - metrics.getDescent() - metrics.getHeight()
0N/A - barSpacing
0N/A - ((columns - i - 1) * (barSpacing + barHeight));
0N/A
0N/A // draw the label
0N/A g.setColor(Color.black);
0N/A g.drawString(labels[i], cx, cy);
0N/A cx += maxLabelWidth + 3;
0N/A
0N/A
0N/A // draw the shadow
0N/A g.fillRect(cx + 4, cy - barHeight + 4,
0N/A (values[i] * scale), barHeight);
0N/A
0N/A // draw the bar
0N/A g.setColor(colors[i]);
0N/A if (styles[i] == STRIPED) {
0N/A for (int k = 0; k <= values[i] * scale; k += 2) {
0N/A g.drawLine(cx + k, cy - barHeight, cx + k, cy);
0N/A }
0N/A } else { // SOLID
0N/A g.fillRect(cx, cy - barHeight,
0N/A (values[i] * scale) + 1, barHeight + 1);
0N/A }
0N/A cx += (values[i] * scale) + 4;
0N/A
0N/A // draw the value at the end of the bar
0N/A g.setColor(g.getColor().darker());
0N/A g.drawString(Integer.toString(values[i]), cx, cy);
0N/A }
0N/A }
0N/A
0N/A private void paintVertical(Graphics g) {
0N/A int barWidth = maxLabelWidth;
0N/A
0N/A for (int i = 0; i < columns; i++) {
0N/A
0N/A // X coordinate for this label and bar (centered)
0N/A int widthOfItems = (barWidth + barSpacing) * columns - barSpacing;
0N/A int cx = Math.max((getSize().width - widthOfItems) / 2, 0);
0N/A cx += (maxLabelWidth + barSpacing) * i;
0N/A
0N/A // Y coordinate for this label and bar
0N/A int cy = getSize().height - metrics.getHeight()
0N/A - metrics.getDescent() - 4;
0N/A
0N/A // draw the label
0N/A g.setColor(Color.black);
0N/A g.drawString(labels[i], cx, cy);
0N/A cy -= metrics.getHeight() - 3;
0N/A
0N/A // draw the shadow
0N/A g.fillRect(cx + 4, cy - (values[i] * scale) - 4,
0N/A barWidth, (values[i] * scale));
0N/A
0N/A // draw the bar
0N/A g.setColor(colors[i]);
0N/A if (styles[i] == STRIPED) {
0N/A for (int k = 0; k <= values[i] * scale; k += 2) {
0N/A g.drawLine(cx, cy - k,
0N/A cx + barWidth, cy - k);
0N/A }
0N/A } else {
0N/A g.fillRect(cx, cy - (values[i] * scale),
0N/A barWidth + 1, (values[i] * scale) + 1);
0N/A }
0N/A cy -= (values[i] * scale) + 5;
0N/A
0N/A // draw the value on top of the bar
0N/A g.setColor(g.getColor().darker());
0N/A g.drawString(Integer.toString(values[i]), cx, cy);
0N/A }
0N/A }
0N/A
0N/A @Override
0N/A public String getAppletInfo() {
0N/A return "Title: Bar Chart \n"
0N/A + "Author: Sami Shaio \n"
0N/A + "A simple bar chart demo.";
0N/A }
0N/A
0N/A @Override
0N/A public String[][] getParameterInfo() {
0N/A String[][] info = {
0N/A { "title", "string", "The title of bar graph. Default is 'Chart'" },
0N/A { "scale", "int", "The scale of the bar graph. Default is 10." },
0N/A { "columns", "int", "The number of columns/rows. Default is 5." },
0N/A { "orientation", "{VERTICAL, HORIZONTAL}",
0N/A "The orienation of the bar graph. Default is VERTICAL." },
0N/A { "c#", "int", "Subsitute a number for #. "
0N/A + "The value/size of bar #. Default is 0." },
0N/A { "c#_label", "string", "The label for bar #. "
0N/A + "Default is an empty label." },
0N/A { "c#_style", "{SOLID, STRIPED}", "The style of bar #. "
0N/A + "Default is SOLID." },
0N/A { "c#_color", "{RED, GREEN, BLUE, PINK, ORANGE, MAGENTA, CYAN, "
0N/A + "WHITE, YELLOW, GRAY, DARKGRAY}",
0N/A "The color of bar #. Default is GRAY." }
0N/A };
0N/A return info;
}
}