81,122
社区成员




public class BarChart extends ServletUtilities {
protected static void createTempDir()
{
String tempDirName = "E:/myworkspace/st2/WebRoot/upload1";
if (tempDirName == null)
{
throw new RuntimeException("Temporary directory system property " + "(java.io.tmpdir) is null.");
}
// create the temporary directory if it doesn't exist
File tempDir = new File(tempDirName);
if (!tempDir.exists())
{
tempDir.mkdirs();
}
}
public static String saveChartAsPNG(JFreeChart chart, int width, int height,
ChartRenderingInfo info, HttpSession session) throws IOException
{
if (chart == null)
{
throw new IllegalArgumentException("Null 'chart' argument.");
}
createTempDir();
String prefix = ServletUtilities.getTempFilePrefix();
if (session == null)
{
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
File tempFile = File.createTempFile(prefix, ".png", new File("E:/myworkspace/st2/WebRoot/upload1"));
ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
if (session != null)
{
ServletUtilities.registerChartForDeletion(tempFile, session);
}
System.out.println("tempFile.getName()=" + tempFile.getName());
return tempFile.getName();
}
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page
import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities,com.test.jfreechart.BarChart"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form namespace="/">
<%
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);
JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd,
true, false, false);
String fileName = "";
fileName = BarChart
.saveChartAsPNG(chart, 800, 600, session);
System.out.println("fileName=" + fileName);
String url = request.getContextPath() + "/DisplayChart?filename="
+ fileName;
System.out.println("url =" + url);
%>
<img src="<%=url%>" width="500" height="300">
</s:form>
</body>
</html>
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>
org.jfree.chart.servlet.DisplayChart
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/DisplayChart</url-pattern>
</servlet-mapping>
saveChartAsPNG(JFreeChart chart, int width,
int height, ChartRenderingInfo info, HttpSession session)
fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);
double[] data = {85, 156, 179.5, 211, 123};
//The labels for the bar chart
String[] labels = {"Mon", "Tue", "Wed", "Thu", "Fri"};
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
for (int i = 0; i < data.length; i++) {
dataset.addValue(data[i], null, labels[i]);
}
JFreeChart chart = ChartFactory.createBarChart3D("Weekly Server Load", "Work Week 25", "MBytes", dataset, PlotOrientation.VERTICAL, false, false, false);
chart.setBackgroundPaint(new Color(0xE1E1E1));
CategoryPlot plot = chart.getCategoryPlot();
// 设置Y轴显示整数
NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
CategoryAxis domainAxis = plot.getDomainAxis();
//设置距离图片左端距离
domainAxis.setLowerMargin(0.05);
BarRenderer3D renderer = new BarRenderer3D();
//设置柱的颜色
renderer.setSeriesPaint(0, new Color(0xff00));
plot.setRenderer(renderer);
//这里是保存图片到路径:默认是保存到tomcat的临时文件夹,你用我刚刚保存方法!
//String filename = ServletUtilities.saveChartAsPNG(chart, 300, 280, null, session);
public static String saveChartAsPNG(JFreeChart chart, int width,
int height, ChartRenderingInfo info, HttpSession session)
throws IOException {
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}
String prefix = ServletUtilities.getTempFilePrefix();
if (session == null) {
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
prefix = prefix.replaceAll("jfreechart", "chart");
File tempFile = File.createTempFile(prefix, ".png",
new File(ServletActionContext.getServletContext().getRealPath(
"/upload")));
ChartUtilities.saveChartAsPNG(tempFile, chart, width, height, info);
if (session != null) {
ServletUtilities.registerChartForDeletion(tempFile, session);
}
return tempFile.getName();
}
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%>
<%@ page import="org.jfree.chart.ChartFactory"%>
<%@ page
import="org.jfree.chart.JFreeChart,org.jfree.chart.servlet.ServletUtilities,com.test.jfreechart.BarChart"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<s:form namespace="/">
<%
DefaultPieDataset dpd = new DefaultPieDataset();
dpd.setValue("管理人员", 25);
dpd.setValue("市场人员", 25);
dpd.setValue("开发人员", 45);
dpd.setValue("其他人员", 10);
JFreeChart chart = ChartFactory.createPieChart("某公司组织结构图", dpd,
true, false, false);
String fileName = "";
fileName = BarChart.saveChartAsPNG(chart, 800, 600, session);
System.out.println("fileName=" + fileName);
String url = request.getContextPath() + "/DisplayChart?filename="
+ fileName;
System.out.println("url =" + url);
%>
<img src="<%=url%>" width="500" height="300">
</s:form>
</body>
</html>
package com.sunun.util.jfreechart;
import java.io.File;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.servlet.ServletUtilities;
public class JfreeChartUtilities extends ServletUtilities {
public static String saveChartAsPNG(JFreeChart chart, int width,
int height, HttpServletRequest request) throws IOException {
if (chart == null) {
throw new IllegalArgumentException("Null 'chart' argument.");
}
String prefix = ServletUtilities.getTempFilePrefix();
if (request.getSession() == null) {
prefix = ServletUtilities.getTempOneTimeFilePrefix();
}
prefix = prefix.replaceAll("jfreechart", "chart");
File tempFile = File.createTempFile(prefix, ".jpeg", new File(request
.getRealPath("images/imgchart")));
ChartUtilities.saveChartAsJPEG(tempFile, chart, width, height);
if (request.getSession() != null) {
ServletUtilities.registerChartForDeletion(tempFile, request
.getSession());
}
return tempFile.getName();
}
}
JfreeChartUtilities.saveChartAsPNG(chart, 600, 400,
request);
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(print);// 输出MAP信息
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 300,info, session);
String filename = new ChartUtil1().createLineImg(session,w);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;
<servlet>
<servlet-name>DisplayChart</servlet-name>
<servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class>
</servlet>
<!-- Action Servlet Mapping -->
<servlet-mapping>
<servlet-name>DisplayChart</servlet-name>
<url-pattern>/servlet/DisplayChart</url-pattern>
</servlet-mapping>
StandardEntityCollection sec = new StandardEntityCollection();
ChartRenderingInfo info = new ChartRenderingInfo(sec);
PrintWriter w = new PrintWriter(print);// 输出MAP信息
String filename = ServletUtilities.saveChartAsPNG(chart, 800, 300,info, session);
.jsp
[code=HTML]
String filename = new ChartUtil1().createLineImg(session,w);
String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename;