KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > StackedAreaRenderer2


1 package hudson.util;
2
3 import org.jfree.chart.axis.CategoryAxis;
4 import org.jfree.chart.axis.ValueAxis;
5 import org.jfree.chart.entity.EntityCollection;
6 import org.jfree.chart.labels.CategoryToolTipGenerator;
7 import org.jfree.chart.plot.CategoryPlot;
8 import org.jfree.chart.renderer.AreaRendererEndType;
9 import org.jfree.chart.renderer.category.CategoryItemRendererState;
10 import org.jfree.chart.renderer.category.StackedAreaRenderer;
11 import org.jfree.chart.urls.CategoryURLGenerator;
12 import org.jfree.data.category.CategoryDataset;
13 import org.jfree.ui.RectangleEdge;
14
15 import java.awt.Graphics2D JavaDoc;
16 import java.awt.Polygon JavaDoc;
17 import java.awt.Paint JavaDoc;
18 import java.awt.geom.Rectangle2D JavaDoc;
19
20 /**
21  * Modified {@link StackedAreaRenderer}.
22  *
23  * <ol>
24  * <li>Built-in support for {@link #generateToolTip(CategoryDataset, int, int) tooltip}
25  * and {@link #generateURL(CategoryDataset, int, int) hyperlinks} for clickable map.
26  * <li>Clickable map hit test is modified so that the entire area is clickable,
27  * not just the small area around each data point.
28  * <li>Rendering algorithm is modified so that
29  * {@link #getItemPaint(int, int) different color on the same data series}
30  * will look more natural.
31  * </ol>
32  *
33  * @author Kohsuke Kawaguchi
34 */

35 public class StackedAreaRenderer2 extends StackedAreaRenderer
36     implements CategoryToolTipGenerator, CategoryURLGenerator {
37
38     public StackedAreaRenderer2() {
39         setEndType(AreaRendererEndType.TRUNCATE);
40         setItemURLGenerator(this);
41         setToolTipGenerator(this);
42     }
43
44     /**
45      * Override this method to specify the hyperlink target of the given data point.
46      */

47     public String JavaDoc generateURL(CategoryDataset dataset, int row, int column) {
48         return null;
49     }
50
51     /**
52      * Override this method to specify the tool tip text of the given data point.
53      */

54     public String JavaDoc generateToolTip(CategoryDataset dataset, int row, int column) {
55         return null;
56     }
57
58     /**
59      * Override this method to specify the color to draw the given area.
60      */

61     public Paint JavaDoc getItemPaint(int row, int column) {
62         return super.getItemPaint(row, column);
63     }
64
65     // Mostly copied from the base class.
66
@Override JavaDoc
67     public void drawItem(Graphics2D JavaDoc g2,
68                          CategoryItemRendererState state,
69                          Rectangle2D JavaDoc dataArea,
70                          CategoryPlot plot,
71                          CategoryAxis domainAxis,
72                          ValueAxis rangeAxis,
73                          CategoryDataset dataset,
74                          int row,
75                          int column,
76                          int pass) {
77
78         // plot non-null values...
79
Number JavaDoc dataValue = dataset.getValue(row, column);
80         if (dataValue == null) {
81             return;
82         }
83
84         double value = dataValue.doubleValue();
85
86         // leave the y values (y1, y0) untranslated as it is going to be be
87
// stacked up later by previous series values, after this it will be
88
// translated.
89
double xx1 = domainAxis.getCategoryMiddle(column, getColumnCount(),
90                 dataArea, plot.getDomainAxisEdge());
91
92         double previousHeightx1 = getPreviousHeight(dataset, row, column);
93         double y1 = value + previousHeightx1;
94         RectangleEdge location = plot.getRangeAxisEdge();
95         double yy1 = rangeAxis.valueToJava2D(y1, dataArea, location);
96
97         g2.setPaint(getItemPaint(row, column));
98         g2.setStroke(getItemStroke(row, column));
99
100         // add an item entity, if this information is being collected
101
EntityCollection entities = state.getEntityCollection();
102
103         // in column zero, the only job to do is draw any visible item labels
104
// and this is done in the second pass...
105
if (column == 0) {
106             if (pass == 1) {
107                 // draw item labels, if visible
108
if (isItemLabelVisible(row, column)) {
109                     drawItemLabel(g2, plot.getOrientation(), dataset, row, column,
110                             xx1, yy1, (y1 < 0.0));
111                 }
112             }
113         } else {
114             Number JavaDoc previousValue = dataset.getValue(row, column - 1);
115             if (previousValue != null) {
116
117                 double xx0 = domainAxis.getCategoryMiddle(column - 1,
118                         getColumnCount(), dataArea, plot.getDomainAxisEdge());
119                 double y0 = previousValue.doubleValue();
120
121
122                 // Get the previous height, but this will be different for both
123
// y0 and y1 as the previous series values could differ.
124
double previousHeightx0 = getPreviousHeight(dataset, row,
125                         column - 1);
126
127                 // Now stack the current y values on top of the previous values.
128
y0 += previousHeightx0;
129
130                 // Now translate the previous heights
131
double previousHeightxx0 = rangeAxis.valueToJava2D(
132                         previousHeightx0, dataArea, location);
133                 double previousHeightxx1 = rangeAxis.valueToJava2D(
134                         previousHeightx1, dataArea, location);
135
136                 // Now translate the current y values.
137
double yy0 = rangeAxis.valueToJava2D(y0, dataArea, location);
138
139                 if (pass == 0) {
140                     // left half
141
Polygon JavaDoc p = new Polygon JavaDoc();
142                     p.addPoint((int) xx0, (int) yy0);
143                     p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
144                     p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
145                     p.addPoint((int) xx0, (int) previousHeightxx0);
146
147                     g2.setPaint(getItemPaint(row, column-1));
148                     g2.setStroke(getItemStroke(row, column-1));
149                     g2.fill(p);
150
151                     if (entities != null)
152                         addItemEntity(entities, dataset, row, column-1, p);
153
154                     // right half
155
p = new Polygon JavaDoc();
156                     p.addPoint((int) xx1, (int) yy1);
157                     p.addPoint((int) (xx0+xx1)/2, (int) (yy0+yy1)/2);
158                     p.addPoint((int) (xx0+xx1)/2, (int) (previousHeightxx0+previousHeightxx1)/2);
159                     p.addPoint((int) xx1, (int) previousHeightxx1);
160
161                     g2.setPaint(getItemPaint(row, column));
162                     g2.setStroke(getItemStroke(row, column));
163                     g2.fill(p);
164
165                     if (entities != null)
166                         addItemEntity(entities, dataset, row, column, p);
167                 } else {
168                     if (isItemLabelVisible(row, column)) {
169                         drawItemLabel(g2, plot.getOrientation(), dataset, row,
170                                 column, xx1, yy1, (y1 < 0.0));
171                     }
172                 }
173             }
174         }
175     }
176 }
177
Popular Tags