KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jfree > chart > demo > ImageMapDemo3


1 /* ======================================
2  * JFreeChart : a free Java chart library
3  * ======================================
4  *
5  * Project Info: http://www.jfree.org/jfreechart/index.html
6  * Project Lead: David Gilbert (david.gilbert@object-refinery.com);
7  *
8  * (C) Copyright 2000-2003, by Object Refinery Limited and Contributors.
9  *
10  * This library is free software; you can redistribute it and/or modify it under the terms
11  * of the GNU Lesser General Public License as published by the Free Software Foundation;
12  * either version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
15  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16  * See the GNU Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public License along with this
19  * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
20  * Boston, MA 02111-1307, USA.
21  *
22  * ------------------
23  * ImageMapDemo3.java
24  * ------------------
25  * (C) Copyright 2002, 2003, by Richard Atkinson and Contributors.
26  *
27  * Original Author: Richard Atkinson (richard_c_atkinson@ntlworld.com);
28  * Contributor(s): David Gilbert (for Object Refinery Limited);
29  *
30  * $Id: ImageMapDemo3.java,v 1.9 2003/11/28 10:57:35 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 18-Jul-2002 : Version 1 (RA);
35  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
36  *
37  */

38 package org.jfree.chart.demo;
39
40 import java.io.BufferedOutputStream JavaDoc;
41 import java.io.File JavaDoc;
42 import java.io.FileOutputStream JavaDoc;
43 import java.io.IOException JavaDoc;
44 import java.io.OutputStream JavaDoc;
45 import java.io.PrintWriter JavaDoc;
46 import java.text.ParseException JavaDoc;
47 import java.text.SimpleDateFormat JavaDoc;
48 import java.util.ArrayList JavaDoc;
49
50 import org.jfree.chart.ChartRenderingInfo;
51 import org.jfree.chart.ChartUtilities;
52 import org.jfree.chart.JFreeChart;
53 import org.jfree.chart.axis.DateAxis;
54 import org.jfree.chart.axis.NumberAxis;
55 import org.jfree.chart.axis.ValueAxis;
56 import org.jfree.chart.entity.StandardEntityCollection;
57 import org.jfree.chart.labels.CustomXYToolTipGenerator;
58 import org.jfree.chart.plot.XYPlot;
59 import org.jfree.chart.renderer.StandardXYItemRenderer;
60 import org.jfree.chart.urls.StandardXYURLGenerator;
61 import org.jfree.data.XYSeries;
62 import org.jfree.data.XYSeriesCollection;
63
64 /**
65  * A demo showing the construction of HTML image maps with JFreeChart.
66  *
67  * @author Richard Atkinson
68  */

69 public class ImageMapDemo3 {
70
71     /**
72      * Default constructor.
73      */

74     public ImageMapDemo3() {
75         super();
76     }
77
78     /**
79      * Starting point for the demo.
80      *
81      * @param args ignored.
82      *
83      * @throws ParseException if there is a problem parsing dates.
84      */

85     public static void main(String JavaDoc[] args) throws ParseException JavaDoc {
86
87         // Create a sample dataset
88
SimpleDateFormat JavaDoc sdf = new SimpleDateFormat JavaDoc("dd-MMM-yyyy");
89         XYSeries dataSeries = new XYSeries("Curve data");
90         ArrayList JavaDoc toolTips = new ArrayList JavaDoc();
91         dataSeries.add(sdf.parse("01-Jul-2002").getTime(), 5.22);
92         toolTips.add("1D - 5.22");
93         dataSeries.add(sdf.parse("02-Jul-2002").getTime(), 5.18);
94         toolTips.add("2D - 5.18");
95         dataSeries.add(sdf.parse("03-Jul-2002").getTime(), 5.23);
96         toolTips.add("3D - 5.23");
97         dataSeries.add(sdf.parse("04-Jul-2002").getTime(), 5.15);
98         toolTips.add("4D - 5.15");
99         dataSeries.add(sdf.parse("05-Jul-2002").getTime(), 5.22);
100         toolTips.add("5D - 5.22");
101         dataSeries.add(sdf.parse("06-Jul-2002").getTime(), 5.25);
102         toolTips.add("6D - 5.25");
103         dataSeries.add(sdf.parse("07-Jul-2002").getTime(), 5.31);
104         toolTips.add("7D - 5.31");
105         dataSeries.add(sdf.parse("08-Jul-2002").getTime(), 5.36);
106         toolTips.add("8D - 5.36");
107         XYSeriesCollection xyDataset = new XYSeriesCollection(dataSeries);
108         CustomXYToolTipGenerator ttg = new CustomXYToolTipGenerator();
109         ttg.addToolTipSeries(toolTips);
110
111         // Create the chart
112
StandardXYURLGenerator urlg = new StandardXYURLGenerator("xy_details.jsp");
113         ValueAxis timeAxis = new DateAxis("");
114         NumberAxis valueAxis = new NumberAxis("");
115         valueAxis.setAutoRangeIncludesZero(false); // override default
116
XYPlot plot = new XYPlot(xyDataset, timeAxis, valueAxis, null);
117         StandardXYItemRenderer sxyir = new StandardXYItemRenderer(
118             StandardXYItemRenderer.LINES + StandardXYItemRenderer.SHAPES,
119             ttg, urlg);
120         sxyir.setShapesFilled(true);
121         plot.setRenderer(sxyir);
122         JFreeChart chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, false);
123         chart.setBackgroundPaint(java.awt.Color.white);
124
125         // ****************************************************************************
126
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
127
// * Please note that commercial support and documentation is available from: *
128
// * *
129
// * http://www.object-refinery.com/jfreechart/support.html *
130
// * *
131
// * This is not only a great service for developers, but is a VERY IMPORTANT *
132
// * source of funding for the JFreeChart project. Please support us so that *
133
// * we can continue developing free software. *
134
// ****************************************************************************
135

136         // save it to an image
137
try {
138             ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());
139             File JavaDoc file1 = new File JavaDoc("xychart100.png");
140             ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
141
142             // write an HTML page incorporating the image with an image map
143
File JavaDoc file2 = new File JavaDoc("xychart100.html");
144             OutputStream JavaDoc out = new BufferedOutputStream JavaDoc(new FileOutputStream JavaDoc(file2));
145             PrintWriter JavaDoc writer = new PrintWriter JavaDoc(out);
146             writer.println("<HTML>");
147             writer.println("<HEAD><TITLE>JFreeChart Image Map Demo</TITLE></HEAD>");
148             writer.println("<BODY>");
149             ChartUtilities.writeImageMap(writer, "chart", info);
150             writer.println("<IMG SRC=\"xychart100.png\" "
151                            + "WIDTH=\"600\" HEIGHT=\"400\" BORDER=\"0\" USEMAP=\"#chart\">");
152             writer.println("</BODY>");
153             writer.println("</HTML>");
154             writer.close();
155
156         }
157         catch (IOException JavaDoc e) {
158             System.out.println(e.toString());
159         }
160         return;
161     }
162 }
163
Popular Tags