如何在web Servlet中应用JFreeChart
JfreeChart在web开发中如何实现报表:
JfreeChart的下载链接地址 http://sourceforge.net/projects/jfreechart/files/
import java.sql.*;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import com.zhaoming.shopping.util.DB;
public class ShowProductSalesServlet extends HttpServlet
{
/**
*
*/
private static final long serialVersionUID = 1L;
private static DefaultCategoryDataset barDataset = new DefaultCategoryDataset();
private static DefaultPieDataset pieDataset = new DefaultPieDataset();
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException
{
req.setCharacterEncoding("GBK");
resp.setCharacterEncoding("GBK");
// 创建数据
getDataSet();
// 创建 JFreeChart 对象
//直方图
JFreeChart chart = ChartFactory.createBarChart("商品销量图", "商品", "销量",
barDataset, PlotOrientation.VERTICAL, true, true, false);
// 配置JFreeChart对象相关信息,如:字体大小,颜色,防止乱码
ShowProductSalesServlet.configFont(chart);
// 输出
//创建 Jfreechart对象 实现饼图
JFreeChart jfreechart = ChartFactory.createPieChart3D("pieChart", pieDataset, true, true, false);
//为了防止出现乱码,对于字体进行设置
jfreechart.getTitle().setFont(new Font("宋体", Font.CENTER_BASELINE, 20));//设置标题字体
PiePlot piePlot= (PiePlot) jfreechart.getPlot();//获取图表区域对象
piePlot.setLabelFont(new Font("宋体",Font.BOLD,10));
jfreechart.getLegend().setItemFont(new Font("微软雅黑",Font.BOLD,10));
//得到图片
FileOutputStream fos_jpg = null;
FileOutputStream fos_jpg2 = null;
try
{
//把图片存到硬盘的相对位置
fos_jpg = new FileOutputStream(
"F:\\svnTest\\webTest\\project\\Shopping2\\WebRoot\\images\\chart\\Bar.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg, 0.99f, chart, 800, 600,
null);
fos_jpg2 = new FileOutputStream(
"F:\\svnTest\\webTest\\project\\Shopping2\\WebRoot\\images\\chart\\Pie.jpg");
// 用工具把图象写到硬盘,支持两种格式,JPG,PNG,还支持MAP
ChartUtilities.writeChartAsJPEG(fos_jpg2, 0.99f, jfreechart, 600,
400, null);
this.getServletContext().getRequestDispatcher(
"/admin/ShowProductSalesChart.jsp").forward(req, resp);
fos_jpg.close();
fos_jpg2.close();
} catch (Exception e)
{
e.printStackTrace();
}
}
//连接数据库,拿到相应的数据,设置到对象中
public static void getDataSet()
{
// 创建数据
// 数据初始化
Connection conn = null;
ResultSet rs = null;
try
{
conn = DB.getConn();
String sql = "select p.name ,sum(pcont) from product p join salesitem si on ( p.id = si.productid) group by p.id";
//System.out.println("sales sql" + sql);
rs = DB.getRs(conn, sql);
while (rs.next())
{
barDataset.addValue(rs.getInt(2),"", rs.getString(1));
pieDataset.setValue(rs.getString(1), rs.getInt(2));
}
} catch (Exception e)
{
e.printStackTrace();
} finally
{
DB.close(rs);
DB.close(conn);
}
}
/**
* 对生成的直方图像进行设置
* @param chart
*/
public static void configFont(JFreeChart chart)
{
// 设置边框宽度
chart.setBorderStroke(new BasicStroke(1));
// 设置边框是否可见
chart.setBorderVisible(true);
// 设置边框着色
chart.setBorderPaint(Color.cyan);
// 设置背景颜色
// chart.setBackgroundPaint(Color.YELLOW);
// 配置字体
Font xfont = new Font("宋体", Font.CENTER_BASELINE, 20);// X轴
Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴
Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部
Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题
CategoryPlot plot = chart.getCategoryPlot();// 图形的绘制结构对象
// 数据轴网格线条颜色
plot.setRangeGridlinePaint(Color.BLUE);
// 数据轴网格线条笔触
plot.setRangeGridlineStroke(new BasicStroke(1.0f));
// 图片标题
chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
// 底部字体样式,防止乱码
chart.getLegend().setItemFont(kfont);
// X轴
CategoryAxis domainAxis = plot.getDomainAxis();
// 设置X轴标题字体
domainAxis.setLabelFont(xfont);
// 设置X轴字体
domainAxis.setTickLabelFont(xfont);
// 设置字体颜色
domainAxis.setTickLabelPaint(Color.BLUE);
// 横轴上的label斜显示
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
// 分类轴边距,同种类型之间的距离
// 这是分类之间的距离,和BAR与BAR之间的距离有差别
// domainAxis.setCategoryMargin(0.2f);
// 分类轴下(左)边距,就是离左边的距离
domainAxis.setLowerMargin(0.1);
// 分类轴下(右)边距,就是离最右边的距离
domainAxis.setUpperMargin(0.1);
// Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置Y轴标题字体
rangeAxis.setLabelFont(yfont);
// 设置Y轴字体
rangeAxis.setTickLabelFont(yfont);
// 字体颜色
rangeAxis.setLabelPaint(Color.RED);
// 设置Bar的颜色
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setSeriesPaint(0, Color.gray);
renderer.setSeriesPaint(1, Color.orange);
// 每个BAR之间的间隔
renderer.setItemMargin(0.0f);
// 每个BAR的最大宽度
// renderer.setMaximumBarWidth(0.5f);
}
}