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