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 charting3 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 3");
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(72);
24        ValueSet [] nw = new ValueSet [] {
25          new LineValueSet("Red - full scale",     1.0, 0xff0000, noise, 0.0, 2.0 * Math.PI),
26          new LineValueSet("Green - half scale",   1.0, 0x00ff00, noise, 0.0, 2.0 * Math.PI),
27          new LineValueSet("Magenta - shifted double scale", 1.0, 0xff00ff, noise, 0.0, 2.0 * Math.PI)
28          };
29        for (int n = 0; n < 3; ++n) page.appendValues(nw[n]);
30    
31        // Change the scaling
32        nw[0].setRange(-1.0, 1.0, -1.0);
33        nw[1].setRange(-2.0, 2.0, -2.0);
34        nw[2].setRange(-0.5, 0.5, 0.0);
35    
36        // Apply grid lines
37        page.appendGridX(0.5, 1, 0x000088);
38        page.appendGridX(1.0, 9, 0x0);
39        page.appendGridY(1.0, 2, 0x0);
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        String [] xLabels = new String [13];
46        for (int i = 0; i < xLabels.length; ++i) {
47          xLabels[i] = Integer.toString(i * 30);
48        }
49        page.setFont(page.BOLD, 10);
50        page.drawBottomLabels(0.05, 0.375, 90.0, xLabels, 0.05);
51        page.setTextColor(0x880000);
52        page.drawLeftLabels(0.375, -0.05, 0.0,
53          new String [] {"-1.0", "-0.5", "0.0", "0.5", "1.0"}, 0.05);
54        page.setTextDensity(0.5);
55        page.drawLegend(5.25, 5.25, 2.5, 1.0, 0.09, 0.09, 0xdddddd, 2.0, 0x0);
56    
57        // Doodles
58        page.setLineColor(0x0000ff);
59        page.setLineWidth(0.5);
60        page.moveTo(5.25, 5.25);
61        page.drawTo(1.5, 7.0);
62        page.moveTo(7.75, 5.25);
63        page.drawTo(8.0, 7.0);
64    
65        // Modify the value array
66        for (int i = 10; i < 30; ++i) noise[i] = 0.5;
67    
68        // Draw the second main chart
69        page.drawMain(1.5, 2.0, 6.5, 3.0, 36, 4, 0xdddddd, 2.0, 0x0);
70    
71        // Output the chart to the PDF file
72        page.close();
73      }
74    
75    // Produce sine wave data which is modulated with random offsets
76    double [] generateNoise(int resolution) {
77        double [] v = new double[resolution + 1];
78        for (int i = 0; i < v.length; ++i) {
79          v[i] = Math.sin((double)i * 2.0 * Math.PI / (double)(v.length - 1))
80            / 2.0 + Math.random() - 0.5;
81        }
82        return v;
83      }
84    
85    }