01 import java.awt.*;
02 import java.io.*;
03 import javax.servlet.*;
04 import javax.servlet.http.*;
05
06 import Acme.JPM.Encoders.GifEncoder;
07 import javachart.chart.*;
08
09 public class AutoChart extends HttpServlet {
10
11 static final int WIDTH = 450;
12 static final int HEIGHT = 320;
13
14 public void doGet(HttpServletRequest req, HttpServletResponse res)
15 throws ServletException, IOException {
16
17 ServletOutputStream out = res.getOutputStream();
18
19 Frame frame = null;
20 Graphics g = null;
21
22 try {
23 //vytvoríme graf
24 BarChart chart = new BarChart("Audi a koda");
25
26 //určíme mu názov
27 chart.getBackground().setTitleFont(new Font("Serif", Font.PLAIN, 24));
28 chart.getBackground().setTitleString("Porovnanie Audi a koda.");
29
30 //zobrazíme, umiestnime ho a nastavíme legendu
31 chart.setLegendVisible(true);
32 chart.getLegend().setLlX(0.4);
33 chart.getLegend().setLlY(0.75);
34 chart.getLegend().setIconHeight(0.04);
35 chart.getLegend().setIconWidth(0.04);
36 chart.getLegend().setIconGap(0.02);
37 chart.getLegend().setVerticalLayout(false);
38
39 //pridáme dáta a popisky
40 double[] audiData = {450, 520, 740, 563, 850};
41 chart.addDataSet("Audi", audiData);
42
43 double[] skodaData = {1435, 1650, 1555, 1440, 1595};
44 chart.addDataSet("koda", skodaData);
45
46 String[] labels = {"1999", "2000", "2001", "2002", "2003"};
47 chart.getXAxis().addLabels(labels);
48
49 //nastavíme farbu stĺpcov
50 chart.getDatasets()[0].getGc().setFillColor(Color.red);
51 chart.getDatasets()[1].getGc().setFillColor(Color.blue);
52
53 //pomenujeme osy
54 chart.getXAxis().setTitleString("Rok");
55 chart.getYAxis().setTitleString("Predaj");
56
57 //nastavíme vežkos
58 chart.resize(WIDTH, HEIGHT);
59
60 //vytvoríme frame
61 frame = new Frame();
62 frame.addNotify();
63
64 //vytvoríme image
65 Image image = frame.createImage(WIDTH, HEIGHT);
66 g = image.getGraphics();
67
68 //vykreslíme graf
69 chart.drawGraph(g);
70
71 //zakódujeme do GIFu a poleme
72 res.setContentType("image/gif");
73 GifEncoder encoder = new GifEncoder(image, out);
74 encoder.encode();
75 }
76 finally {
77 //uvožníme zdroje
78 if (g != null) g.dispose();
79 if (frame != null) frame.removeNotify();
80 }
81 }
82 }
|