001/* =========================================================== 002 * JFreeChart : a free chart library for the Java(tm) platform 003 * =========================================================== 004 * 005 * (C) Copyright 2000-2013, by Object Refinery Limited and Contributors. 006 * 007 * Project Info: http://www.jfree.org/jfreechart/index.html 008 * 009 * This library is free software; you can redistribute it and/or modify it 010 * under the terms of the GNU Lesser General Public License as published by 011 * the Free Software Foundation; either version 2.1 of the License, or 012 * (at your option) any later version. 013 * 014 * This library is distributed in the hope that it will be useful, but 015 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 016 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 017 * License for more details. 018 * 019 * You should have received a copy of the GNU Lesser General Public 020 * License along with this library; if not, write to the Free Software 021 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, 022 * USA. 023 * 024 * [Oracle and Java are registered trademarks of Oracle and/or its affiliates. 025 * Other names may be trademarks of their respective owners.] 026 * 027 * ------------------- 028 * CategoryAxis3D.java 029 * ------------------- 030 * (C) Copyright 2003-2013, by Klaus Rheinwald and Contributors. 031 * 032 * Original Author: Klaus Rheinwald; 033 * Contributor(s): Tin Luu, 034 * David Gilbert (for Object Refinery Limited); 035 * Adriaan Joubert; 036 * 037 * Changes 038 * ------- 039 * 19-Feb-2003 : File creation; 040 * 21-Mar-2003 : Added to JFreeChart CVS, see bug id 685501 for code 041 * contribution from KR (DG); 042 * 26-Mar-2003 : Implemented Serializable (DG); 043 * 13-May-2003 : Renamed HorizontalCategoryAxis3D --> CategoryAxis3D, and 044 * modified to take into account the plot orientation (DG); 045 * 14-Aug-2003 : Implemented Cloneable (DG); 046 * 21-Aug-2003 : Fixed draw() method bugs (DG); 047 * 22-Mar-2004 : Added workaround for bug 920959 (null pointer exception with 048 * no renderer) (DG); 049 * ------------- JFREECHART 1.0.x --------------------------------------------- 050 * 18-Aug-2006 : Fix for bug drawing category labels, thanks to Adriaan 051 * Joubert (1277726) (DG); 052 * 16-Apr-2009 : Draw axis line and tick marks (DG); 053 * 01-Aug-2013 : Added attributedLabel override to support superscripts, 054 * subscripts and more (DG); 055 * 056 */ 057 058package org.jfree.chart.axis; 059 060import java.awt.Graphics2D; 061import java.awt.geom.Rectangle2D; 062import java.io.Serializable; 063 064import org.jfree.chart.Effect3D; 065import org.jfree.chart.plot.CategoryPlot; 066import org.jfree.chart.plot.PlotRenderingInfo; 067import org.jfree.chart.renderer.category.CategoryItemRenderer; 068import org.jfree.ui.RectangleEdge; 069 070/** 071 * An axis that displays categories and has a 3D effect. 072 * Used for bar charts and line charts. 073 */ 074public class CategoryAxis3D extends CategoryAxis implements Cloneable, 075 Serializable { 076 077 /** For serialization. */ 078 private static final long serialVersionUID = 4114732251353700972L; 079 080 /** 081 * Creates a new axis. 082 */ 083 public CategoryAxis3D() { 084 this(null); 085 } 086 087 /** 088 * Creates a new axis using default attribute values. 089 * 090 * @param label the axis label (<code>null</code> permitted). 091 */ 092 public CategoryAxis3D(String label) { 093 super(label); 094 } 095 096 /** 097 * Draws the axis on a Java 2D graphics device (such as the screen or a 098 * printer). 099 * 100 * @param g2 the graphics device (<code>null</code> not permitted). 101 * @param cursor the cursor location. 102 * @param plotArea the area within which the axis should be drawn 103 * (<code>null</code> not permitted). 104 * @param dataArea the area within which the plot is being drawn 105 * (<code>null</code> not permitted). 106 * @param edge the location of the axis (<code>null</code> not permitted). 107 * @param plotState collects information about the plot (<code>null</code> 108 * permitted). 109 * 110 * @return The axis state (never <code>null</code>). 111 */ 112 @Override 113 public AxisState draw(Graphics2D g2, double cursor, Rectangle2D plotArea, 114 Rectangle2D dataArea, RectangleEdge edge, 115 PlotRenderingInfo plotState) { 116 117 // if the axis is not visible, don't draw it... 118 if (!isVisible()) { 119 return new AxisState(cursor); 120 } 121 122 // calculate the adjusted data area taking into account the 3D effect... 123 // this assumes that there is a 3D renderer, all this 3D effect is a 124 // bit of an ugly hack... 125 CategoryPlot plot = (CategoryPlot) getPlot(); 126 127 Rectangle2D adjustedDataArea = new Rectangle2D.Double(); 128 if (plot.getRenderer() instanceof Effect3D) { 129 Effect3D e3D = (Effect3D) plot.getRenderer(); 130 double adjustedX = dataArea.getMinX(); 131 double adjustedY = dataArea.getMinY(); 132 double adjustedW = dataArea.getWidth() - e3D.getXOffset(); 133 double adjustedH = dataArea.getHeight() - e3D.getYOffset(); 134 135 if (edge == RectangleEdge.LEFT || edge == RectangleEdge.BOTTOM) { 136 adjustedY += e3D.getYOffset(); 137 } 138 else if (edge == RectangleEdge.RIGHT || edge == RectangleEdge.TOP) { 139 adjustedX += e3D.getXOffset(); 140 } 141 adjustedDataArea.setRect(adjustedX, adjustedY, adjustedW, 142 adjustedH); 143 } 144 else { 145 adjustedDataArea.setRect(dataArea); 146 } 147 148 if (isAxisLineVisible()) { 149 drawAxisLine(g2, cursor, adjustedDataArea, edge); 150 } 151 // draw the category labels and axis label 152 AxisState state = new AxisState(cursor); 153 if (isTickMarksVisible()) { 154 drawTickMarks(g2, cursor, adjustedDataArea, edge, state); 155 } 156 state = drawCategoryLabels(g2, plotArea, adjustedDataArea, edge, 157 state, plotState); 158 if (getAttributedLabel() != null) { 159 state = drawAttributedLabel(getAttributedLabel(), g2, plotArea, 160 dataArea, edge, state); 161 162 } else { 163 state = drawLabel(getLabel(), g2, plotArea, dataArea, edge, state); 164 } 165 return state; 166 } 167 168 /** 169 * Returns the Java 2D coordinate for a category. 170 * 171 * @param anchor the anchor point. 172 * @param category the category index. 173 * @param categoryCount the category count. 174 * @param area the data area. 175 * @param edge the location of the axis. 176 * 177 * @return The coordinate. 178 */ 179 @Override 180 public double getCategoryJava2DCoordinate(CategoryAnchor anchor, 181 int category, int categoryCount, Rectangle2D area, 182 RectangleEdge edge) { 183 184 double result = 0.0; 185 Rectangle2D adjustedArea = area; 186 CategoryPlot plot = (CategoryPlot) getPlot(); 187 CategoryItemRenderer renderer = plot.getRenderer(); 188 if (renderer instanceof Effect3D) { 189 Effect3D e3D = (Effect3D) renderer; 190 double adjustedX = area.getMinX(); 191 double adjustedY = area.getMinY(); 192 double adjustedW = area.getWidth() - e3D.getXOffset(); 193 double adjustedH = area.getHeight() - e3D.getYOffset(); 194 195 if (edge == RectangleEdge.LEFT || edge == RectangleEdge.BOTTOM) { 196 adjustedY += e3D.getYOffset(); 197 } 198 else if (edge == RectangleEdge.RIGHT || edge == RectangleEdge.TOP) { 199 adjustedX += e3D.getXOffset(); 200 } 201 adjustedArea = new Rectangle2D.Double(adjustedX, adjustedY, 202 adjustedW, adjustedH); 203 } 204 205 if (anchor == CategoryAnchor.START) { 206 result = getCategoryStart(category, categoryCount, adjustedArea, 207 edge); 208 } 209 else if (anchor == CategoryAnchor.MIDDLE) { 210 result = getCategoryMiddle(category, categoryCount, adjustedArea, 211 edge); 212 } 213 else if (anchor == CategoryAnchor.END) { 214 result = getCategoryEnd(category, categoryCount, adjustedArea, 215 edge); 216 } 217 return result; 218 219 } 220 221 /** 222 * Returns a clone of the axis. 223 * 224 * @return A clone. 225 * 226 * @throws CloneNotSupportedException If the axis is not cloneable for 227 * some reason. 228 */ 229 @Override 230 public Object clone() throws CloneNotSupportedException { 231 return super.clone(); 232 } 233 234}