PageRenderTime 13ms CodeModel.GetById 2ms app.highlight 8ms RepoModel.GetById 1ms app.codeStats 0ms

/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 */
 24package hudson.util;
 25
 26import org.jfree.chart.axis.CategoryAxis;
 27import org.jfree.chart.axis.ValueAxis;
 28import org.jfree.chart.entity.EntityCollection;
 29import org.jfree.chart.labels.CategoryToolTipGenerator;
 30import org.jfree.chart.plot.CategoryPlot;
 31import org.jfree.chart.renderer.AreaRendererEndType;
 32import org.jfree.chart.renderer.category.CategoryItemRendererState;
 33import org.jfree.chart.renderer.category.StackedAreaRenderer;
 34import org.jfree.chart.urls.CategoryURLGenerator;
 35import org.jfree.data.category.CategoryDataset;
 36import org.jfree.ui.RectangleEdge;
 37
 38import java.awt.Graphics2D;
 39import java.awt.Polygon;
 40import java.awt.Paint;
 41import java.awt.geom.Rectangle2D;
 42
 43/**
 44 * Modified {@link StackedAreaRenderer}.
 45 *
 46 * <ol>
 47 * <li>Built-in support for {@link #generateToolTip(CategoryDataset, int, int) tooltip}
 48 *     and {@link #generateURL(CategoryDataset, int, int) hyperlinks} for clickable map.
 49 * <li>Clickable map hit test is modified so that the entire area is clickable,
 50 *     not just the small area around each data point.
 51 * <li>Rendering algorithm is modified so that
 52 *     {@link #getItemPaint(int, int) different color on the same data series}
 53 *     will look more natural.
 54 * </ol>
 55 *
 56 * @author Kohsuke Kawaguchi
 57*/
 58public class StackedAreaRenderer2 extends StackedAreaRenderer
 59    implements CategoryToolTipGenerator, CategoryURLGenerator {
 60
 61    public StackedAreaRenderer2() {
 62        setEndType(AreaRendererEndType.TRUNCATE);
 63        setItemURLGenerator(this);
 64        setToolTipGenerator(this);
 65    }
 66
 67    /**
 68     * Override this method to specify the hyperlink target of the given data point.
 69     */
 70    public String generateURL(CategoryDataset dataset, int row, int column) {
 71        return null;
 72    }
 73
 74    /**
 75     * Override this method to specify the tool tip text of the given data point.
 76     */
 77    public String generateToolTip(CategoryDataset dataset, int row, int column) {
 78        return null;
 79    }
 80
 81    /**
 82     * Override this method to specify the color to draw the given area.
 83     */
 84    @Override
 85    public Paint getItemPaint(int row, int column) {
 86        return super.getItemPaint(row, column);
 87    }
 88
 89    // Mostly copied from the base class.
 90    @Override
 91    public void drawItem(Graphics2D g2,
 92                         CategoryItemRendererState state,
 93                         Rectangle2D dataArea,
 94                         CategoryPlot plot,
 95                         CategoryAxis domainAxis,
 96                         ValueAxis rangeAxis,
 97                         CategoryDataset dataset,
 98                         int row,
 99                         int column,
100                         int pass) {
101
102        // plot non-null values...
103        Number dataValue = dataset.getValue(row, column);
104        if (dataValue == null) {
105            return;
106        }
107
108        double value = dataValue.doubleValue();
109
110        // leave the y values (y1, y0) untranslated as it is going to be be
111        // stacked up later by previous series values, after this it will be
112        // translated.
113        double xx1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
114                dataArea, plot.getDomainAxisEdge());
115
116        double previousHeightx1 = getPreviousHeight(dataset, row, column);
117        double y1 = value + previousHeightx1;
118        RectangleEdge location = plot.getRangeAxisEdge();
119        double yy1 = rangeAxis.valueToJava2D(y1, dataArea, location);
120
121        g2.setPaint(getItemPaint(row, column));
122        g2.setStroke(getItemStroke(row, column));
123
124        // add an item entity, if this information is being collected
125        EntityCollection entities = state.getEntityCollection();
126
127        // in column zero, the only job to do is draw any visible item labels
128        // and this is done in the second pass...
129        if (column == 0) {
130            if (pass == 1) {
131                // draw item labels, if visible
132                if (isItemLabelVisible(row, column)) {
133                    drawItemLabel(g2, plot.getOrientation(), dataset, row, column,
134                            xx1, yy1, (y1 < 0.0));
135                }
136            }
137        } else {
138            Number previousValue = dataset.getValue(row, column - 1);
139            if (previousValue != null) {
140
141                double xx0 = domainAxis.getCategoryMiddle(column - 1,
142                        getColumnCount(), dataArea, plot.getDomainAxisEdge());
143                double y0 = previousValue.doubleValue();
144
145
146                // Get the previous height, but this will be different for both
147                // y0 and y1 as the previous series values could differ.
148                double previousHeightx0 = getPreviousHeight(dataset, row,
149                        column - 1);
150
151                // Now stack the current y values on top of the previous values.
152                y0 += previousHeightx0;
153
154                // Now translate the previous heights
155                double previousHeightxx0 = rangeAxis.valueToJava2D(
156                        previousHeightx0, dataArea, location);
157                double previousHeightxx1 = rangeAxis.valueToJava2D(
158                        previousHeightx1, dataArea, location);
159
160                // Now translate the current y values.
161                double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);
162
163                if (pass == 0) {
164                    // left half
165                    Polygon p = new Polygon();
166                    p.addPoint((int) xx0, (int) yy0);
167                    p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
168                    p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
169                    p.addPoint((int) xx0, (int) previousHeightxx0);
170
171                    g2.setPaint(getItemPaint(row, column-1));
172                    g2.setStroke(getItemStroke(row, column-1));
173                    g2.fill(p);
174
175                    if (entities != null)
176                        addItemEntity(entities, dataset, row, column-1, p);
177
178                    // right half
179                    p = new Polygon();
180                    p.addPoint((int) xx1, (int) yy1);
181                    p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
182                    p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
183                    p.addPoint((int) xx1, (int) previousHeightxx1);
184
185                    g2.setPaint(getItemPaint(row, column));
186                    g2.setStroke(getItemStroke(row, column));
187                    g2.fill(p);
188
189                    if (entities != null)
190                        addItemEntity(entities, dataset, row, column, p);
191                } else {
192                    if (isItemLabelVisible(row, column)) {
193                        drawItemLabel(g2, plot.getOrientation(), dataset, row,
194                                column, xx1, yy1, (y1 < 0.0));
195                    }
196                }
197            }
198        }
199    }
200}