📄 imagemapdemo8.java
字号:
/* ------------------
* ImageMapDemo8.java
* ------------------
* (C) Copyright 2004, 2005, by Object Refinery Limited.
*
*/
package demo;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.imagemap.ImageMapUtilities;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.data.category.IntervalCategoryDataset;
import org.jfree.data.gantt.Task;
import org.jfree.data.gantt.TaskSeries;
import org.jfree.data.gantt.TaskSeriesCollection;
import org.jfree.data.time.SimpleTimePeriod;
/**
* This demo shows an image map created from a Gantt chart.
*/
public class ImageMapDemo8 {
/**
* Creates a new demo.
*/
public ImageMapDemo8() {
super();
}
/**
* Creates a sample dataset for a Gantt chart.
*
* @return The dataset.
*/
public static IntervalCategoryDataset createDataset() {
TaskSeries s1 = new TaskSeries("Scheduled");
s1.add(new Task("Write Proposal",
new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
date(5, Calendar.APRIL, 2001))));
s1.add(new Task("Obtain Approval",
new SimpleTimePeriod(date(9, Calendar.APRIL, 2001),
date(9, Calendar.APRIL, 2001))));
s1.add(new Task("Requirements Analysis",
new SimpleTimePeriod(date(10, Calendar.APRIL, 2001),
date(5, Calendar.MAY, 2001))));
s1.add(new Task("Design Phase",
new SimpleTimePeriod(date(6, Calendar.MAY, 2001),
date(30, Calendar.MAY, 2001))));
s1.add(new Task("Design Signoff",
new SimpleTimePeriod(date(2, Calendar.JUNE, 2001),
date(2, Calendar.JUNE, 2001))));
s1.add(new Task("Alpha Implementation",
new SimpleTimePeriod(date(3, Calendar.JUNE, 2001),
date(31, Calendar.JULY, 2001))));
s1.add(new Task("Design Review",
new SimpleTimePeriod(date(1, Calendar.AUGUST, 2001),
date(8, Calendar.AUGUST, 2001))));
s1.add(new Task("Revised Design Signoff",
new SimpleTimePeriod(date(10, Calendar.AUGUST, 2001),
date(10, Calendar.AUGUST, 2001))));
s1.add(new Task("Beta Implementation",
new SimpleTimePeriod(date(12, Calendar.AUGUST, 2001),
date(12, Calendar.SEPTEMBER, 2001))));
s1.add(new Task("Testing",
new SimpleTimePeriod(date(13, Calendar.SEPTEMBER, 2001),
date(31, Calendar.OCTOBER, 2001))));
s1.add(new Task("Final Implementation",
new SimpleTimePeriod(date(1, Calendar.NOVEMBER, 2001),
date(15, Calendar.NOVEMBER, 2001))));
s1.add(new Task("Signoff",
new SimpleTimePeriod(date(28, Calendar.NOVEMBER, 2001),
date(30, Calendar.NOVEMBER, 2001))));
final TaskSeries s2 = new TaskSeries("Actual");
s2.add(new Task("Write Proposal",
new SimpleTimePeriod(date(1, Calendar.APRIL, 2001),
date(5, Calendar.APRIL, 2001))));
s2.add(new Task("Obtain Approval",
new SimpleTimePeriod(date(9, Calendar.APRIL, 2001),
date(9, Calendar.APRIL, 2001))));
s2.add(new Task("Requirements Analysis",
new SimpleTimePeriod(date(10, Calendar.APRIL, 2001),
date(15, Calendar.MAY, 2001))));
s2.add(new Task("Design Phase",
new SimpleTimePeriod(date(15, Calendar.MAY, 2001),
date(17, Calendar.JUNE, 2001))));
s2.add(new Task("Design Signoff",
new SimpleTimePeriod(date(30, Calendar.JUNE, 2001),
date(30, Calendar.JUNE, 2001))));
s2.add(new Task("Alpha Implementation",
new SimpleTimePeriod(date(1, Calendar.JULY, 2001),
date(12, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Design Review",
new SimpleTimePeriod(date(12, Calendar.SEPTEMBER, 2001),
date(22, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Revised Design Signoff",
new SimpleTimePeriod(date(25, Calendar.SEPTEMBER, 2001),
date(27, Calendar.SEPTEMBER, 2001))));
s2.add(new Task("Beta Implementation",
new SimpleTimePeriod(date(27, Calendar.SEPTEMBER, 2001),
date(30, Calendar.OCTOBER, 2001))));
s2.add(new Task("Testing",
new SimpleTimePeriod(date(31, Calendar.OCTOBER, 2001),
date(17, Calendar.NOVEMBER, 2001))));
s2.add(new Task("Final Implementation",
new SimpleTimePeriod(date(18, Calendar.NOVEMBER, 2001),
date(5, Calendar.DECEMBER, 2001))));
s2.add(new Task("Signoff",
new SimpleTimePeriod(date(10, Calendar.DECEMBER, 2001),
date(11, Calendar.DECEMBER, 2001))));
TaskSeriesCollection collection = new TaskSeriesCollection();
collection.add(s1);
collection.add(s2);
return collection;
}
/**
* Utility method for creating <code>Date</code> objects.
*
* @param day the date.
* @param month the month.
* @param year the year.
*
* @return a date.
*/
private static Date date(int day, int month, int year) {
Calendar calendar = Calendar.getInstance();
calendar.set(year, month, day);
Date result = calendar.getTime();
return result;
}
/**
* Creates a chart.
*
* @param dataset the dataset.
*
* @return The chart.
*/
private static JFreeChart createChart(IntervalCategoryDataset dataset) {
JFreeChart chart = ChartFactory.createGanttChart(
"Gantt Chart Demo", // chart title
"Task", // domain axis label
"Date", // range axis label
dataset, // data
true, // include legend
true, // tooltips
true // urls
);
CategoryPlot plot = (CategoryPlot) chart.getPlot();
CategoryAxis axis = plot.getDomainAxis();
axis.setMaximumCategoryLabelWidthRatio(10.0f);
axis.addCategoryLabelToolTip("Obtain Approval", "Testing 123");
return chart;
}
/**
* Starting point for the demonstration application.
*
* @param args ignored.
*/
public static void main(String[] args) {
JFreeChart chart = createChart(createDataset());
// save it to an image
try {
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection()
);
File file1 = new File("ImageMapDemo8.png");
ChartUtilities.saveChartAsPNG(file1, chart, 600, 400, info);
// write an HTML page incorporating the image with an image map
File file2 = new File("ImageMapDemo8.html");
OutputStream out = new BufferedOutputStream(
new FileOutputStream(file2)
);
PrintWriter writer = new PrintWriter(out);
writer.println("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"");
writer.println("\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">");
writer.println("<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"en\" xml:lang=\"en\">");
writer.println(
"<head><title>JFreeChart Image Map Demo 8</title></head>"
);
writer.println("<body><p>");
ImageMapUtilities.writeImageMap(writer, "chart", info);
writer.println(
"<img src=\"ImageMapDemo8.png\" "
+ "width=\"600\" height=\"400\" usemap=\"#chart\" alt=\"ImageMapDemo8.png\"/>"
);
writer.println("</p></body>");
writer.println("</html>");
writer.close();
}
catch (IOException e) {
System.out.println(e.toString());
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -