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 charting4 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        // Identify the chart
16        page.setTextColor(0x0);
17        page.setFont(page.BOLD, 24);
18        page.println("Tutorial - Charting 4");
19        page.setFont(page.NORMAL, 12);
20        page.println(new Date().toString());
21    
22        // Get value sets and associate them to the chart
23        double [] noise = generateNoise(36);
24        BarValueSet [] nw = new BarValueSet [] {
25          new BarValueSet("Red - full scale",     1.0, 0xff0000, noise,
26            0xff8888, 0.02),
27          new BarValueSet("Green - half scale",   1.0, 0x00ff00, noise,
28            0x88ff88, 0.02),
29          new BarValueSet("Magenta - shifted double scale", 1.0, 0xff00ff, noise,
30            0xff88ff, 0.02)
31          };
32        nw[0].setOffset(-0.04);
33        nw[2].setOffset(0.04);
34        for (int n = 0; n < 3; ++n) page.appendValues(nw[n]);
35    
36        // Change the scaling
37        nw[0].setRange(-1.0, 1.0, -1.0);
38        nw[1].setRange(-2.0, 2.0, -2.0);
39        nw[2].setRange(-0.5, 0.5, -0.5);
40    
41        // Draw the first main chart
42        page.drawMain(1.5, 7.0, 6.5, 3.0, 36, 4, 0xdddddd, 2.0, 0x0);
43    
44        // Decorate the chart
45        page.drawLegend(5.25, 5.25, 2.5, 1.0, 0.09, 0.09, 0xdddddd, 2.0, 0x0);
46    
47        // Doodles
48        page.setLineColor(0x0000ff);
49        page.setLineWidth(0.5);
50        page.moveTo(5.25, 5.25);
51        page.drawTo(1.5, 7.0);
52        page.moveTo(7.75, 5.25);
53        page.drawTo(8.0, 7.0);
54    
55        // Modify the value array
56        for (int i = 10; i < 30; ++i) noise[i] = 0.5;
57    
58        // Stack the first and second values, and take up the width
59        nw[0].setStackable(true);
60        nw[0].setOffset(0.0);
61        nw[0].setBarWidth(0.04);
62        nw[1].setStackable(true);
63        nw[1].setBarWidth(0.04);
64        nw[2].setOffset(0.06);
65    
66        // Draw the second main chart
67        page.drawMain(1.5, 2.0, 6.5, 3.0, 36, 4, 0xdddddd, 2.0, 0x0);
68    
69        // Output the chart to the PDF file
70        page.close();
71      }
72    
73    // Produce sine wave data which is modulated with random offsets
74    double [] generateNoise(int resolution) {
75        double [] v = new double[resolution + 1];
76        for (int i = 0; i < v.length; ++i) {
77          v[i] = Math.sin((double)i * 2.0 * Math.PI / (double)(v.length - 1))
78            / 2.0 + Math.random() - 0.5;
79        }
80        return v;
81      }
82    
83    }