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 ; 11 import javax.servlet.ServletOutputStream ; 12 import java.awt.Font ; 13 import java.awt.HeadlessException ; 14 import java.awt.image.BufferedImage ; 15 import java.io.IOException ; 16 17 25 public class ChartUtil { 26 29 public static final class NumberOnlyBuildLabel implements Comparable <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 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 toString() { 50 return build.getDisplayName(); 51 } 52 } 53 54 57 public static boolean awtProblem = false; 58 59 62 public static void generateGraph(StaplerRequest req, StaplerResponse rsp, JFreeChart chart, int defaultW, int defaultH) throws IOException { 63 try { 64 String w = req.getParameter("width"); 65 if(w==null) w=String.valueOf(defaultW); 66 String h = req.getParameter("height"); 67 if(h==null) h=String.valueOf(defaultH); 68 BufferedImage image = chart.createBufferedImage(Integer.parseInt(w),Integer.parseInt(h)); 69 rsp.setContentType("image/png"); 70 ServletOutputStream os = rsp.getOutputStream(); 71 ImageIO.write(image, "PNG", os); 72 os.close(); 73 } catch(HeadlessException e) { 74 rsp.sendRedirect2(req.getContextPath()+"/images/headless.png"); 76 } 77 } 78 79 82 public static void generateClickableMap(StaplerRequest req, StaplerResponse rsp, JFreeChart chart, int defaultW, int defaultH) throws IOException { 83 String w = req.getParameter("width"); 84 if(w==null) w=String.valueOf(defaultW); 85 String 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 ("SansSerif",Font.BOLD,18).toString(); 98 } catch (Throwable t) { 99 awtProblem = true; 100 } 101 } 102 } 103 | Popular Tags |