KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > hudson > util > ChartUtil


1 package hudson.util;
2
3 import hudson.model.AbstractBuild;
4 import org.jfree.chart.ChartRenderingInfo;
5 import org.jfree.chart.ChartUtilities;
6 import org.jfree.chart.JFreeChart;
7 import org.kohsuke.stapler.StaplerRequest;
8 import org.kohsuke.stapler.StaplerResponse;
9
10 import javax.imageio.ImageIO JavaDoc;
11 import javax.servlet.ServletOutputStream JavaDoc;
12 import java.awt.Font JavaDoc;
13 import java.awt.HeadlessException JavaDoc;
14 import java.awt.image.BufferedImage JavaDoc;
15 import java.io.IOException JavaDoc;
16
17 /**
18  * Chart generation utility code around JFreeChart.
19  *
20  * @see StackedAreaRenderer2
21  * @see DataSetBuilder
22  * @see ShiftedCategoryAxis
23  * @author Kohsuke Kawaguchi
24  */

25 public class ChartUtil {
26     /**
27      * Can be used as a graph label. Only displays numbers.
28      */

29     public static final class NumberOnlyBuildLabel implements Comparable JavaDoc<NumberOnlyBuildLabel> {
30         public final AbstractBuild build;
31
32         public NumberOnlyBuildLabel(AbstractBuild build) {
33             this.build = build;
34         }
35
36         public int compareTo(NumberOnlyBuildLabel that) {
37             return this.build.number-that.build.number;
38         }
39
40         public boolean equals(Object JavaDoc o) {
41             NumberOnlyBuildLabel that = (NumberOnlyBuildLabel) o;
42             return build==that.build;
43         }
44
45         public int hashCode() {
46             return build.hashCode();
47         }
48
49         public String JavaDoc toString() {
50             return build.getDisplayName();
51         }
52     }
53
54     /**
55      * See issue 93. Detect an error in X11 and handle it gracefully.
56      */

57     public static boolean awtProblem = false;
58
59     /**
60      * Generates the graph in PNG format and sends that to the response.
61      */

62     public static void generateGraph(StaplerRequest req, StaplerResponse rsp, JFreeChart chart, int defaultW, int defaultH) throws IOException JavaDoc {
63         try {
64             String JavaDoc w = req.getParameter("width");
65             if(w==null) w=String.valueOf(defaultW);
66             String JavaDoc h = req.getParameter("height");
67             if(h==null) h=String.valueOf(defaultH);
68             BufferedImage JavaDoc image = chart.createBufferedImage(Integer.parseInt(w),Integer.parseInt(h));
69             rsp.setContentType("image/png");
70             ServletOutputStream JavaDoc os = rsp.getOutputStream();
71             ImageIO.write(image, "PNG", os);
72             os.close();
73         } catch(HeadlessException JavaDoc e) {
74             // not available. send out error message
75
rsp.sendRedirect2(req.getContextPath()+"/images/headless.png");
76         }
77     }
78
79     /**
80      * Generates the clickable map info and sends that to the response.
81      */

82     public static void generateClickableMap(StaplerRequest req, StaplerResponse rsp, JFreeChart chart, int defaultW, int defaultH) throws IOException JavaDoc {
83         String JavaDoc w = req.getParameter("width");
84         if(w==null) w=String.valueOf(defaultW);
85         String JavaDoc h = req.getParameter("height");
86         if(h==null) h=String.valueOf(defaultH);
87
88         ChartRenderingInfo info = new ChartRenderingInfo();
89         chart.createBufferedImage(Integer.parseInt(w),Integer.parseInt(h),info);
90
91         rsp.setContentType("text/plain;charset=UTF-8");
92         rsp.getWriter().println(ChartUtilities.getImageMap( "map", info ));
93     }
94
95     static {
96         try {
97             new Font JavaDoc("SansSerif",Font.BOLD,18).toString();
98         } catch (Throwable JavaDoc t) {
99             awtProblem = true;
100         }
101     }
102 }
103
Popular Tags