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: StackedBarChart3D.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:30 markl 030 Javadoc cleanup. 031 032 Revision 1.5 2000/10/17 20:43:11 markl 033 Fixes to catch null values. 034 035 Revision 1.4 2000/10/15 09:40:30 markl 036 Added javadoc and final API polishing. 037 038 Revision 1.3 2000/10/13 08:06:05 markl 039 Integration fixes, cleanup. 040 041 Revision 1.2 2000/10/13 02:04:20 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 bar chart that renders each data sample as a stack of bars; each 055 * bar in the stack represents one of the values in the data sample. This type 056 * of chart is used to compare the contributions of values to the total for 057 * each data sample and across data samples. 058 * 059 * <p><center><img src="snapshot/StackedBarChart3D.gif"><br> 060 * <i>An example StackedBarChart3D.</i> 061 * </center> 062 * 063 * @author Mark Lindner 064 */ 065 066public class StackedBarChart3D extends BarChart3D 067 { 068 069 /** Construct a new <code>StackedBarChart3D</code> for the specified chart 070 * definition and with the specified orientation. 071 * 072 * @param chart The chart definition. 073 * @param orientation The orientation of the chart; one of the constants 074 * <code>VERTICAL</code> or <code>HORIZONTAL</code> defined in 075 * <code>ChartView</code>. 076 */ 077 078 public StackedBarChart3D(Chart chart, int orientation) 079 { 080 super(chart, orientation); 081 } 082 083 /** Paint the chart. 084 */ 085 086 protected void paintChart(Graphics gc) 087 { 088 Dimension d = getSize(); 089 int cx = horizontalPad; 090 int cy = verticalPad; 091 092 if(orientation == HORIZONTAL) 093 cy += barDepth + scaleWidth + verticalPad; 094 else 095 cx += scaleWidth + horizontalPad; 096 097 // loop over the bar clusters 098 099 Enumeration e = model.getDataSamples(); 100 while(e.hasMoreElements()) 101 { 102 DataSample ds = (DataSample)e.nextElement(); 103 104 // loop over the bars in a cluster 105 106 int ox = horizontalPad; 107 int oy = verticalPad; 108 109 Enumeration f = chart.getValues(); 110 111 while(f.hasMoreElements()) 112 { 113 ChartValue cv = (ChartValue)f.nextElement(); 114 Color color = cv.getColor(); 115 double value = 0.0; 116 Object o = ds.getValue(cv.getName()); 117 if((o != null) && (o instanceof Number)) 118 value = ((Number)o).doubleValue(); 119 120 switch(orientation) 121 { 122 case VERTICAL: 123 default: 124 { 125 oy = drawVerticalBar(gc, cx, oy, value, color); 126 break; 127 } 128 129 case HORIZONTAL: 130 { 131 ox = drawHorizontalBar(gc, ox, cy, value, color); 132 break; 133 } 134 } 135 } 136 137 if(orientation == VERTICAL) 138 cx += barWidth + barSpacing; 139 else 140 cy += barWidth + barSpacing; 141 } 142 143 // draw the scale 144 145 switch(orientation) 146 { 147 case HORIZONTAL: 148 drawHorizontalScale(gc, verticalPad + scaleWidth); 149 break; 150 151 case VERTICAL: 152 drawVerticalScale(gc, horizontalPad + scaleWidth); 153 break; 154 } 155 } 156 157 /** Compute the maximum value. 158 */ 159 160 protected double getMaxValue() 161 { 162 double maxval = 0.0; 163 164 Enumeration e = model.getDataSamples(); 165 while(e.hasMoreElements()) 166 { 167 DataSample ds = (DataSample)e.nextElement(); 168 169 double total = 0.0; 170 Enumeration f = chart.getValues(); 171 while(f.hasMoreElements()) 172 { 173 ChartValue cv = (ChartValue)f.nextElement(); 174 Object o = ds.getValue(cv.getName()); 175 double value = 0.0; 176 if((o != null) && (o instanceof Number)) 177 value = ((Number)o).doubleValue(); 178 179 total += value; 180 181 } 182 183 if(total > maxval) 184 maxval = total; 185 } 186 187 return(maxval); 188 } 189 190 } 191 192/* end of source file */