1. /* ===========================================================
  2. * JFreeChart : a free chart library for the Java(tm) platform
  3. * ===========================================================
  4. *
  5. * (C) Copyright 2000-2004, 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. * XYStepAreaRenderer.java
  26. * -----------------------
  27. * (C) Copyright 2003, 2004, by Matthias Rose and Contributors.
  28. *
  29. * Original Author: Matthias Rose (based on XYAreaRenderer.java);
  30. * Contributor(s): David Gilbert (for Object Refinery Limited);
  31. *
  32. * $Id: XYStepAreaRenderer.java,v 1.3 2004/11/12 09:31:44 mungady Exp $
  33. *
  34. * Changes:
  35. * --------
  36. * 07-Oct-2003 : Version 1, contributed by Matthias Rose (DG);
  37. * 10-Feb-2004 : Added some getter and setter methods (DG);
  38. * 25-Feb-2004 : Replaced CrosshairInfo with CrosshairState. Renamed
  39. * XYToolTipGenerator --> XYItemLabelGenerator (DG);
  40. * 15-Jul-2004 : Switched getX() with getXValue() and getY() with getYValue() (DG);
  41. * 11-Nov-2004 : Now uses ShapeUtilities to translate shapes (DG);
  42. *
  43. */
  44. package org.jfree.chart.renderer.xy;
  45. import java.awt.Graphics2D;
  46. import java.awt.Paint;
  47. import java.awt.Polygon;
  48. import java.awt.Shape;
  49. import java.awt.Stroke;
  50. import java.awt.geom.Rectangle2D;
  51. import java.io.Serializable;
  52. import org.jfree.chart.axis.ValueAxis;
  53. import org.jfree.chart.entity.EntityCollection;
  54. import org.jfree.chart.entity.XYItemEntity;
  55. import org.jfree.chart.event.RendererChangeEvent;
  56. import org.jfree.chart.labels.XYToolTipGenerator;
  57. import org.jfree.chart.plot.CrosshairState;
  58. import org.jfree.chart.plot.PlotOrientation;
  59. import org.jfree.chart.plot.PlotRenderingInfo;
  60. import org.jfree.chart.plot.XYPlot;
  61. import org.jfree.chart.urls.XYURLGenerator;
  62. import org.jfree.data.xy.XYDataset;
  63. import org.jfree.util.PublicCloneable;
  64. import org.jfree.util.ShapeUtilities;
  65. /**
  66. * A step chart renderer that fills the area between the step and the x-axis.
  67. */
  68. public class XYStepAreaRenderer extends AbstractXYItemRenderer
  69. implements XYItemRenderer,
  70. Cloneable,
  71. PublicCloneable,
  72. Serializable {
  73. /** Useful constant for specifying the type of rendering (shapes only). */
  74. public static final int SHAPES = 1;
  75. /** Useful constant for specifying the type of rendering (area only). */
  76. public static final int AREA = 2;
  77. /** Useful constant for specifying the type of rendering (area and shapes). */
  78. public static final int AREA_AND_SHAPES = 3;
  79. /** A flag indicating whether or not shapes are drawn at each XY point. */
  80. private boolean plotShapes;
  81. /** A flag that controls whether or not shapes are filled for ALL series. */
  82. private boolean shapesFilled;
  83. /** A flag indicating whether or not Area are drawn at each XY point. */
  84. private boolean plotArea;
  85. /** A flag that controls whether or not the outline is shown. */
  86. private boolean showOutline;
  87. /** Area of the complete series */
  88. protected transient Polygon pArea = null;
  89. /** The value on the range axis which defines the 'lower' border of the area. */
  90. private double rangeBase;
  91. /**
  92. * Constructs a new renderer.
  93. */
  94. public XYStepAreaRenderer() {
  95. this(AREA);
  96. }
  97. /**
  98. * Constructs a new renderer.
  99. *
  100. * @param type the type of the renderer.
  101. */
  102. public XYStepAreaRenderer(int type) {
  103. this(type, null, null);
  104. }
  105. /**
  106. * Constructs a new renderer.
  107. * <p>
  108. * To specify the type of renderer, use one of the constants:
  109. * AREA, SHAPES or AREA_AND_SHAPES.
  110. *
  111. * @param type the type of renderer.
  112. * @param toolTipGenerator the tool tip generator to use (<code>null</code> permitted).
  113. * @param urlGenerator the URL generator (<code>null</code> permitted).
  114. */
  115. public XYStepAreaRenderer(int type,
  116. XYToolTipGenerator toolTipGenerator, XYURLGenerator urlGenerator) {
  117. super();
  118. setToolTipGenerator(toolTipGenerator);
  119. setURLGenerator(urlGenerator);
  120. if (type == AREA) {
  121. this.plotArea = true;
  122. }
  123. else if (type == SHAPES) {
  124. this.plotShapes = true;
  125. }
  126. else if (type == AREA_AND_SHAPES) {
  127. this.plotArea = true;
  128. this.plotShapes = true;
  129. }
  130. this.showOutline = false;
  131. }
  132. /**
  133. * Returns a flag that controls whether or not outlines of the areas are drawn.
  134. *
  135. * @return the flag.
  136. */
  137. public boolean isOutline() {
  138. return this.showOutline;
  139. }
  140. /**
  141. * Sets a flag that controls whether or not outlines of the areas are drawn, and sends a
  142. * {@link RendererChangeEvent} to all registered listeners.
  143. *
  144. * @param show the flag.
  145. */
  146. public void setOutline(boolean show) {
  147. this.showOutline = show;
  148. notifyListeners(new RendererChangeEvent(this));
  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. * Sets the flag that controls whether or not shapes are displayed for each data item.
  160. *
  161. * @param flag the flag.
  162. */
  163. public void setPlotShapes(boolean flag) {
  164. this.plotShapes = flag;
  165. notifyListeners(new RendererChangeEvent(this));
  166. }
  167. /**
  168. * Returns the flag that controls whether or not the shapes are filled.
  169. *
  170. * @return a boolean.
  171. */
  172. public boolean isShapesFilled() {
  173. return this.shapesFilled;
  174. }
  175. /**
  176. * Sets the 'shapes filled' for ALL series.
  177. *
  178. * @param filled the flag.
  179. */
  180. public void setShapesFilled(boolean filled) {
  181. this.shapesFilled = filled;
  182. notifyListeners(new RendererChangeEvent(this));
  183. }
  184. /**
  185. * Returns true if Area is being plotted by the renderer.
  186. *
  187. * @return <code>true</code> if Area is being plotted by the renderer.
  188. */
  189. public boolean getPlotArea() {
  190. return this.plotArea;
  191. }
  192. /**
  193. * Sets a flag that controls whether or not areas are drawn for each data item.
  194. *
  195. * @param flag the flag.
  196. */
  197. public void setPlotArea(boolean flag) {
  198. this.plotArea = flag;
  199. notifyListeners(new RendererChangeEvent(this));
  200. }
  201. /**
  202. * Returns the value on the range axis which defines the 'lower' border of the area.
  203. *
  204. * @return <code>double</code> the value on the range axis which defines the 'lower' border of
  205. * the area.
  206. */
  207. public double getRangeBase() {
  208. return this.rangeBase;
  209. }
  210. /**
  211. * Sets the value on the range axis which defines the default border of the area.
  212. * E.g. setRangeBase(Double.NEGATIVE_INFINITY) lets areas always reach the lower border of the
  213. * plotArea.
  214. *
  215. * @param val the value on the range axis which defines the default border of the area.
  216. */
  217. public void setRangeBase(double val) {
  218. this.rangeBase = val;
  219. notifyListeners(new RendererChangeEvent(this));
  220. }
  221. /**
  222. * Initialises the renderer. Here we calculate the Java2D y-coordinate for
  223. * zero, since all the bars have their bases fixed at zero.
  224. *
  225. * @param g2 the graphics device.
  226. * @param dataArea the area inside the axes.
  227. * @param plot the plot.
  228. * @param data the data.
  229. * @param info an optional info collection object to return data back to the caller.
  230. *
  231. * @return The number of passes required by the renderer.
  232. */
  233. public XYItemRendererState initialise(Graphics2D g2,
  234. Rectangle2D dataArea,
  235. XYPlot plot,
  236. XYDataset data,
  237. PlotRenderingInfo info) {
  238. return super.initialise(g2, dataArea, plot, data, info);
  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. PlotOrientation orientation = plot.getOrientation();
  269. // Get the item count for the series, so that we can know which is the end of the series.
  270. int itemCount = dataset.getItemCount(series);
  271. Paint paint = getItemPaint(series, item);
  272. Stroke seriesStroke = getItemStroke(series, item);
  273. g2.setPaint(paint);
  274. g2.setStroke(seriesStroke);
  275. // get the data point...
  276. Number x1 = dataset.getX(series, item);
  277. Number y1 = dataset.getY(series, item);
  278. double x = x1.doubleValue();
  279. double y = y1 == null ? getRangeBase() : y1.doubleValue();
  280. double transX1 = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
  281. double transY1 = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());
  282. // avoid possible sun.dc.pr.PRException: endPath: bad path
  283. transY1 = restrictValueToDataArea(transY1, plot, dataArea);
  284. if (this.pArea == null && y1 != null) {
  285. // Create a new Area for the series
  286. this.pArea = new Polygon();
  287. // start from Y = rangeBase
  288. double transY2 = rangeAxis.valueToJava2D(
  289. getRangeBase(), dataArea, plot.getRangeAxisEdge()
  290. );
  291. // avoid possible sun.dc.pr.PRException: endPath: bad path
  292. transY2 = restrictValueToDataArea(transY2, plot, dataArea);
  293. // The first point is (x, this.baseYValue)
  294. if (orientation == PlotOrientation.VERTICAL) {
  295. this.pArea.addPoint((int) transX1, (int) transY2);
  296. }
  297. else if (orientation == PlotOrientation.HORIZONTAL) {
  298. this.pArea.addPoint((int) transY2, (int) transX1);
  299. }
  300. }
  301. double transX0 = 0;
  302. double transY0 = restrictValueToDataArea(getRangeBase(), plot, dataArea);
  303. Number x0 = null;
  304. Number y0 = null;
  305. if (item > 0) {
  306. // get the previous data point...
  307. x0 = dataset.getX(series, item - 1);
  308. y0 = y1 == null ? null : dataset.getY(series, item - 1);
  309. x = x0.doubleValue();
  310. y = y0 == null ? getRangeBase() : y0.doubleValue();
  311. transX0 = domainAxis.valueToJava2D(x, dataArea, plot.getDomainAxisEdge());
  312. transY0 = rangeAxis.valueToJava2D(y, dataArea, plot.getRangeAxisEdge());
  313. // avoid possible sun.dc.pr.PRException: endPath: bad path
  314. transY0 = restrictValueToDataArea(transY0, plot, dataArea);
  315. if (y1 == null) {
  316. // NULL value -> insert point on base line
  317. // instead of 'step point'
  318. transX1 = transX0;
  319. transY0 = transY1;
  320. }
  321. if (transY0 != transY1) {
  322. // not just a horizontal bar but need to perform a 'step'.
  323. if (orientation == PlotOrientation.VERTICAL) {
  324. this.pArea.addPoint((int) transX1, (int) transY0);
  325. }
  326. else if (orientation == PlotOrientation.HORIZONTAL) {
  327. this.pArea.addPoint((int) transY0, (int) transX1);
  328. }
  329. }
  330. }
  331. Shape shape = null;
  332. if (y1 != null) {
  333. // Add each point to Area (x, y)
  334. if (orientation == PlotOrientation.VERTICAL) {
  335. this.pArea.addPoint((int) transX1, (int) transY1);
  336. }
  337. else if (orientation == PlotOrientation.HORIZONTAL) {
  338. this.pArea.addPoint((int) transY1, (int) transX1);
  339. }
  340. if (getPlotShapes()) {
  341. shape = getItemShape(series, item);
  342. if (orientation == PlotOrientation.VERTICAL) {
  343. shape = ShapeUtilities.createTranslatedShape(shape, transX1, transY1);
  344. }
  345. else if (orientation == PlotOrientation.HORIZONTAL) {
  346. shape = ShapeUtilities.createTranslatedShape(shape, transY1, transX1);
  347. }
  348. if (isShapesFilled()) {
  349. g2.fill(shape);
  350. }
  351. else {
  352. g2.draw(shape);
  353. }
  354. }
  355. else {
  356. if (orientation == PlotOrientation.VERTICAL) {
  357. shape = new Rectangle2D.Double(transX1 - 2, transY1 - 2, 4.0, 4.0);
  358. }
  359. else if (orientation == PlotOrientation.HORIZONTAL) {
  360. shape = new Rectangle2D.Double(transY1 - 2, transX1 - 2, 4.0, 4.0);
  361. }
  362. }
  363. }
  364. // Check if the item is the last item for the series or if it
  365. // is a NULL value and number of items > 0. We can't draw an area for a single point.
  366. if (getPlotArea() && item > 0 && this.pArea != null
  367. && (item == (itemCount - 1) || y1 == null)) {
  368. double transY2 = rangeAxis.valueToJava2D(
  369. getRangeBase(), dataArea, plot.getRangeAxisEdge()
  370. );
  371. // avoid possible sun.dc.pr.PRException: endPath: bad path
  372. transY2 = restrictValueToDataArea(transY2, plot, dataArea);
  373. if (orientation == PlotOrientation.VERTICAL) {
  374. // Add the last point (x,0)
  375. this.pArea.addPoint((int) transX1, (int) transY2);
  376. }
  377. else if (orientation == PlotOrientation.HORIZONTAL) {
  378. // Add the last point (x,0)
  379. this.pArea.addPoint((int) transY2, (int) transX1);
  380. }
  381. // fill the polygon
  382. g2.fill(this.pArea);
  383. // draw an outline around the Area.
  384. if (isOutline()) {
  385. g2.setStroke(plot.getOutlineStroke());
  386. g2.setPaint(plot.getOutlinePaint());
  387. g2.draw(this.pArea);
  388. }
  389. // start new area when needed (see above)
  390. this.pArea = null;
  391. }
  392. // do we need to update the crosshair values?
  393. if (y1 != null) {
  394. updateCrosshairValues(
  395. crosshairState, x1.doubleValue(), y1.doubleValue(), transX1, transY1, orientation
  396. );
  397. }
  398. // collect entity and tool tip information...
  399. if (state.getInfo() != null) {
  400. EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
  401. if (entities != null && shape != null) {
  402. String tip = null;
  403. XYToolTipGenerator generator = getToolTipGenerator(series, item);
  404. if (generator != null) {
  405. tip = generator.generateToolTip(dataset, series, item);
  406. }
  407. String url = null;
  408. if (getURLGenerator() != null) {
  409. url = getURLGenerator().generateURL(dataset, series, item);
  410. }
  411. XYItemEntity entity = new XYItemEntity(shape, dataset, series, item, tip, url);
  412. entities.add(entity);
  413. }
  414. }
  415. }
  416. /**
  417. * Returns a clone of the renderer.
  418. *
  419. * @return A clone.
  420. *
  421. * @throws CloneNotSupportedException if the renderer cannot be cloned.
  422. */
  423. public Object clone() throws CloneNotSupportedException {
  424. return super.clone();
  425. }
  426. /**
  427. * Helper method which returns a value if it lies
  428. * inside the visible dataArea and otherwise the corresponding
  429. * coordinate on the border of the dataArea. The PlotOrientation
  430. * is taken into account.
  431. * Useful to avoid possible sun.dc.pr.PRException: endPath: bad path
  432. * which occurs when trying to draw lines/shapes which in large part
  433. * lie outside of the visible dataArea.
  434. *
  435. * @param value the value which shall be
  436. * @param dataArea the area within which the data is being drawn.
  437. * @param plot the plot (can be used to obtain standard color information etc).
  438. * @return <code>double</code> value inside the data area.
  439. */
  440. protected static double restrictValueToDataArea(double value,
  441. XYPlot plot,
  442. Rectangle2D dataArea) {
  443. double min = 0;
  444. double max = 0;
  445. if (plot.getOrientation() == PlotOrientation.VERTICAL) {
  446. min = dataArea.getMinY();
  447. max = dataArea.getMaxY();
  448. }
  449. else if (plot.getOrientation() == PlotOrientation.HORIZONTAL) {
  450. min = dataArea.getMinX();
  451. max = dataArea.getMaxX();
  452. }
  453. if (value < min) {
  454. value = min;
  455. }
  456. else if (value > max) {
  457. value = max;
  458. }
  459. return value;
  460. }
  461. }