/* * @(#)CBarChartUI.java 0.0.0 99/07/24 * * Copyright (c) 1999 by Willie Wheeler. All rights reserved. */ package stats.plaf.concrete; import java.awt.*; import java.text.*; import javax.swing.*; import javax.swing.plaf.*; import stats.*; import stats.plaf.*; /** * @version 0.0.0 07/24/99 * @author Willie Wheeler */ public class CBarChartUI extends BarChartUI { protected static final int XPAD = 5, YPAD = 5; protected static final int LABEL_WIDTH = 27; protected static final float FONT_SIZE = 10.0f; protected static ComponentUI ui = new CBarChartUI(); protected static NumberFormat nf; static { nf = NumberFormat.getNumberInstance(); nf.setMinimumFractionDigits(2); nf.setMaximumFractionDigits(2); } protected double max, sum; public static ComponentUI createUI(JComponent c) { return ui; } public void installUI(JComponent c) { c.setOpaque(false); } public void uninstallUI(JComponent c) { } public void paint(Graphics g, JComponent c) { Graphics2D g2 = (Graphics2D)g; BarChart chart = (BarChart)c; int chartWidth = chart.getSize().width; int chartHeight = chart.getSize().height; int xMin = XPAD + LABEL_WIDTH; int xMax = chartWidth - XPAD; int xRange = xMax - xMin; int yMin = YPAD; int yMax = chartHeight - YPAD; int yRange = yMax - yMin; int numCats = chart.getNumStates(); int numRangeGroups = chart.getNumRangeGroups(); double groupWidth = 1.0 / numRangeGroups; // Compute column stats. We assume nonnegative numeric values! max = 0.0; sum = 0.0; for (int i = 0; i < numCats; i++) { double value = chart.getStateValue(i); max = Math.max(value, max); sum += value; } double tempMax = (max == 0.0 ? 1.0 : max); double tempSum = (sum == 0.0 ? 1.0 : sum); double div = (chart.getAutoScale() ? tempMax : tempSum); double scale = yRange / div; // Paint range group lines. if (numRangeGroups > 0) { g2.setPaint(Color.lightGray); g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f, new float[] { 2.0f, 2.0f }, 0.0f)); for (int i = 0; i < numRangeGroups + 1; i++) { int y = yMax - (int)(scale * i * groupWidth * tempSum); if (yMin <= y) { g2.drawLine(xMin, y, xMax, y); } } g2.setStroke(new BasicStroke()); } // Paint bars. int barWidth = xRange / numCats; for (int i = 0; i < numCats; i++) { // Compute the bar bounds. int barHeight = (int)(scale * chart.getStateValue(i)); int barX = xMin + (i * barWidth); int barY = yMax - barHeight; // Draw the bar. g2.setPaint(chart.getStateColor(i)); g2.fillRect(barX, barY, barWidth, barHeight); g2.setPaint(Color.black); g2.drawRect(barX, barY, barWidth, barHeight); } // Paint axes. g2.setPaint(Color.black); g2.drawLine(xMin-1, yMax, xMax, yMax); // Horizontal axis. g2.drawLine(xMin, yMin, xMin, yMax); // Vertical axis. // Paint range groups. if (numRangeGroups > 0) { g2.setFont(c.getFont().deriveFont(FONT_SIZE)); g2.setStroke(new BasicStroke(1.0f, BasicStroke.CAP_BUTT, BasicStroke.JOIN_BEVEL, 0.0f, new float[] { 2.0f, 2.0f }, 0.0f)); for (int i = 1; i <= numRangeGroups; i++) { double bd = i * groupWidth; int y = yMax - (int)(scale * bd * tempSum); if (yMin <= y) { g2.drawLine(xMin-1, y, xMin+1, y); g2.drawString(nf.format(bd), XPAD, y + (int)(FONT_SIZE/2.0f)); } } } } }