1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2005, by Object Refinery Limited and Contributors.
  6. *
  7. * Project Info: http://www.jfree.org/jfreechart/index.html
  8. *
  9. * This library is free software; you can redistribute it and/or modify it under the terms
  10. * of the GNU Lesser General Public License as published by the Free Software Foundation;
  11. * either version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  14. * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. * See the GNU Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public License along with this
  18. * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
  19. * Boston, MA 02111-1307, USA.
  20. *
  21. * [Java is a trademark or registered trademark of Sun Microsystems, Inc.
  22. * in the United States and other countries.]
  23. *
  24. * -------------------------
  25. * StackedXYBarRenderer.java
  26. * -------------------------
  27. * (C) Copyright 2004, 2005, by Andreas Schroeder and Contributors.
  28. *
  29. * Original Author: Andreas Schroeder;
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: StackedXYBarRenderer.java,v 1.8 2005/01/25 22:40:35 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 01-Apr-2004 : Version 1 (AS);
  37. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with
  38. * getYValue() (DG);
  39. * 15-Aug-2004 : Added drawBarOutline to control draw/don't-draw bar
  40. * outlines (BN);
  41. * 10-Sep-2004 : drawBarOutline attribute is now inherited from XYBarRenderer
  42. * and double primitives are retrieved from the dataset rather
  43. * than Number objects (DG);
  44. * 07-Jan-2005 : Updated for method name change in DatasetUtilities (DG);
  45. * 25-Jan-2005 : Modified to handle negative values correctly (DG);
  46. *
  47. */
  48. package org.jfree.chart.renderer.xy;
  49. import java.awt.Graphics2D;
  50. import java.awt.geom.Rectangle2D;
  51. import org.jfree.chart.axis.ValueAxis;
  52. import org.jfree.chart.entity.EntityCollection;
  53. import org.jfree.chart.entity.XYItemEntity;
  54. import org.jfree.chart.labels.XYToolTipGenerator;
  55. import org.jfree.chart.plot.CrosshairState;
  56. import org.jfree.chart.plot.PlotOrientation;
  57. import org.jfree.chart.plot.PlotRenderingInfo;
  58. import org.jfree.chart.plot.XYPlot;
  59. import org.jfree.data.Range;
  60. import org.jfree.data.general.DatasetUtilities;
  61. import org.jfree.data.xy.IntervalXYDataset;
  62. import org.jfree.data.xy.TableXYDataset;
  63. import org.jfree.data.xy.XYDataset;
  64. import org.jfree.ui.RectangleEdge;
  65. /**
  66. * A bar renderer that displays the series items stacked.
  67. * The dataset used together with this renderer must be a
  68. * {@link org.jfree.data.xy.IntervalXYDataset} and a
  69. * {@link org.jfree.data.xy.TableXYDataset}. For example, the
  70. * dataset class {@link org.jfree.data.xy.CategoryTableXYDataset}
  71. * implements both interfaces.
  72. *
  73. * @author andreas.schroeder
  74. */
  75. public class StackedXYBarRenderer extends XYBarRenderer {
  76. /**
  77. * Creates a new renderer.
  78. */
  79. public StackedXYBarRenderer() {
  80. super();
  81. }
  82. /**
  83. * Creates a new renderer.
  84. *
  85. * @param margin the percentual amount of the bars that are cut away.
  86. */
  87. public StackedXYBarRenderer(double margin) {
  88. super(margin);
  89. }
  90. /**
  91. * Initialises the renderer and returns a state object that should be
  92. * passed to all subsequent calls to the drawItem() method. Here there is
  93. * nothing to do.
  94. *
  95. * @param g2 the graphics device.
  96. * @param dataArea the area inside the axes.
  97. * @param plot the plot.
  98. * @param data the data.
  99. * @param info an optional info collection object to return data back to
  100. * the caller.
  101. *
  102. * @return A state object.
  103. */
  104. public XYItemRendererState initialise(Graphics2D g2,
  105. Rectangle2D dataArea,
  106. XYPlot plot,
  107. XYDataset data,
  108. PlotRenderingInfo info) {
  109. return new XYBarRendererState(info);
  110. }
  111. /**
  112. * Returns the range of values the renderer requires to display all the
  113. * items from the specified dataset.
  114. *
  115. * @param dataset the dataset (<code>null</code> permitted).
  116. *
  117. * @return The range (<code>null</code> if the dataset is <code>null</code>
  118. * or empty).
  119. */
  120. public Range findRangeBounds(XYDataset dataset) {
  121. if (dataset != null) {
  122. return DatasetUtilities.findStackedRangeBounds((TableXYDataset) dataset);
  123. }
  124. else {
  125. return null;
  126. }
  127. }
  128. /**
  129. * Draws the visual representation of a single data item.
  130. *
  131. * @param g2 the graphics device.
  132. * @param state the renderer state.
  133. * @param dataArea the area within which the plot is being drawn.
  134. * @param info collects information about the drawing.
  135. * @param plot the plot (can be used to obtain standard color information
  136. * etc).
  137. * @param domainAxis the domain axis.
  138. * @param rangeAxis the range axis.
  139. * @param dataset the dataset.
  140. * @param series the series index (zero-based).
  141. * @param item the item index (zero-based).
  142. * @param crosshairState crosshair information for the plot
  143. * (<code>null</code> permitted).
  144. * @param pass the pass index.
  145. */
  146. public void drawItem(Graphics2D g2,
  147. XYItemRendererState state,
  148. Rectangle2D dataArea,
  149. PlotRenderingInfo info,
  150. XYPlot plot,
  151. ValueAxis domainAxis,
  152. ValueAxis rangeAxis,
  153. XYDataset dataset,
  154. int series,
  155. int item,
  156. CrosshairState crosshairState,
  157. int pass) {
  158. if (!(dataset instanceof IntervalXYDataset && dataset instanceof TableXYDataset)) {
  159. String message = "dataset (type " + dataset.getClass().getName() + ") has wrong type:";
  160. boolean and = false;
  161. if (!IntervalXYDataset.class.isAssignableFrom(dataset.getClass())) {
  162. message += " it is no IntervalXYDataset";
  163. and = true;
  164. }
  165. if (!TableXYDataset.class.isAssignableFrom(dataset.getClass())) {
  166. if (and) {
  167. message += " and";
  168. }
  169. message += " it is no TableXYDataset";
  170. }
  171. throw new IllegalArgumentException(message);
  172. }
  173. IntervalXYDataset intervalDataset = (IntervalXYDataset) dataset;
  174. double value = intervalDataset.getYValue(series, item);
  175. if (Double.isNaN(value)) {
  176. return;
  177. }
  178. double positiveBase = 0.0;
  179. double negativeBase = 0.0;
  180. for (int i = 0; i < series; i++) {
  181. double v = dataset.getYValue(i, item);
  182. if (!Double.isNaN(v)) {
  183. if (v > 0) {
  184. positiveBase = positiveBase + v;
  185. }
  186. else {
  187. negativeBase = negativeBase + v;
  188. }
  189. }
  190. }
  191. double translatedBase;
  192. double translatedValue;
  193. RectangleEdge edgeR = plot.getRangeAxisEdge();
  194. if (value > 0.0) {
  195. translatedBase = rangeAxis.valueToJava2D(positiveBase, dataArea, edgeR);
  196. translatedValue = rangeAxis.valueToJava2D(positiveBase + value, dataArea, edgeR);
  197. }
  198. else {
  199. translatedBase = rangeAxis.valueToJava2D(negativeBase, dataArea, edgeR);
  200. translatedValue = rangeAxis.valueToJava2D(negativeBase + value, dataArea, edgeR);
  201. }
  202. RectangleEdge edgeD = plot.getDomainAxisEdge();
  203. double startX = intervalDataset.getStartXValue(series, item);
  204. if (Double.isNaN(startX)) {
  205. return;
  206. }
  207. double translatedStartX = domainAxis.valueToJava2D(startX, dataArea, edgeD);
  208. double endX = intervalDataset.getEndXValue(series, item);
  209. if (Double.isNaN(endX)) {
  210. return;
  211. }
  212. double translatedEndX = domainAxis.valueToJava2D(endX, dataArea, edgeD);
  213. double translatedWidth = Math.max(1, Math.abs(translatedEndX - translatedStartX));
  214. double translatedHeight = Math.abs(translatedValue - translatedBase);
  215. if (getMargin() > 0.0) {
  216. double cut = translatedWidth * getMargin();
  217. translatedWidth = translatedWidth - cut;
  218. translatedStartX = translatedStartX + cut / 2;
  219. }
  220. Rectangle2D bar = null;
  221. PlotOrientation orientation = plot.getOrientation();
  222. if (orientation == PlotOrientation.HORIZONTAL) {
  223. bar = new Rectangle2D.Double(
  224. Math.min(translatedBase, translatedValue),
  225. translatedEndX,
  226. translatedHeight,
  227. translatedWidth
  228. );
  229. }
  230. else if (orientation == PlotOrientation.VERTICAL) {
  231. bar = new Rectangle2D.Double(
  232. translatedStartX,
  233. Math.min(translatedBase, translatedValue),
  234. translatedWidth,
  235. translatedHeight
  236. );
  237. }
  238. g2.setPaint(getItemPaint(series, item));
  239. g2.fill(bar);
  240. if (isDrawBarOutline() && Math.abs(translatedEndX - translatedStartX) > 3) {
  241. g2.setStroke(getItemStroke(series, item));
  242. g2.setPaint(getItemOutlinePaint(series, item));
  243. g2.draw(bar);
  244. }
  245. // add an entity for the item...
  246. if (info != null) {
  247. EntityCollection entities = info.getOwner().getEntityCollection();
  248. if (entities != null) {
  249. String tip = null;
  250. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  251. if (generator != null) {
  252. tip = generator.generateToolTip(dataset, series, item);
  253. }
  254. String url = null;
  255. if (getURLGenerator() != null) {
  256. url = getURLGenerator().generateURL(dataset, series, item);
  257. }
  258. XYItemEntity entity = new XYItemEntity(
  259. bar, dataset, series, item, tip, url
  260. );
  261. entities.add(entity);
  262. }
  263. }
  264. }
  265. }