1     package sutut1;
2     
3     import javax.servlet.*;
4     import javax.servlet.http.*;
5     import java.io.*;
6     import java.util.*;
7     
8     import us.oh.state.common.utility.*;
9     
10    public class charting5 extends HttpServlet {
11    
12      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
13        response.setContentType("application/pdf");
14        PdfChart page = new PdfChart(response.getOutputStream());
15        page.setTextColor(0x0);
16        page.setFont(page.BOLD, 24);
17        page.println("Tutorial - Charting 5c");
18        page.setFont(page.NORMAL, 12);
19        page.println(new Date().toString());
20        double [] vals = new double [] { 3.4, 2.9, 7.5, 4.1, 2.4 };
21        // glyph coordinates are pairs of XY doubles
22        double [] glyph = new double [] {
23          -0.10, 0.0,
24          0.10, 0.0,
25          0.0, 0.16
26          };
27        GlyphBarValueSet gb = new GlyphBarValueSet("JVM Performance", 1.0, 0x0,
28          vals, 0xff8888, 0.25, glyph, 0.20);
29        page.appendValues(gb);
30        gb.setRange(0.0, 10.0, 0.0);
31        page.drawMain(1.5, 7.0, 6.5, 3.0, 36, 4, 0xdddddd, 2.0, 0x0);
32        page.close();
33      }
34    
35    }
36    
37    class GlyphBarValueSet extends ValueSet {
38    
39    private int barColor;
40    private double barWidth;
41    private double [] glyphCoords;
42    private double pitch;
43    
44    public GlyphBarValueSet(String name, double lineThickness, int lineColor,
45        double [] values, int barColor, double barWidth,
46        double [] glyphCoords, double pitch) {
47      super(name, lineThickness, lineColor, values);
48      this.barColor = barColor;
49      this.barWidth = barWidth;
50      this.glyphCoords = glyphCoords;
51      this.pitch = pitch;
52    }
53    
54    public int getFillColor() {
55      return barColor;
56    }
57    
58    public double getBarWidth() {
59      return barWidth;
60    }
61    
62    public void setBarWidth(double barWidth) {
63      this.barWidth = barWidth;
64    }
65    
66    public void render(ChartGraphics chart) {
67      chart.setLineColor(this.getLineColor());
68      chart.setLineWidth(this.getLineThickness());
69      chart.setFillColor(this.getFillColor());
70      int j = this.getValueCount();
71      double incr = chart.getWidth() / (j + 1);
72      this.setScale(chart.getHeight());
73      double yAxis = chart.getYLocation() + this.getBaseValue();
74      double x = chart.getXLocation() - this.getBarWidth() / 2.0;
75      double x2 = x + this.getBarWidth();
76      // Examine the glyph to find the lowest point
77      double lowestPt = glyphCoords[1];
78      int numPoints = glyphCoords.length / 2;
79      for (int pt = 0; pt < numPoints; ++pt) {
80        if (glyphCoords[pt * 2 + 1] < lowestPt) lowestPt = glyphCoords[pt * 2 + 1];
81      }
82      // Some objects which implement ChartGraphics may not be able to
83      // push and pop state or provide clipping - but we know that we
84      // are working with a PdfChart, so we can go around this barrier
85      PdfChart pchart = (PdfChart)chart;
86      // Draw each bar
87      while (j-- > 0) {
88        pchart.pushGraphicState();
89        double top = yAxis + this.getNextValue();
90        pchart.clipRect(x += incr, yAxis, x2 += incr, top);
91        double cx = (x + x2) / 2.0;
92        double cy = yAxis;
93        // Draw enough glyphs to be sure the topmost one is clipped
94        while ((cy - lowestPt) < top) {
95          for (int pt = 0; pt < numPoints; ++pt) {
96            double gx = cx + glyphCoords[pt * 2];
97            double gy = cy + glyphCoords[pt * 2 + 1];
98            if (pt == 0) chart.moveTo(gx, gy);
99            else chart.drawTo(gx, gy);
100         }
101         cy += pitch;
102         chart.fillPoly();
103       }
104       pchart.popGraphicState();
105     }
106   }
107   
108   public void sample(ChartGraphics chart, double x, double cx, double y, double size) {
109     chart.setLineColor(this.getLineColor());
110     chart.setLineWidth(this.getLineThickness());
111     chart.setFillColor(this.getFillColor());
112     chart.fillRect(cx - size, y - size, cx + size, y + size);
113   }
114   
115   }