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. * XYAreaRenderer.java
  26. * -------------------
  27. * (C) Copyright 2002-2005, by Hari and Contributors.
  28. *
  29. * Original Author: Hari (ourhari@hotmail.com);
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. * Richard Atkinson;
  32. * Christian W. Zuckschwerdt;
  33. *
  34. * $Id: XYAreaRenderer.java,v 1.4 2005/01/19 12:41:39 mungady Exp $
  35. *
  36. * Changes:
  37. * --------
  38. * 03-Apr-2002 : Version 1, contributed by Hari. This class is based on the StandardXYItemRenderer
  39. * class (DG);
  40. * 09-Apr-2002 : Removed the translated zero from the drawItem method - overridden the initialise()
  41. * method to calculate it (DG);
  42. * 30-May-2002 : Added tool tip generator to constructor to match super class (DG);
  43. * 25-Jun-2002 : Removed unnecessary local variable (DG);
  44. * 05-Aug-2002 : Small modification to drawItem method to support URLs for HTML image maps (RA);
  45. * 01-Oct-2002 : Fixed errors reported by Checkstyle (DG);
  46. * 07-Nov-2002 : Renamed AreaXYItemRenderer --> XYAreaRenderer (DG);
  47. * 25-Mar-2003 : Implemented Serializable (DG);
  48. * 01-May-2003 : Modified drawItem(...) method signature (DG);
  49. * 27-Jul-2003 : Made line and polygon properties protected rather than private (RA);
  50. * 30-Jul-2003 : Modified entity constructor (CZ);
  51. * 20-Aug-2003 : Implemented Cloneable and PublicCloneable (DG);
  52. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  53. * 07-Oct-2003 : Added renderer state (DG);
  54. * 08-Dec-2003 : Modified hotspot for chart entity (DG);
  55. * 10-Feb-2004 : Changed the drawItem() method to make cut-and-paste overriding easier. Also
  56. * moved state class into this class (DG);
  57. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed XYToolTipGenerator
  58. * --> XYItemLabelGenerator (DG);
  59. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  60. * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
  61. * 19-Jan-2005 : Now accesses primitives only from dataset (DG);
  62. *
  63. */
  64. package org.jfree.chart.renderer.xy;
  65. import java.awt.Graphics2D;
  66. import java.awt.Paint;
  67. import java.awt.Polygon;
  68. import java.awt.Shape;
  69. import java.awt.Stroke;
  70. import java.awt.geom.Line2D;
  71. import java.awt.geom.Rectangle2D;
  72. import java.io.Serializable;
  73. import org.jfree.chart.axis.ValueAxis;
  74. import org.jfree.chart.entity.EntityCollection;
  75. import org.jfree.chart.entity.XYItemEntity;
  76. import org.jfree.chart.labels.XYToolTipGenerator;
  77. import org.jfree.chart.plot.CrosshairState;
  78. import org.jfree.chart.plot.PlotOrientation;
  79. import org.jfree.chart.plot.PlotRenderingInfo;
  80. import org.jfree.chart.plot.XYPlot;
  81. import org.jfree.chart.urls.XYURLGenerator;
  82. import org.jfree.data.xy.XYDataset;
  83. import org.jfree.util.PublicCloneable;
  84. import org.jfree.util.ShapeUtilities;
  85. /**
  86. * Area item renderer for an {@link XYPlot}. This class can draw (a) shapes at each
  87. * point, or (b) lines between points, or (c) both shapes and lines, or (d)
  88. * filled areas, or (e) filled areas and shapes.
  89. */
  90. public class XYAreaRenderer extends AbstractXYItemRenderer
  91. implements XYItemRenderer,
  92. Cloneable,
  93. PublicCloneable,
  94. Serializable {
  95. /**
  96. * A state object used by this renderer.
  97. */
  98. static class XYAreaRendererState extends XYItemRendererState {
  99. /** Working storage for the area under one series. */
  100. public Polygon area;
  101. /** Working line that can be recycled. */
  102. public Line2D line;
  103. /**
  104. * Creates a new state.
  105. *
  106. * @param info the plot rendering info.
  107. */
  108. public XYAreaRendererState(PlotRenderingInfo info) {
  109. super(info);
  110. this.area = new Polygon();
  111. this.line = new Line2D.Double();
  112. }
  113. }
  114. /** Useful constant for specifying the type of rendering (shapes only). */
  115. public static final int SHAPES = 1;
  116. /** Useful constant for specifying the type of rendering (lines only). */
  117. public static final int LINES = 2;
  118. /** Useful constant for specifying the type of rendering (shapes and lines). */
  119. public static final int SHAPES_AND_LINES = 3;
  120. /** Useful constant for specifying the type of rendering (area only). */
  121. public static final int AREA = 4;
  122. /** Useful constant for specifying the type of rendering (area and shapes). */
  123. public static final int AREA_AND_SHAPES = 5;
  124. /** A flag indicating whether or not shapes are drawn at each XY point. */
  125. private boolean plotShapes;
  126. /** A flag indicating whether or not lines are drawn between XY points. */
  127. private boolean plotLines;
  128. /** A flag indicating whether or not Area are drawn at each XY point. */
  129. private boolean plotArea;
  130. /** A flag that controls whether or not the outline is shown. */
  131. private boolean showOutline;
  132. /**
  133. * Constructs a new renderer.
  134. */
  135. public XYAreaRenderer() {
  136. this(AREA);
  137. }
  138. /**
  139. * Constructs a new renderer.
  140. *
  141. * @param type the type of the renderer.
  142. */
  143. public XYAreaRenderer(int type) {
  144. this(type, null, null);
  145. }
  146. /**
  147. * Constructs a new renderer.
  148. * <p>
  149. * To specify the type of renderer, use one of the constants: SHAPES, LINES,
  150. * SHAPES_AND_LINES, AREA or AREA_AND_SHAPES.
  151. *
  152. * @param type the type of renderer.
  153. * @param toolTipGenerator the tool tip generator to use. <code>null</code> is none.
  154. * @param urlGenerator the URL generator (null permitted).
  155. */
  156. public XYAreaRenderer(int type,
  157. XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) {
  158. super();
  159. setToolTipGenerator(toolTipGenerator);
  160. setURLGenerator(urlGenerator);
  161. if (type == SHAPES) {
  162. this.plotShapes = true;
  163. }
  164. if (type == LINES) {
  165. this.plotLines = true;
  166. }
  167. if (type == SHAPES_AND_LINES) {
  168. this.plotShapes = true;
  169. this.plotLines = true;
  170. }
  171. if (type == AREA) {
  172. this.plotArea = true;
  173. }
  174. if (type == AREA_AND_SHAPES) {
  175. this.plotArea = true;
  176. this.plotShapes = true;
  177. }
  178. this.showOutline = false;
  179. }
  180. /**
  181. * Returns a flag that controls whether or not outlines of the areas are drawn.
  182. *
  183. * @return The flag.
  184. */
  185. public boolean isOutline() {
  186. return this.showOutline;
  187. }
  188. /**
  189. * Sets a flag that controls whether or not outlines of the areas are drawn.
  190. *
  191. * @param show the flag.
  192. */
  193. public void setOutline(boolean show) {
  194. this.showOutline = show;
  195. }
  196. /**
  197. * Returns true if shapes are being plotted by the renderer.
  198. *
  199. * @return <code>true</code> if shapes are being plotted by the renderer.
  200. */
  201. public boolean getPlotShapes() {
  202. return this.plotShapes;
  203. }
  204. /**
  205. * Returns true if lines are being plotted by the renderer.
  206. *
  207. * @return <code>true</code> if lines are being plotted by the renderer.
  208. */
  209. public boolean getPlotLines() {
  210. return this.plotLines;
  211. }
  212. /**
  213. * Returns true if Area is being plotted by the renderer.
  214. *
  215. * @return <code>true</code> if Area is being plotted by the renderer.
  216. */
  217. public boolean getPlotArea() {
  218. return this.plotArea;
  219. }
  220. /**
  221. * Initialises the renderer and returns a state object that should be passed to all subsequent
  222. * calls to the drawItem(...) method.
  223. *
  224. * @param g2 the graphics device.
  225. * @param dataArea the area inside the axes.
  226. * @param plot the plot.
  227. * @param data the data.
  228. * @param info an optional info collection object to return data back to the caller.
  229. *
  230. * @return A state object for use by the renderer.
  231. */
  232. public XYItemRendererState initialise(Graphics2D g2,
  233. Rectangle2D dataArea,
  234. XYPlot plot,
  235. XYDataset data,
  236. PlotRenderingInfo info) {
  237. XYAreaRendererState state = new XYAreaRendererState(info);
  238. return state;
  239. }
  240. /**
  241. * Draws the visual representation of a single data item.
  242. *
  243. * @param g2 the graphics device.
  244. * @param state the renderer state.
  245. * @param dataArea the area within which the data is being drawn.
  246. * @param info collects information about the drawing.
  247. * @param plot the plot (can be used to obtain standard color information etc).
  248. * @param domainAxis the domain axis.
  249. * @param rangeAxis the range axis.
  250. * @param dataset the dataset.
  251. * @param series the series index (zero-based).
  252. * @param item the item index (zero-based).
  253. * @param crosshairState crosshair information for the plot (<code>null</code> permitted).
  254. * @param pass the pass index.
  255. */
  256. public void drawItem(Graphics2D g2,
  257. XYItemRendererState state,
  258. Rectangle2D dataArea,
  259. PlotRenderingInfo info,
  260. XYPlot plot,
  261. ValueAxis domainAxis,
  262. ValueAxis rangeAxis,
  263. XYDataset dataset,
  264. int series,
  265. int item,
  266. CrosshairState crosshairState,
  267. int pass) {
  268. XYAreaRendererState areaState = (XYAreaRendererState) state;
  269. // get the data point...
  270. double x1 = dataset.getXValue(series, item);
  271. double y1 = dataset.getYValue(series, item);
  272. if (Double.isNaN(y1)) {
  273. y1 = 0.0;
  274. }
  275. double transX1 = domainAxis.valueToJava2D(x1, dataArea, plot.getDomainAxisEdge());
  276. double transY1 = rangeAxis.valueToJava2D(y1, dataArea, plot.getRangeAxisEdge());
  277. // get the previous point and the next point so we can calculate a "hot spot"
  278. // for the area (used by the chart entity)...
  279. int itemCount = dataset.getItemCount(series);
  280. double x0 = dataset.getXValue(series, Math.max(item - 1, 0));
  281. double y0 = dataset.getYValue(series, Math.max(item - 1, 0));
  282. if (Double.isNaN(y0)) {
  283. y0 = 0.0;
  284. }
  285. double transX0 = domainAxis.valueToJava2D(x0, dataArea, plot.getDomainAxisEdge());
  286. double transY0 = rangeAxis.valueToJava2D(y0, dataArea, plot.getRangeAxisEdge());
  287. double x2 = dataset.getXValue(series, Math.min(item + 1, itemCount - 1));
  288. double y2 = dataset.getYValue(series, Math.min(item + 1, itemCount - 1));
  289. if (Double.isNaN(y2)) {
  290. y2 = 0.0;
  291. }
  292. double transX2 = domainAxis.valueToJava2D(x2, dataArea, plot.getDomainAxisEdge());
  293. double transY2 = rangeAxis.valueToJava2D(y2, dataArea, plot.getRangeAxisEdge());
  294. double transZero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
  295. Polygon hotspot = null;
  296. if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
  297. hotspot = new Polygon();
  298. hotspot.addPoint((int) transZero, (int) ((transX0 + transX1) / 2.0));
  299. hotspot.addPoint((int) ((transY0 + transY1) / 2.0), (int) ((transX0 + transX1) / 2.0));
  300. hotspot.addPoint((int) transY1, (int) transX1);
  301. hotspot.addPoint((int) ((transY1 + transY2) / 2.0), (int) ((transX1 + transX2) / 2.0));
  302. hotspot.addPoint((int) transZero, (int) ((transX1 + transX2) / 2.0));
  303. }
  304. else { // vertical orientation
  305. hotspot = new Polygon();
  306. hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) transZero);
  307. hotspot.addPoint((int) ((transX0 + transX1) / 2.0), (int) ((transY0 + transY1) / 2.0));
  308. hotspot.addPoint((int) transX1, (int) transY1);
  309. hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) ((transY1 + transY2) / 2.0));
  310. hotspot.addPoint((int) ((transX1 + transX2) / 2.0), (int) transZero);
  311. }
  312. if (item == 0) { // create a new area polygon for the series
  313. areaState.area = new Polygon();
  314. // the first point is (x, 0)
  315. double zero = rangeAxis.valueToJava2D(0.0, dataArea, plot.getRangeAxisEdge());
  316. if (plot.getOrientation() == PlotOrientation.VERTICAL) {
  317. areaState.area.addPoint((int) transX1, (int) zero);
  318. }
  319. else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
  320. areaState.area.addPoint((int) zero, (int) transX1);
  321. }
  322. }
  323. // Add each point to Area (x, y)
  324. if (plot.getOrientation() == PlotOrientation.VERTICAL) {
  325. areaState.area.addPoint((int) transX1, (int) transY1);
  326. }
  327. else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
  328. areaState.area.addPoint((int) transY1, (int) transX1);
  329. }
  330. PlotOrientation orientation = plot.getOrientation();
  331. Paint paint = getItemPaint(series, item);
  332. Stroke stroke = getItemStroke(series, item);
  333. g2.setPaint(paint);
  334. g2.setStroke(stroke);
  335. Shape shape = null;
  336. if (getPlotShapes()) {
  337. shape = getItemShape(series, item);
  338. if (orientation == PlotOrientation.VERTICAL) {
  339. shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
  340. }
  341. else if (orientation == PlotOrientation.HORIZONTAL) {
  342. shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
  343. }
  344. g2.draw(shape);
  345. }
  346. if (getPlotLines()) {
  347. if (item > 0) {
  348. if (plot.getOrientation() == PlotOrientation.VERTICAL) {
  349. areaState.line.setLine(transX0, transY0, transX1, transY1);
  350. }
  351. else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
  352. areaState.line.setLine(transY0, transX0, transY1, transX1);
  353. }
  354. g2.draw(areaState.line);
  355. }
  356. }
  357. // Check if the item is the last item for the series.
  358. // and number of items > 0. We can't draw an area for a single point.
  359. if (getPlotArea() && item > 0 && item == (itemCount - 1)) {
  360. if (orientation == PlotOrientation.VERTICAL) {
  361. // Add the last point (x,0)
  362. areaState.area.addPoint((int) transX1, (int) transZero);
  363. }
  364. else if (orientation == PlotOrientation.HORIZONTAL) {
  365. // Add the last point (x,0)
  366. areaState.area.addPoint((int) transZero, (int) transX1);
  367. }
  368. g2.fill(areaState.area);
  369. // draw an outline around the Area.
  370. if (isOutline()) {
  371. g2.setStroke(getItemOutlineStroke(series, item));
  372. g2.setPaint(getItemOutlinePaint(series, item));
  373. g2.draw(areaState.area);
  374. }
  375. }
  376. updateCrosshairValues(crosshairState, x1, y1, transX1, transY1, orientation);
  377. // collect entity and tool tip information...
  378. if (state.getInfo() != null) {
  379. EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
  380. if (entities != null && hotspot != null) {
  381. String tip = null;
  382. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  383. if (generator != null) {
  384. tip = generator.generateToolTip(dataset, series, item);
  385. }
  386. String url = null;
  387. if (getURLGenerator() != null) {
  388. url = getURLGenerator().generateURL(dataset, series, item);
  389. }
  390. XYItemEntity entity = new XYItemEntity(hotspot, dataset, series, item, tip, url);
  391. entities.add(entity);
  392. }
  393. }
  394. }
  395. /**
  396. * Returns a clone of the renderer.
  397. *
  398. * @return A clone.
  399. *
  400. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  401. */
  402. public Object clone() throws CloneNotSupportedException {
  403. return super.clone();
  404. }
  405. }