001/* ---------------------------------------------------------------------------- 002 The Kiwi Toolkit - A Java Class Library 003 Copyright (C) 1998-2004 Mark A. Lindner 004 005 This library is free software; you can redistribute it and/or 006 modify it under the terms of the GNU General Public License as 007 published by the Free Software Foundation; either version 2 of the 008 License, or (at your option) any later version. 009 010 This library is distributed in the hope that it will be useful, 011 but WITHOUT ANY WARRANTY; without even the implied warranty of 012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 General Public License for more details. 014 015 You should have received a copy of the GNU General Public License 016 along with this library; if not, write to the Free Software 017 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 018 02111-1307, USA. 019 020 The author may be contacted at: mark_a_lindner@yahoo.com 021 ---------------------------------------------------------------------------- 022 $Log: BarChart3D.java,v $ 023 Revision 1.8 2004/05/05 22:24:14 markl 024 comment block updates 025 026 Revision 1.7 2003/01/19 09:33:21 markl 027 Javadoc & comment header updates. 028 029 Revision 1.6 2001/03/12 07:23:28 markl 030 Javadoc cleanup. 031 032 Revision 1.5 2000/10/24 01:32:32 markl 033 Off-by-1 painting bugs fixed. 034 035 Revision 1.4 2000/10/17 19:03:11 markl 036 Fixed off-by-1 pixel painting error. 037 038 Revision 1.3 2000/10/13 08:06:04 markl 039 Integration fixes, cleanup. 040 041 Revision 1.2 2000/10/13 02:04:18 markl 042 Added remaining classes, and integrated components with models. 043 ---------------------------------------------------------------------------- 044*/ 045 046package kiwi.ui.graph; 047 048import java.awt.*; 049import java.util.*; 050import javax.swing.*; 051 052import kiwi.util.*; 053 054/** A base class for bar charts that provides some general painting logic. 055 * 056 * @author Mark Lindner 057 */ 058 059public abstract class BarChart3D extends ChartView 060 { 061 /** The width of a bar. 062 */ 063 064 protected int barWidth = 10; 065 066 /** The amount of space between bars (or bar clusters). 067 */ 068 069 protected int barSpacing = 20; 070 071 /** The depth of a bar (shading). 072 */ 073 074 protected int barDepth = barWidth / 2; 075 076 /** Construct a new <code>BarChart3D</code> for the specified chart 077 * definition and orientation. 078 * 079 * @param chart The chart definition. 080 * @param orientation The orientation; one of the symbolic constants 081 * <code>HORIZONTAL</code> or <code>VERTICAL</code>. 082 */ 083 084 public BarChart3D(Chart chart, int orientation) 085 { 086 super(chart); 087 setOrientation(orientation); 088 089 setTickInterval(chart.getTickInterval()); 090 } 091 092 /** Draw a horizontal bar. 093 * 094 * @param gc The graphics context. 095 * @param x The x-coordinate of the upper left corner of the base. 096 * @param y The y-coordinate of the upper left corner of the base. 097 * @param value The value that this bar represents. 098 * @param color The color of the bar. 099 * 100 * @return The x-coordinate of the end of the bar. 101 */ 102 103 protected int drawHorizontalBar(Graphics gc, int x, int y, double value, 104 Color color) 105 { 106 int px, py; 107 int barLength = (int)(value * scale); 108 int ox = x; 109 int oy = y; 110 111 // draw face of bar 112 113 gc.setColor(color); 114 gc.fillRect(ox, oy, barLength, barWidth); 115 116 // draw shadow (above) 117 118 px = ox; 119 py = oy - 1; 120 121 gc.setColor(color.darker()); 122 for(int i = 0; i < barDepth; i++) 123 { 124 gc.drawLine(px, py, px + barLength, py); 125 px++; 126 py--; 127 } 128 129 // draw end of bar 130 131 px = ox + barLength - 1; 132 py = oy - 1; 133 134 gc.setColor(color); 135 for(int i = 0; i < barDepth; i++) 136 { 137 gc.drawLine(px, py, px, py + barWidth); 138 px++; 139 py--; 140 } 141 142 // return the x coordinate of the end of this bar 143 144 return(x + barLength); 145 } 146 147 /** Draw a vertical bar. 148 * 149 * @param gc The graphics context. 150 * @param x The x-coordinate of the lower left corner of the base. 151 * @param y The y-coordinate of the lower left corner of the base. 152 * @param value The value that this bar represents. 153 * @param color The color of the bar. 154 * 155 * @return The y-coordinate of the top of the bar. 156 */ 157 158 protected int drawVerticalBar(Graphics gc, int x, int y, double value, 159 Color color) 160 { 161 int barDepth = barWidth / 2; 162 int px, py; 163 int barLength = (int)(value * scale); 164 int ox = x; 165 int oy = getSize().height - y - barLength; 166 167 // draw face of bar 168 169 gc.setColor(color); 170 gc.fillRect(ox, oy, barWidth, barLength); 171 172 // draw shadow (right side) 173 174 px = ox + barWidth; 175 py = oy; 176 177 gc.setColor(color.darker()); 178 for(int i = 0; i < barDepth; i++) 179 { 180 gc.drawLine(px, py, px, py + barLength - 1); 181 px++; 182 py--; 183 } 184 185 // draw top of bar 186 187 px = ox; 188 py = oy - 1; 189 190 gc.setColor(color); 191 for(int i = 0; i < barDepth; i++) 192 { 193 gc.drawLine(px, py, px + barWidth + 1, py); 194 px++; 195 py--; 196 } 197 198 // return the y coordinate of the top of this bar 199 200 return(y + barLength); 201 } 202 203 } 204 205/* end of source file */