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: ClusteredBarChart3D.java,v $ 023 Revision 1.7 2004/05/05 22:24:14 markl 024 comment block updates 025 026 Revision 1.6 2003/01/19 09:33:21 markl 027 Javadoc & comment header updates. 028 029 Revision 1.5 2001/03/12 07:23:29 markl 030 Javadoc cleanup. 031 032 Revision 1.4 2000/10/17 20:43:10 markl 033 Fixes to catch null values. 034 035 Revision 1.3 2000/10/15 09:40:29 markl 036 Added javadoc and final API polishing. 037 038 Revision 1.2 2000/10/13 02:04:19 markl 039 Added remaining classes, and integrated components with models. 040 ---------------------------------------------------------------------------- 041*/ 042 043package kiwi.ui.graph; 044 045import java.awt.*; 046import java.util.*; 047import javax.swing.*; 048 049import kiwi.util.*; 050 051/** A bar chart that renders each data sample as a cluster of bars; each 052 * bar in the cluster represents one of the values in the data sample. This 053 * type of chart is used to compare values across data samples. 054 * 055 * <p><center><img src="snapshot/ClusteredBarChart3D.gif"><br> 056 * <i>An example ClusteredBarChart3D.</i> 057 * </center> 058 * 059 * @author Mark Lindner 060 */ 061 062public class ClusteredBarChart3D extends BarChart3D 063 { 064 065 /** Construct a new <code>ClusteredBarChart3D</code> for the specified chart 066 * definition and with the specified orientation. 067 * 068 * @param chart The chart definition. 069 * @param orientation The orientation of the chart; one of the constants 070 * <code>VERTICAL</code> or <code>HORIZONTAL</code> defined in 071 * <code>ChartView</code>. 072 */ 073 074 public ClusteredBarChart3D(Chart chart, int orientation) 075 { 076 super(chart, orientation); 077 } 078 079 /** Paint the chart. 080 */ 081 082 protected void paintChart(Graphics gc) 083 { 084 Dimension d = getSize(); 085 int cx = horizontalPad; 086 int cy = verticalPad; 087 088 if(orientation == HORIZONTAL) 089 cy += barDepth + scaleWidth + verticalPad; 090 else 091 cx += scaleWidth + horizontalPad; 092 093 // loop over the bar clusters 094 095 Enumeration e = model.getDataSamples(); 096 while(e.hasMoreElements()) 097 { 098 // for(int i = 0; i < bars.size(); i++) 099 // { 100 // Bar bar = (Bar)(bars.elementAt(i)); 101 102 DataSample ds = (DataSample)e.nextElement(); 103 104 Enumeration f = chart.getValues(); 105 int valueCount = chart.getValueCount(); 106 int skip = (valueCount - 1) * barWidth; 107 if(orientation == HORIZONTAL) 108 cy += skip; 109 110 // loop over the bars in a cluster; if it's a horizontal bar chart, 111 // we have to draw them in reverse, because the shadows are above the 112 // bars 113 114 while(f.hasMoreElements()) 115 { 116 ChartValue cv = (ChartValue)f.nextElement(); 117 118 Color color = cv.getColor(); 119 Object o = ds.getValue(cv.getName()); 120 double value = 0.0; 121 if((o != null) && (o instanceof Number)) 122 value = ((Number)o).doubleValue(); 123 124 switch (orientation) 125 { 126 case VERTICAL: 127 default: 128 { 129 drawVerticalBar(gc, cx, verticalPad, value, color); 130 cx += barWidth; 131 break; 132 } 133 134 case HORIZONTAL: 135 { 136 drawHorizontalBar(gc, horizontalPad, cy, value, color); 137 cy -= barWidth; 138 break; 139 } 140 } 141 } 142 143 if(orientation == VERTICAL) 144 cx += barSpacing; 145 else 146 cy += skip + barWidth + barSpacing; 147 } 148 149 // draw the scale 150 151 switch(orientation) 152 { 153 case HORIZONTAL: 154 drawHorizontalScale(gc, verticalPad + scaleWidth); 155 break; 156 157 case VERTICAL: 158 drawVerticalScale(gc, horizontalPad + scaleWidth); 159 break; 160 } 161 } 162 163 /** Compute the maximum value. 164 */ 165 166 protected double getMaxValue() 167 { 168 double maxval = 0.0; 169 170 Enumeration e = chart.getValues(); 171 while(e.hasMoreElements()) 172 { 173 ChartValue cv = (ChartValue)e.nextElement(); 174 String var = cv.getName(); 175 176 Enumeration f = model.getDataSamples(); 177 while(f.hasMoreElements()) 178 { 179 DataSample ds = (DataSample)f.nextElement(); 180 Object o = ds.getValue(var); 181 double value = 0.0; 182 if((o != null) && (o instanceof Number)) 183 value = ((Number)o).doubleValue(); 184 185 if(value > maxval) 186 maxval = value; 187 } 188 } 189 190 return(maxval); 191 } 192 193 } 194 195/* end of source file */