📄 chartservlet.java
字号:
/*
* ChartServlet.java
*
* Created on 08-Feb-2006
*
* JWebChart
* Copyright (C) 2006 by Luke Trevorrow (www.axioma.org.uk)
*/
package uk.org.axioma.servlet;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.servlet.ChartDeleter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.Second;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.xml.DatasetReader;
import org.jfree.data.xy.XYSeries;
import uk.org.axioma.chart.CategoryChart;
import uk.org.axioma.chart.MeterChart;
import uk.org.axioma.chart.PieChart;
import uk.org.axioma.chart.XYChart;
/**
* <p>
* The ChartServlet object
* </p>
* @author <a href="mailto:luke.trevorrow@gmail.com">Luke Trevorrow</a>
* @version 0.3
*
*/
public class ChartServlet extends HttpServlet {
/**
* autogenerated serialVersionUID
*/
private static final long serialVersionUID = 6748895383151504466L;
/**
*
*/
public ChartServlet() {
super();
}
/**
* Init method.
*
* @throws ServletException never.
*/
public void init() throws ServletException {
return;
}
/**
* Service method.
*
* @param request the request.
* @param response the response.
*
* @throws ServletException ??.
* @throws IOException ??.
*/
public void service(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession();
Enumeration en = request.getParameterNames();
Map<String,String[]> map = new HashMap<String,String[]>(request.getParameterMap());
JFreeChart jfc = null;
String series = null;
int width = 300, height = 200;
/*PrintWriter out = response.getWriter();
response.setContentType("text/html");
out.println("<H4>");*/
// go through the hash map and extract the core values.
// extract the rest in the createChart methods
if (map.containsKey("Series")) {
// use the getParameter method rather than the map to get the string array as I am sure this will be faster
series = request.getParameter("Series");
map.remove("Series");
//out.println("<br>Graph : " + series);
}
if (map.containsKey("Width")) {
width = Integer.valueOf(request.getParameter("Width")).intValue();
map.remove("Width");
//out.println("<br>Width : " + width);
}
if (map.containsKey("Height")) {
height = Integer.valueOf(request.getParameter("Height")).intValue();
map.remove("Height");
//out.println("<br>Height : " + height);
}
// create the chart
if (series.equalsIgnoreCase("Pie")) {
jfc = createPie(map, request);
} else if (series.equalsIgnoreCase("Category")) {
jfc = createCategory(map, request);
} else if (series.equalsIgnoreCase("XY")) {
jfc = createXY(map, request);
} else if (series.equalsIgnoreCase("Meter")) {
jfc = createMeter(map, request);
}
// save the image for display
String filename = ServletUtilities.saveChartAsPNG(jfc,width,height,null);
//out.println(filename);
// Check the file exists
File file = new File(System.getProperty("java.io.tmpdir"), filename);
if (!file.exists()) {
throw new ServletException(
"File '" + file.getAbsolutePath() + "' does not exist"
);
}
// Check that the graph being served was created by the current user
// or that it begins with "public"
boolean isChartInUserList = false;
ChartDeleter chartDeleter = (ChartDeleter) session.getAttribute(
"JFreeChart_Deleter"
);
if (chartDeleter != null) {
isChartInUserList = chartDeleter.isChartAvailable(filename);
}
boolean isChartPublic = false;
if (filename.length() >= 6) {
if (filename.substring(0, 6).equals("public")) {
isChartPublic = true;
}
}
boolean isOneTimeChart = false;
if (filename.startsWith(ServletUtilities.getTempOneTimeFilePrefix())) {
isOneTimeChart = true;
}
if (isChartInUserList || isChartPublic || isOneTimeChart) {
// Serve it up
ServletUtilities.sendTempFile(file, response);
if (isOneTimeChart) {
file.delete();
}
} else {
throw new ServletException("Chart image not found");
}
// For keys of a map
//for (Iterator<String> it=map.keySet().iterator(); it.hasNext(); ) {
//Object key = it.next();
//out.println("Key : " + key);
//}
// just return the filename to the browser for now
//out.println("</H4>");
//out.println("<TR>");
//out.println("<TH CLASS=\"data\">");
//out.println("filename");
//out.println("</TH>");
//out.println("</TR>");
//out.print("<TD CLASS=\"data\">");
//out.println(filename);
//out.println("</TD>");
}
/**
* Create meter chart method.
* @param data which can consist of elements such as:
* <ul>
* <li> Type - the type of chart: either Chord, Pie, Circle, RAGChord, RAGPie, or RAGCircle.</li>
* <li> Title - the title of the chart.</li>
* <li> Value - the value range for the meter.</li>
* <li> Units - the unit of measure for the meter.</li>
* <li> Range - the range for the meter.</li>
* <li> Interval - the interval ranges for the legend. Format is name=startval,endval, eg: Low=0,70</li>
* </ul>
* @param req which is the Http Servlet Request
*
* @return JFreeChart
* @throws ServletException
*
*/
private JFreeChart createMeter(Map<String, String[]> data, HttpServletRequest req)
throws ServletException {
MeterChart mc = new MeterChart();
// what type?
if (data.containsKey("Type")) {
String type = req.getParameter("Type");
if (type.equalsIgnoreCase("Chord")) {
mc.type = MeterChart.TYPECHORD;
} else if (type.equalsIgnoreCase("Circle")) {
mc.type = MeterChart.TYPECIRCLE;
} else if (type.equalsIgnoreCase("Pie")) {
mc.type = MeterChart.TYPEPIE;
} else if (type.equalsIgnoreCase("RAGChord")) {
mc.type = MeterChart.TYPERAGCHORD;
} else if (type.equalsIgnoreCase("RAGCircle")) {
mc.type = MeterChart.TYPERAGCIRCLE;
} else if (type.equalsIgnoreCase("RAGPie")) {
mc.type = MeterChart.TYPERAGPIE;
}
data.remove("Type");
}
// set the title for the Pie Chart
if (data.containsKey("Title")) {
mc.title = req.getParameter("Title");
data.remove("Title");
}
// set the units of measure
if (data.containsKey("Units")) {
mc.units = req.getParameter("Units");
data.remove("Units");
}
// set the range
if (data.containsKey("Range")) {
mc.range = req.getParameter("Range");
data.remove("Range");
}
// set the value
if (data.containsKey("Value")) {
mc.dataset.setValue(Double.valueOf(req.getParameter("Value")));
data.remove("Value");
}
mc.interval = data;
//MeterChart.out = out;
// create the chart
JFreeChart chart = mc.getChart();
return chart;
}
/**
* Create pie chart method.
*
* @param data which can consist of elements such as:
* <ul>
* <li> Type - the type of chart: either 2D, 3D, or Ring.</li>
* <li> Title - the title of the chart.</li>
* <li> XMLFile - the XML file name if applicable.</li>
* <li> data - the data to plot. Format is name=value&name=value, eg: Fish=70&Chips=66.</li>
* </ul>
* @param req which is the Http Servlet Request
*
* @return JFreeChart
* @throws ServletException
*
*/
private JFreeChart createPie(Map<String, String[]> data, HttpServletRequest req)
throws ServletException {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -