KickJava   Java API By Example, From Geeks To Geeks.

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


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  * ImageMapDemo1.java
24  * ------------------
25  * (C) Copyright 2002, 2003, by Object Refinery Limited and Contributors.
26  *
27  * Original Author: David Gilbert (for Object Refinery Limited);
28  * Contributor(s): Richard Atkinson (richard_c_atkinson@ntlworld.com);
29  *
30  * $Id: ImageMapDemo1.java,v 1.16 2003/11/28 10:57:36 mungady Exp $
31  *
32  * Changes
33  * -------
34  * 26-Jun-2002 : Version 1 (DG);
35  * 05-Aug-2002 : Modified to demonstrate hrefs and alt tags in image map (RA);
36  * 10-Oct-2002 : Fixed errors reported by Checkstyle (DG);
37  *
38  */

39
40 package org.jfree.chart.demo;
41
42 import java.io.BufferedOutputStream JavaDoc;
43 import java.io.File JavaDoc;
44 import java.io.FileOutputStream JavaDoc;
45 import java.io.IOException JavaDoc;
46 import java.io.OutputStream JavaDoc;
47 import java.io.PrintWriter JavaDoc;
48
49 import org.jfree.chart.ChartFactory;
50 import org.jfree.chart.ChartRenderingInfo;
51 import org.jfree.chart.ChartUtilities;
52 import org.jfree.chart.JFreeChart;
53 import org.jfree.chart.axis.CategoryAxis;
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.StandardCategoryItemLabelGenerator;
58 import org.jfree.chart.plot.CategoryPlot;
59 import org.jfree.chart.plot.PlotOrientation;
60 import org.jfree.chart.renderer.BarRenderer;
61 import org.jfree.chart.urls.StandardCategoryURLGenerator;
62 import org.jfree.data.CategoryDataset;
63 import org.jfree.data.DatasetUtilities;
64
65 /**
66  * A demo showing how to create an HTML image map with JFreeChart.
67  *
68  * @author David Gilbert
69  */

70 public class ImageMapDemo1 {
71
72     /**
73      * Default constructor.
74      */

75     public ImageMapDemo1() {
76     }
77
78     /**
79      * Starting point for the demo.
80      *
81      * @param args ignored.
82      */

83     public static void main(String JavaDoc[] args) {
84
85         // create a chart
86
double[][] data = new double[][] {
87             {56.0, -12.0, 34.0, 76.0, 56.0, 100.0, 67.0, 45.0},
88             {37.0, 45.0, 67.0, 25.0, 34.0, 34.0, 100.0, 53.0},
89             {43.0, 54.0, 34.0, 34.0, 87.0, 64.0, 73.0, 12.0}
90         };
91         CategoryDataset dataset = DatasetUtilities.createCategoryDataset("Series ", "Type ", data);
92
93         JFreeChart chart = null;
94         boolean drilldown = true;
95
96         if (drilldown) {
97             CategoryAxis categoryAxis = new CategoryAxis("Category");
98             ValueAxis valueAxis = new NumberAxis("Value");
99             BarRenderer renderer = new BarRenderer();
100             renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
101             renderer.setItemURLGenerator(new StandardCategoryURLGenerator("bar_chart_detail.jsp"));
102             CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis, renderer);
103             plot.setOrientation(PlotOrientation.VERTICAL);
104             chart = new JFreeChart("Vertical Bar Chart", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
105         }
106         else {
107             chart = ChartFactory.createBarChart(
108                 "Vertical Bar Chart", // chart title
109
"Category", // domain axis label
110
"Value", // range axis label
111
dataset, // data
112
PlotOrientation.VERTICAL,
113                 true, // include legend
114
true,
115                 false
116             );
117         }
118         chart.setBackgroundPaint(java.awt.Color.white);
119
120         // ****************************************************************************
121
// * COMMERCIAL SUPPORT / JFREECHART DEVELOPER GUIDE *
122
// * Please note that commercial support and documentation is available from: *
123
// * *
124
// * http://www.object-refinery.com/jfreechart/support.html *
125
// * *
126
// * This is not only a great service for developers, but is a VERY IMPORTANT *
127
// * source of funding for the JFreeChart project. Please support us so that *
128
// * we can continue developing free software. *
129
// ****************************************************************************
130

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