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. * BarRenderer.java
  26. * ----------------
  27. * (C) Copyright 2002-2005, by Object Refinery Limited.
  28. *
  29. * Original Author: David Gilbert (for Object Refinery Limited);
  30. * Contributor(s): Christian W. Zuckschwerdt;
  31. *
  32. * $Id: BarRenderer.java,v 1.7 2005/02/23 17:58:07 mungady Exp $
  33. *
  34. * Changes
  35. * -------
  36. * 14-Mar-2002 : Version 1 (DG);
  37. * 23-May-2002 : Added tooltip generator to renderer (DG);
  38. * 29-May-2002 : Moved tooltip generator to abstract super-class (DG);
  39. * 25-Jun-2002 : Changed constructor to protected and removed redundant code (DG);
  40. * 26-Jun-2002 : Added axis to initialise method, and record upper and lower clip values (DG);
  41. * 24-Sep-2002 : Added getLegendItem() method (DG);
  42. * 09-Oct-2002 : Modified constructor to include URL generator (DG);
  43. * 05-Nov-2002 : Base dataset is now TableDataset not CategoryDataset (DG);
  44. * 10-Jan-2003 : Moved get/setItemMargin() method up from subclasses (DG);
  45. * 17-Jan-2003 : Moved plot classes into a separate package (DG);
  46. * 25-Mar-2003 : Implemented Serializable (DG);
  47. * 01-May-2003 : Modified clipping to allow for dual axes and datasets (DG);
  48. * 12-May-2003 : Merged horizontal and vertical bar renderers (DG);
  49. * 12-Jun-2003 : Updates for item labels (DG);
  50. * 30-Jul-2003 : Modified entity constructor (CZ);
  51. * 02-Sep-2003 : Changed initialise method to fix bug 790407 (DG);
  52. * 16-Sep-2003 : Changed ChartRenderingInfo --> PlotRenderingInfo (DG);
  53. * 07-Oct-2003 : Added renderer state (DG);
  54. * 27-Oct-2003 : Merged drawHorizontalItem() and drawVerticalItem(...) methods (DG);
  55. * 28-Oct-2003 : Added support for gradient paint on bars (DG);
  56. * 14-Nov-2003 : Added 'maxBarWidth' attribute (DG);
  57. * 10-Feb-2004 : Small changes inside drawItem() method to ease cut-and-paste overriding (DG);
  58. * 19-Mar-2004 : Fixed bug introduced with separation of tool tip and item label generators.
  59. * Fixed equals() method (DG);
  60. * 11-May-2004 : Fix for null pointer exception (bug id 951127) (DG);
  61. * 05-Nov-2004 : Modified drawItem() signature (DG);
  62. * 26-Jan-2005 : Provided override for getLegendItem() method (DG);
  63. *
  64. */
  65. package org.jfree.chart.renderer.category;
  66. import java.awt.Font;
  67. import java.awt.GradientPaint;
  68. import java.awt.Graphics2D;
  69. import java.awt.Paint;
  70. import java.awt.Shape;
  71. import java.awt.Stroke;
  72. import java.awt.geom.Point2D;
  73. import java.awt.geom.Rectangle2D;
  74. import java.io.Serializable;
  75. import org.jfree.chart.LegendItem;
  76. import org.jfree.chart.axis.CategoryAxis;
  77. import org.jfree.chart.axis.ValueAxis;
  78. import org.jfree.chart.entity.CategoryItemEntity;
  79. import org.jfree.chart.entity.EntityCollection;
  80. import org.jfree.chart.event.RendererChangeEvent;
  81. import org.jfree.chart.labels.CategoryLabelGenerator;
  82. import org.jfree.chart.labels.CategoryToolTipGenerator;
  83. import org.jfree.chart.labels.ItemLabelAnchor;
  84. import org.jfree.chart.labels.ItemLabelPosition;
  85. import org.jfree.chart.plot.CategoryPlot;
  86. import org.jfree.chart.plot.PlotOrientation;
  87. import org.jfree.chart.plot.PlotRenderingInfo;
  88. import org.jfree.data.category.CategoryDataset;
  89. import org.jfree.text.TextUtilities;
  90. import org.jfree.ui.GradientPaintTransformer;
  91. import org.jfree.ui.RectangleEdge;
  92. import org.jfree.ui.StandardGradientPaintTransformer;
  93. import org.jfree.util.ObjectUtilities;
  94. import org.jfree.util.PublicCloneable;
  95. /**
  96. * A {@link CategoryItemRenderer} that draws individual data items as bars.
  97. */
  98. public class BarRenderer extends AbstractCategoryItemRenderer
  99. implements Cloneable, PublicCloneable, Serializable {
  100. /** The default item margin percentage. */
  101. public static final double DEFAULT_ITEM_MARGIN = 0.20;
  102. /** Constant that controls the minimum width before a bar has an outline drawn. */
  103. public static final double BAR_OUTLINE_WIDTH_THRESHOLD = 3.0;
  104. /** The margin between items (bars) within a category. */
  105. private double itemMargin;
  106. /** A flag that controls whether or not bar outlines are drawn. */
  107. private boolean drawBarOutline;
  108. /** The maximum bar width as a percentage of the available space. */
  109. private double maxBarWidth;
  110. /** The minimum bar length (in Java2D units). */
  111. private double minimumBarLength;
  112. /** An optional class used to transform gradient paint objects to fit each bar. */
  113. private GradientPaintTransformer gradientPaintTransformer;
  114. /** The fallback position if a positive item label doesn't fit inside the bar. */
  115. private ItemLabelPosition positiveItemLabelPositionFallback;
  116. /** The fallback position if a negative item label doesn't fit inside the bar. */
  117. private ItemLabelPosition negativeItemLabelPositionFallback;
  118. /** The upper clip (axis) value for the axis. */
  119. private double upperClip; // TODO: this needs to move into the renderer state
  120. /** The lower clip (axis) value for the axis. */
  121. private double lowerClip; // TODO: this needs to move into the renderer state
  122. /**
  123. * Creates a new bar renderer with default settings.
  124. */
  125. public BarRenderer() {
  126. super();
  127. this.itemMargin = DEFAULT_ITEM_MARGIN;
  128. this.drawBarOutline = true;
  129. this.maxBarWidth = 1.0; // 100 percent, so it will not apply unless changed
  130. this.positiveItemLabelPositionFallback = null;
  131. this.negativeItemLabelPositionFallback = null;
  132. this.gradientPaintTransformer = new StandardGradientPaintTransformer();
  133. this.minimumBarLength = 0.0;
  134. }
  135. /**
  136. * Returns the item margin as a percentage of the available space for all bars.
  137. *
  138. * @return The margin percentage (where 0.10 is ten percent).
  139. */
  140. public double getItemMargin() {
  141. return this.itemMargin;
  142. }
  143. /**
  144. * Sets the item margin and sends a {@link RendererChangeEvent} to all registered listeners.
  145. * The value is expressed as a percentage of the available width for plotting all the bars,
  146. * with the resulting amount to be distributed between all the bars evenly.
  147. *
  148. * @param percent the margin (where 0.10 is ten percent).
  149. */
  150. public void setItemMargin(double percent) {
  151. this.itemMargin = percent;
  152. notifyListeners(new RendererChangeEvent(this));
  153. }
  154. /**
  155. * Returns a flag that controls whether or not bar outlines are drawn.
  156. *
  157. * @return A boolean.
  158. */
  159. public boolean isDrawBarOutline() {
  160. return this.drawBarOutline;
  161. }
  162. /**
  163. * Sets the flag that controls whether or not bar outlines are drawn and sends a
  164. * {@link RendererChangeEvent} to all registered listeners.
  165. *
  166. * @param draw the flag.
  167. */
  168. public void setDrawBarOutline(boolean draw) {
  169. this.drawBarOutline = draw;
  170. notifyListeners(new RendererChangeEvent(this));
  171. }
  172. /**
  173. * Returns the maximum bar width, as a percentage of the available drawing space.
  174. *
  175. * @return The maximum bar width.
  176. */
  177. public double getMaxBarWidth() {
  178. return this.maxBarWidth;
  179. }
  180. /**
  181. * Sets the maximum bar width, which is specified as a percentage of the available space
  182. * for all bars, and sends a {@link RendererChangeEvent} to all registered listeners.
  183. *
  184. * @param percent the percent (where 0.05 is five percent).
  185. */
  186. public void setMaxBarWidth(double percent) {
  187. this.maxBarWidth = percent;
  188. notifyListeners(new RendererChangeEvent(this));
  189. }
  190. /**
  191. * Returns the minimum bar length (in Java2D units).
  192. *
  193. * @return The minimum bar length.
  194. */
  195. public double getMinimumBarLength() {
  196. return this.minimumBarLength;
  197. }
  198. /**
  199. * Sets the minimum bar length and sends a {@link RendererChangeEvent} to all registered
  200. * listeners. The minimum bar length is specified in Java2D units, and can be used to prevent
  201. * bars that represent very small data values from disappearing when drawn on the screen.
  202. *
  203. * @param min the minimum bar length (in Java2D units).
  204. */
  205. public void setMinimumBarLength(double min) {
  206. this.minimumBarLength = min;
  207. notifyListeners(new RendererChangeEvent(this));
  208. }
  209. /**
  210. * Returns the gradient paint transformer (an object used to transform gradient paint objects
  211. * to fit each bar.
  212. *
  213. * @return A transformer (<code>null</code> possible).
  214. */
  215. public GradientPaintTransformer getGradientPaintTransformer() {
  216. return this.gradientPaintTransformer;
  217. }
  218. /**
  219. * Sets the gradient paint transformer and sends a {@link RendererChangeEvent} to all registered
  220. * listeners.
  221. *
  222. * @param transformer the transformer (<code>null</code> permitted).
  223. */
  224. public void setGradientPaintTransformer(GradientPaintTransformer transformer) {
  225. this.gradientPaintTransformer = transformer;
  226. notifyListeners(new RendererChangeEvent(this));
  227. }
  228. /**
  229. * Returns the fallback position for positive item labels that don't fit within a bar.
  230. *
  231. * @return The fallback position (<code>null</code> possible).
  232. */
  233. public ItemLabelPosition getPositiveItemLabelPositionFallback() {
  234. return this.positiveItemLabelPositionFallback;
  235. }
  236. /**
  237. * Sets the fallback position for positive item labels that don't fit within a bar, and sends
  238. * a {@link RendererChangeEvent} to all registered listeners.
  239. *
  240. * @param position the position (<code>null</code> permitted).
  241. */
  242. public void setPositiveItemLabelPositionFallback(ItemLabelPosition position) {
  243. this.positiveItemLabelPositionFallback = position;
  244. notifyListeners(new RendererChangeEvent(this));
  245. }
  246. /**
  247. * Returns the fallback position for negative item labels that don't fit within a bar.
  248. *
  249. * @return The fallback position (<code>null</code> possible).
  250. */
  251. public ItemLabelPosition getNegativeItemLabelPositionFallback() {
  252. return this.negativeItemLabelPositionFallback;
  253. }
  254. /**
  255. * Sets the fallback position for negative item labels that don't fit within a bar, and sends
  256. * a {@link RendererChangeEvent} to all registered listeners.
  257. *
  258. * @param position the position (<code>null</code> permitted).
  259. */
  260. public void setNegativeItemLabelPositionFallback(ItemLabelPosition position) {
  261. this.negativeItemLabelPositionFallback = position;
  262. notifyListeners(new RendererChangeEvent(this));
  263. }
  264. /**
  265. * Returns the lower clip value. This value is recalculated in the initialise() method.
  266. *
  267. * @return The value.
  268. */
  269. public double getLowerClip() {
  270. // TODO: this attribute should be transferred to the renderer state.
  271. return this.lowerClip;
  272. }
  273. /**
  274. * Returns the upper clip value. This value is recalculated in the initialise() method.
  275. *
  276. * @return The value.
  277. */
  278. public double getUpperClip() {
  279. // TODO: this attribute should be transferred to the renderer state.
  280. return this.upperClip;
  281. }
  282. /**
  283. * Initialises the renderer and returns a state object that will be passed to subsequent calls
  284. * to the drawItem method. This method gets called once at the start of the process of
  285. * drawing a chart.
  286. *
  287. * @param g2 the graphics device.
  288. * @param dataArea the area in which the data is to be plotted.
  289. * @param plot the plot.
  290. * @param rendererIndex the renderer index.
  291. * @param info collects chart rendering information for return to caller.
  292. *
  293. * @return The renderer state.
  294. */
  295. public CategoryItemRendererState initialise(Graphics2D g2,
  296. Rectangle2D dataArea,
  297. CategoryPlot plot,
  298. int rendererIndex,
  299. PlotRenderingInfo info) {
  300. CategoryItemRendererState state = super.initialise(g2, dataArea, plot, rendererIndex, info);
  301. // get the clipping values...
  302. ValueAxis rangeAxis = getRangeAxis(plot, rendererIndex);
  303. this.lowerClip = rangeAxis.getRange().getLowerBound();
  304. this.upperClip = rangeAxis.getRange().getUpperBound();
  305. // calculate the bar width
  306. calculateBarWidth(plot, dataArea, rendererIndex, state);
  307. return state;
  308. }
  309. /**
  310. * Calculates the bar width and stores it in the renderer state.
  311. *
  312. * @param plot the plot.
  313. * @param dataArea the data area.
  314. * @param rendererIndex the renderer index.
  315. * @param state the renderer state.
  316. */
  317. protected void calculateBarWidth(CategoryPlot plot,
  318. Rectangle2D dataArea,
  319. int rendererIndex,
  320. CategoryItemRendererState state) {
  321. CategoryAxis domainAxis = getDomainAxis(plot, rendererIndex);
  322. CategoryDataset dataset = plot.getDataset(rendererIndex);
  323. if (dataset != null) {
  324. int columns = dataset.getColumnCount();
  325. int rows = dataset.getRowCount();
  326. double space = 0.0;
  327. PlotOrientation orientation = plot.getOrientation();
  328. if (orientation == PlotOrientation.HORIZONTAL) {
  329. space = dataArea.getHeight();
  330. }
  331. else if (orientation == PlotOrientation.VERTICAL) {
  332. space = dataArea.getWidth();
  333. }
  334. double maxWidth = space * getMaxBarWidth();
  335. double categoryMargin = 0.0;
  336. double currentItemMargin = 0.0;
  337. if (columns > 1) {
  338. categoryMargin = domainAxis.getCategoryMargin();
  339. }
  340. if (rows > 1) {
  341. currentItemMargin = getItemMargin();
  342. }
  343. double used = space * (1 - domainAxis.getLowerMargin() - domainAxis.getUpperMargin()
  344. - categoryMargin - currentItemMargin);
  345. if ((rows * columns) > 0) {
  346. state.setBarWidth(Math.min(used / (rows * columns), maxWidth));
  347. }
  348. else {
  349. state.setBarWidth(Math.min(used, maxWidth));
  350. }
  351. }
  352. }
  353. /**
  354. * Calculates the coordinate of the first "side" of a bar. This will be the minimum
  355. * x-coordinate for a vertical bar, and the minimum y-coordinate for a horizontal bar.
  356. *
  357. * @param plot the plot.
  358. * @param orientation the plot orientation.
  359. * @param dataArea the data area.
  360. * @param domainAxis the domain axis.
  361. * @param state the renderer state (has the bar width precalculated).
  362. * @param row the row index.
  363. * @param column the column index.
  364. *
  365. * @return the coordinate.
  366. */
  367. protected double calculateBarW0(CategoryPlot plot,
  368. PlotOrientation orientation,
  369. Rectangle2D dataArea,
  370. CategoryAxis domainAxis,
  371. CategoryItemRendererState state,
  372. int row,
  373. int column) {
  374. // calculate bar width...
  375. double space = 0.0;
  376. if (orientation == PlotOrientation.HORIZONTAL) {
  377. space = dataArea.getHeight();
  378. }
  379. else {
  380. space = dataArea.getWidth();
  381. }
  382. double barW0 = domainAxis.getCategoryStart(
  383. column, getColumnCount(), dataArea, plot.getDomainAxisEdge()
  384. );
  385. int seriesCount = getRowCount();
  386. int categoryCount = getColumnCount();
  387. if (seriesCount > 1) {
  388. double seriesGap = space * getItemMargin() / (categoryCount * (seriesCount - 1));
  389. double seriesW = calculateSeriesWidth(space, domainAxis, categoryCount, seriesCount);
  390. barW0 = barW0 + row * (seriesW + seriesGap)
  391. + (seriesW / 2.0) - (state.getBarWidth() / 2.0);
  392. }
  393. else {
  394. barW0 = domainAxis.getCategoryMiddle(
  395. column, getColumnCount(), dataArea, plot.getDomainAxisEdge()
  396. ) - state.getBarWidth() / 2.0;
  397. }
  398. return barW0;
  399. }
  400. /**
  401. * Calculates the coordinates for the length of a single bar.
  402. *
  403. * @param value the value represented by the bar.
  404. *
  405. * @return the coordinates for each end of the bar (or <code>null</code> if the bar is not
  406. * visible for the current axis range).
  407. */
  408. protected double[] calculateBarL0L1(double value) {
  409. double base = 0.0;
  410. double lclip = getLowerClip();
  411. double uclip = getUpperClip();
  412. if (uclip <= 0.0) { // cases 1, 2, 3 and 4
  413. if (value >= uclip) {
  414. return null; // bar is not visible
  415. }
  416. base = uclip;
  417. if (value <= lclip) {
  418. value = lclip;
  419. }
  420. }
  421. else if (lclip <= 0.0) { // cases 5, 6, 7 and 8
  422. if (value >= uclip) {
  423. value = uclip;
  424. }
  425. else {
  426. if (value <= lclip) {
  427. value = lclip;
  428. }
  429. }
  430. }
  431. else { // cases 9, 10, 11 and 12
  432. if (value <= lclip) {
  433. return null; // bar is not visible
  434. }
  435. base = lclip;
  436. if (value >= uclip) {
  437. value = uclip;
  438. }
  439. }
  440. return new double[] {base, value};
  441. }
  442. /**
  443. * Returns a legend item for a series.
  444. *
  445. * @param datasetIndex the dataset index (zero-based).
  446. * @param series the series index (zero-based).
  447. *
  448. * @return The legend item.
  449. */
  450. public LegendItem getLegendItem(int datasetIndex, int series) {
  451. CategoryPlot cp = getPlot();
  452. if (cp == null) {
  453. return null;
  454. }
  455. CategoryDataset dataset;
  456. dataset = cp.getDataset(datasetIndex);
  457. String label = dataset.getRowKey(series).toString();
  458. String description = label;
  459. Shape shape = new Rectangle2D.Double(-4.0, -4.0, 8.0, 8.0);
  460. Paint paint = getSeriesPaint(series);
  461. Paint outlinePaint = getSeriesOutlinePaint(series);
  462. Stroke outlineStroke = getSeriesOutlineStroke(series);
  463. return new LegendItem(
  464. label, description, shape, paint, outlineStroke, outlinePaint
  465. );
  466. }
  467. /**
  468. * Draws the bar for a single (series, category) data item.
  469. *
  470. * @param g2 the graphics device.
  471. * @param state the renderer state.
  472. * @param dataArea the data area.
  473. * @param plot the plot.
  474. * @param domainAxis the domain axis.
  475. * @param rangeAxis the range axis.
  476. * @param dataset the dataset.
  477. * @param row the row index (zero-based).
  478. * @param column the column index (zero-based).
  479. * @param pass the pass index.
  480. */
  481. public void drawItem(Graphics2D g2,
  482. CategoryItemRendererState state,
  483. Rectangle2D dataArea,
  484. CategoryPlot plot,
  485. CategoryAxis domainAxis,
  486. ValueAxis rangeAxis,
  487. CategoryDataset dataset,
  488. int row,
  489. int column,
  490. int pass) {
  491. // nothing is drawn for null values...
  492. Number dataValue = dataset.getValue(row, column);
  493. if (dataValue == null) {
  494. return;
  495. }
  496. double value = dataValue.doubleValue();
  497. PlotOrientation orientation = plot.getOrientation();
  498. double barW0 = calculateBarW0(plot, orientation, dataArea, domainAxis, state, row, column);
  499. double[] barL0L1 = calculateBarL0L1(value);
  500. if (barL0L1 == null) {
  501. return; // the bar is not visible
  502. }
  503. RectangleEdge edge = plot.getRangeAxisEdge();
  504. double transL0 = rangeAxis.valueToJava2D(barL0L1[0], dataArea, edge);
  505. double transL1 = rangeAxis.valueToJava2D(barL0L1[1], dataArea, edge);
  506. double barL0 = Math.min(transL0, transL1);
  507. double barLength = Math.max(Math.abs(transL1 - transL0), getMinimumBarLength());
  508. // draw the bar...
  509. Rectangle2D bar = null;
  510. if (orientation == PlotOrientation.HORIZONTAL) {
  511. bar = new Rectangle2D.Double(barL0, barW0, barLength, state.getBarWidth());
  512. }
  513. else {
  514. bar = new Rectangle2D.Double(barW0, barL0, state.getBarWidth(), barLength);
  515. }
  516. Paint itemPaint = getItemPaint(row, column);
  517. if (getGradientPaintTransformer() != null && itemPaint instanceof GradientPaint) {
  518. GradientPaint gp = (GradientPaint) itemPaint;
  519. itemPaint = getGradientPaintTransformer().transform(gp, bar);
  520. }
  521. g2.setPaint(itemPaint);
  522. g2.fill(bar);
  523. // draw the outline...
  524. if (isDrawBarOutline() && state.getBarWidth() > BAR_OUTLINE_WIDTH_THRESHOLD) {
  525. Stroke stroke = getItemOutlineStroke(row, column);
  526. Paint paint = getItemOutlinePaint(row, column);
  527. if (stroke != null && paint != null) {
  528. g2.setStroke(stroke);
  529. g2.setPaint(paint);
  530. g2.draw(bar);
  531. }
  532. }
  533. CategoryLabelGenerator generator = getLabelGenerator(row, column);
  534. if (generator != null && isItemLabelVisible(row, column)) {
  535. drawItemLabel(g2, dataset, row, column, plot, generator, bar, (value < 0.0));
  536. }
  537. // collect entity and tool tip information...
  538. if (state.getInfo() != null) {
  539. EntityCollection entities = state.getInfo().getOwner().getEntityCollection();
  540. if (entities != null) {
  541. String tip = null;
  542. CategoryToolTipGenerator tipster = getToolTipGenerator(row, column);
  543. if (tipster != null) {
  544. tip = tipster.generateToolTip(dataset, row, column);
  545. }
  546. String url = null;
  547. if (getItemURLGenerator(row, column) != null) {
  548. url = getItemURLGenerator(row, column).generateURL(dataset, row, column);
  549. }
  550. CategoryItemEntity entity = new CategoryItemEntity(
  551. bar, tip, url, dataset, row, dataset.getColumnKey(column), column
  552. );
  553. entities.add(entity);
  554. }
  555. }
  556. }
  557. /**
  558. * Calculates the available space for each series.
  559. *
  560. * @param space the space along the entire axis (in Java2D units).
  561. * @param axis the category axis.
  562. * @param categories the number of categories.
  563. * @param series the number of series.
  564. *
  565. * @return the width of one series.
  566. */
  567. protected double calculateSeriesWidth(double space, CategoryAxis axis,
  568. int categories, int series) {
  569. double factor = 1.0 - getItemMargin() - axis.getLowerMargin() - axis.getUpperMargin();
  570. if (categories > 1) {
  571. factor = factor - axis.getCategoryMargin();
  572. }
  573. return (space * factor) / (categories * series);
  574. }
  575. /**
  576. * Draws an item label. This method is overridden so that the bar can be used
  577. * to calculate the label anchor point.
  578. *
  579. * @param g2 the graphics device.
  580. * @param data the dataset.
  581. * @param row the row.
  582. * @param column the column.
  583. * @param plot the plot.
  584. * @param generator the label generator.
  585. * @param bar the bar.
  586. * @param negative a flag indicating a negative value.
  587. */
  588. protected void drawItemLabel(Graphics2D g2,
  589. CategoryDataset data,
  590. int row,
  591. int column,
  592. CategoryPlot plot,
  593. CategoryLabelGenerator generator,
  594. Rectangle2D bar,
  595. boolean negative) {
  596. String label = generator.generateLabel(data, row, column);
  597. if (label == null) {
  598. return; // nothing to do
  599. }
  600. Font labelFont = getItemLabelFont(row, column);
  601. g2.setFont(labelFont);
  602. Paint paint = getItemLabelPaint(row, column);
  603. g2.setPaint(paint);
  604. // find out where to place the label...
  605. ItemLabelPosition position = null;
  606. if (!negative) {
  607. position = getPositiveItemLabelPosition(row, column);
  608. }
  609. else {
  610. position = getNegativeItemLabelPosition(row, column);
  611. }
  612. // work out the label anchor point...
  613. Point2D anchorPoint = calculateLabelAnchorPoint(
  614. position.getItemLabelAnchor(), bar, plot.getOrientation()
  615. );
  616. if (isInternalAnchor(position.getItemLabelAnchor())) {
  617. Shape bounds = TextUtilities.calculateRotatedStringBounds(
  618. label, g2,
  619. (float) anchorPoint.getX(),
  620. (float) anchorPoint.getY(),
  621. position.getTextAnchor(),
  622. position.getAngle(),
  623. position.getRotationAnchor()
  624. );
  625. if (bounds != null) {
  626. if (!bar.contains(bounds.getBounds2D())) {
  627. if (!negative) {
  628. position = getPositiveItemLabelPositionFallback();
  629. }
  630. else {
  631. position = getNegativeItemLabelPositionFallback();
  632. }
  633. if (position != null) {
  634. anchorPoint = calculateLabelAnchorPoint(
  635. position.getItemLabelAnchor(), bar, plot.getOrientation()
  636. );
  637. }
  638. }
  639. }
  640. }
  641. if (position != null) {
  642. TextUtilities.drawRotatedString(
  643. label, g2, (float) anchorPoint.getX(), (float) anchorPoint.getY(),
  644. position.getTextAnchor(), position.getAngle(), position.getRotationAnchor()
  645. );
  646. }
  647. }
  648. /**
  649. * Calculates the item label anchor point.
  650. *
  651. * @param anchor the anchor.
  652. * @param bar the bar.
  653. * @param orientation the plot orientation.
  654. *
  655. * @return The anchor point.
  656. */
  657. private Point2D calculateLabelAnchorPoint(ItemLabelAnchor anchor,
  658. Rectangle2D bar, PlotOrientation orientation) {
  659. Point2D result = null;
  660. double offset = getItemLabelAnchorOffset();
  661. double x0 = bar.getX() - offset;
  662. double x1 = bar.getX();
  663. double x2 = bar.getX() + offset;
  664. double x3 = bar.getCenterX();
  665. double x4 = bar.getMaxX() - offset;
  666. double x5 = bar.getMaxX();
  667. double x6 = bar.getMaxX() + offset;
  668. double y0 = bar.getMaxY() + offset;
  669. double y1 = bar.getMaxY();
  670. double y2 = bar.getMaxY() - offset;
  671. double y3 = bar.getCenterY();
  672. double y4 = bar.getMinY() + offset;
  673. double y5 = bar.getMinY();
  674. double y6 = bar.getMinY() - offset;
  675. if (anchor == ItemLabelAnchor.CENTER) {
  676. result = new Point2D.Double(x3, y3);
  677. }
  678. else if (anchor == ItemLabelAnchor.INSIDE1) {
  679. result = new Point2D.Double(x4, y4);
  680. }
  681. else if (anchor == ItemLabelAnchor.INSIDE2) {
  682. result = new Point2D.Double(x4, y4);
  683. }
  684. else if (anchor == ItemLabelAnchor.INSIDE3) {
  685. result = new Point2D.Double(x4, y3);
  686. }
  687. else if (anchor == ItemLabelAnchor.INSIDE4) {
  688. result = new Point2D.Double(x4, y2);
  689. }
  690. else if (anchor == ItemLabelAnchor.INSIDE5) {
  691. result = new Point2D.Double(x4, y2);
  692. }
  693. else if (anchor == ItemLabelAnchor.INSIDE6) {
  694. result = new Point2D.Double(x3, y2);
  695. }
  696. else if (anchor == ItemLabelAnchor.INSIDE7) {
  697. result = new Point2D.Double(x2, y2);
  698. }
  699. else if (anchor == ItemLabelAnchor.INSIDE8) {
  700. result = new Point2D.Double(x2, y2);
  701. }
  702. else if (anchor == ItemLabelAnchor.INSIDE9) {
  703. result = new Point2D.Double(x2, y3);
  704. }
  705. else if (anchor == ItemLabelAnchor.INSIDE10) {
  706. result = new Point2D.Double(x2, y4);
  707. }
  708. else if (anchor == ItemLabelAnchor.INSIDE11) {
  709. result = new Point2D.Double(x2, y4);
  710. }
  711. else if (anchor == ItemLabelAnchor.INSIDE12) {
  712. result = new Point2D.Double(x3, y4);
  713. }
  714. else if (anchor == ItemLabelAnchor.OUTSIDE1) {
  715. result = new Point2D.Double(x5, y6);
  716. }
  717. else if (anchor == ItemLabelAnchor.OUTSIDE2) {
  718. result = new Point2D.Double(x6, y5);
  719. }
  720. else if (anchor == ItemLabelAnchor.OUTSIDE3) {
  721. result = new Point2D.Double(x6, y3);
  722. }
  723. else if (anchor == ItemLabelAnchor.OUTSIDE4) {
  724. result = new Point2D.Double(x6, y1);
  725. }
  726. else if (anchor == ItemLabelAnchor.OUTSIDE5) {
  727. result = new Point2D.Double(x5, y0);
  728. }
  729. else if (anchor == ItemLabelAnchor.OUTSIDE6) {
  730. result = new Point2D.Double(x3, y0);
  731. }
  732. else if (anchor == ItemLabelAnchor.OUTSIDE7) {
  733. result = new Point2D.Double(x1, y0);
  734. }
  735. else if (anchor == ItemLabelAnchor.OUTSIDE8) {
  736. result = new Point2D.Double(x0, y1);
  737. }
  738. else if (anchor == ItemLabelAnchor.OUTSIDE9) {
  739. result = new Point2D.Double(x0, y3);
  740. }
  741. else if (anchor == ItemLabelAnchor.OUTSIDE10) {
  742. result = new Point2D.Double(x0, y5);
  743. }
  744. else if (anchor == ItemLabelAnchor.OUTSIDE11) {
  745. result = new Point2D.Double(x1, y6);
  746. }
  747. else if (anchor == ItemLabelAnchor.OUTSIDE12) {
  748. result = new Point2D.Double(x3, y6);
  749. }
  750. return result;
  751. }
  752. /**
  753. * Returns <code>true</code> if the specified anchor point is inside a bar.
  754. *
  755. * @param anchor the anchor point.
  756. *
  757. * @return A boolean.
  758. */
  759. private boolean isInternalAnchor(ItemLabelAnchor anchor) {
  760. return anchor == ItemLabelAnchor.CENTER
  761. || anchor == ItemLabelAnchor.INSIDE1
  762. || anchor == ItemLabelAnchor.INSIDE2
  763. || anchor == ItemLabelAnchor.INSIDE3
  764. || anchor == ItemLabelAnchor.INSIDE4
  765. || anchor == ItemLabelAnchor.INSIDE5
  766. || anchor == ItemLabelAnchor.INSIDE6
  767. || anchor == ItemLabelAnchor.INSIDE7
  768. || anchor == ItemLabelAnchor.INSIDE8
  769. || anchor == ItemLabelAnchor.INSIDE9
  770. || anchor == ItemLabelAnchor.INSIDE10
  771. || anchor == ItemLabelAnchor.INSIDE11
  772. || anchor == ItemLabelAnchor.INSIDE12;
  773. }
  774. /**
  775. * Tests this instance for equality with an arbitrary object.
  776. *
  777. * @param obj the object (<code>null</code> permitted).
  778. *
  779. * @return A boolean.
  780. */
  781. public boolean equals(Object obj) {
  782. if (obj == this) {
  783. return true;
  784. }
  785. if (!(obj instanceof BarRenderer)) {
  786. return false;
  787. }
  788. if (!super.equals(obj)) {
  789. return false;
  790. }
  791. BarRenderer that = (BarRenderer) obj;
  792. if (this.itemMargin != that.itemMargin) {
  793. return false;
  794. }
  795. if (this.drawBarOutline != that.drawBarOutline) {
  796. return false;
  797. }
  798. if (this.maxBarWidth != that.maxBarWidth) {
  799. return false;
  800. }
  801. if (this.minimumBarLength != that.minimumBarLength) {
  802. return false;
  803. }
  804. if (!ObjectUtilities.equal(this.gradientPaintTransformer, that.gradientPaintTransformer)) {
  805. return false;
  806. }
  807. if (!ObjectUtilities.equal(
  808. this.positiveItemLabelPositionFallback, that.positiveItemLabelPositionFallback
  809. )) {
  810. return false;
  811. }
  812. if (!ObjectUtilities.equal(
  813. this.negativeItemLabelPositionFallback, that.negativeItemLabelPositionFallback
  814. )) {
  815. return false;
  816. }
  817. return true;
  818. }
  819. }