/hudson-core/src/main/java/hudson/util/StackedAreaRenderer2.java

http://github.com/hudson/hudson · Java · 200 lines · 110 code · 27 blank · 63 comment · 18 complexity · 75380accf9e31858c4a05079d9106959 MD5 · raw file

  1. /*
  2. * The MIT License
  3. *
  4. * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. package hudson.util;
  25. import org.jfree.chart.axis.CategoryAxis;
  26. import org.jfree.chart.axis.ValueAxis;
  27. import org.jfree.chart.entity.EntityCollection;
  28. import org.jfree.chart.labels.CategoryToolTipGenerator;
  29. import org.jfree.chart.plot.CategoryPlot;
  30. import org.jfree.chart.renderer.AreaRendererEndType;
  31. import org.jfree.chart.renderer.category.CategoryItemRendererState;
  32. import org.jfree.chart.renderer.category.StackedAreaRenderer;
  33. import org.jfree.chart.urls.CategoryURLGenerator;
  34. import org.jfree.data.category.CategoryDataset;
  35. import org.jfree.ui.RectangleEdge;
  36. import java.awt.Graphics2D;
  37. import java.awt.Polygon;
  38. import java.awt.Paint;
  39. import java.awt.geom.Rectangle2D;
  40. /**
  41. * Modified {@link StackedAreaRenderer}.
  42. *
  43. * <ol>
  44. * <li>Built-in support for {@link #generateToolTip(CategoryDataset, int, int) tooltip}
  45. * and {@link #generateURL(CategoryDataset, int, int) hyperlinks} for clickable map.
  46. * <li>Clickable map hit test is modified so that the entire area is clickable,
  47. * not just the small area around each data point.
  48. * <li>Rendering algorithm is modified so that
  49. * {@link #getItemPaint(int, int) different color on the same data series}
  50. * will look more natural.
  51. * </ol>
  52. *
  53. * @author Kohsuke Kawaguchi
  54. */
  55. public class StackedAreaRenderer2 extends StackedAreaRenderer
  56. implements CategoryToolTipGenerator, CategoryURLGenerator {
  57. public StackedAreaRenderer2() {
  58. setEndType(AreaRendererEndType.TRUNCATE);
  59. setItemURLGenerator(this);
  60. setToolTipGenerator(this);
  61. }
  62. /**
  63. * Override this method to specify the hyperlink target of the given data point.
  64. */
  65. public String generateURL(CategoryDataset dataset, int row, int column) {
  66. return null;
  67. }
  68. /**
  69. * Override this method to specify the tool tip text of the given data point.
  70. */
  71. public String generateToolTip(CategoryDataset dataset, int row, int column) {
  72. return null;
  73. }
  74. /**
  75. * Override this method to specify the color to draw the given area.
  76. */
  77. @Override
  78. public Paint getItemPaint(int row, int column) {
  79. return super.getItemPaint(row, column);
  80. }
  81. // Mostly copied from the base class.
  82. @Override
  83. public void drawItem(Graphics2D g2,
  84. CategoryItemRendererState state,
  85. Rectangle2D dataArea,
  86. CategoryPlot plot,
  87. CategoryAxis domainAxis,
  88. ValueAxis rangeAxis,
  89. CategoryDataset dataset,
  90. int row,
  91. int column,
  92. int pass) {
  93. // plot non-null values...
  94. Number dataValue = dataset.getValue(row, column);
  95. if (dataValue == null) {
  96. return;
  97. }
  98. double value = dataValue.doubleValue();
  99. // leave the y values (y1, y0) untranslated as it is going to be be
  100. // stacked up later by previous series values, after this it will be
  101. // translated.
  102. double xx1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
  103. dataArea, plot.getDomainAxisEdge());
  104. double previousHeightx1 = getPreviousHeight(dataset, row, column);
  105. double y1 = value + previousHeightx1;
  106. RectangleEdge location = plot.getRangeAxisEdge();
  107. double yy1 = rangeAxis.valueToJava2D(y1, dataArea, location);
  108. g2.setPaint(getItemPaint(row, column));
  109. g2.setStroke(getItemStroke(row, column));
  110. // add an item entity, if this information is being collected
  111. EntityCollection entities = state.getEntityCollection();
  112. // in column zero, the only job to do is draw any visible item labels
  113. // and this is done in the second pass...
  114. if (column == 0) {
  115. if (pass == 1) {
  116. // draw item labels, if visible
  117. if (isItemLabelVisible(row, column)) {
  118. drawItemLabel(g2, plot.getOrientation(), dataset, row, column,
  119. xx1, yy1, (y1 < 0.0));
  120. }
  121. }
  122. } else {
  123. Number previousValue = dataset.getValue(row, column - 1);
  124. if (previousValue != null) {
  125. double xx0 = domainAxis.getCategoryMiddle(column - 1,
  126. getColumnCount(), dataArea, plot.getDomainAxisEdge());
  127. double y0 = previousValue.doubleValue();
  128. // Get the previous height, but this will be different for both
  129. // y0 and y1 as the previous series values could differ.
  130. double previousHeightx0 = getPreviousHeight(dataset, row,
  131. column - 1);
  132. // Now stack the current y values on top of the previous values.
  133. y0 += previousHeightx0;
  134. // Now translate the previous heights
  135. double previousHeightxx0 = rangeAxis.valueToJava2D(
  136. previousHeightx0, dataArea, location);
  137. double previousHeightxx1 = rangeAxis.valueToJava2D(
  138. previousHeightx1, dataArea, location);
  139. // Now translate the current y values.
  140. double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);
  141. if (pass == 0) {
  142. // left half
  143. Polygon p = new Polygon();
  144. p.addPoint((int) xx0, (int) yy0);
  145. p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
  146. p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
  147. p.addPoint((int) xx0, (int) previousHeightxx0);
  148. g2.setPaint(getItemPaint(row, column-1));
  149. g2.setStroke(getItemStroke(row, column-1));
  150. g2.fill(p);
  151. if (entities != null)
  152. addItemEntity(entities, dataset, row, column-1, p);
  153. // right half
  154. p = new Polygon();
  155. p.addPoint((int) xx1, (int) yy1);
  156. p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
  157. p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
  158. p.addPoint((int) xx1, (int) previousHeightxx1);
  159. g2.setPaint(getItemPaint(row, column));
  160. g2.setStroke(getItemStroke(row, column));
  161. g2.fill(p);
  162. if (entities != null)
  163. addItemEntity(entities, dataset, row, column, p);
  164. } else {
  165. if (isItemLabelVisible(row, column)) {
  166. drawItemLabel(g2, plot.getOrientation(), dataset, row,
  167. column, xx1, yy1, (y1 < 0.0));
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }