如何在JavaWeb中实现图形报表折线图?
本文实例讲述了JavaWeb实现图形报表折线图的方法。分享给大家供大家参考,具体如下:
步骤说明:
1. 导入log4j.jar,jfreechart-0.9.18.jar,jdom.jar,jcommon-0.9.3.jar四个jar包
2. 在某个包中写入Tuxin.Java类
package com.mengya.util; import java.awt.Color; import java.awt.Font; import java.io.PrintWriter; import javax.servlet.http.HttpSession; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartRenderingInfo; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.StandardLegend; import org.jfree.chart.axis.NumberAxis; import org.jfree.chart.entity.StandardEntityCollection; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.renderer.LineAndShapeRenderer; import org.jfree.chart.servlet.ServletUtilities; import org.jfree.data.DefaultCategoryDataset; public class Line { private DefaultCategoryDataset dataset = new DefaultCategoryDataset(); public void setValue(int sum, String line, String wfield) { dataset.addValue(sum, line, wfield); } public String generateLineChart(String title,String wfield, String hfield, HttpSession session, PrintWriter pw,int wPhoto,int hPhoto) { String filename = null; try { final JFreeChart chart = ChartFactory.createLineChart ( title, // 图表标题 wfield, // 横轴的显示标签 hfield, // 纵轴的显示标签 dataset, //数据集 PlotOrientation.VERTICAL, // 图表方向:水平、垂直 true, // 是否显示图例 true, // 是否生成提示工具 tooltips false // 是否生成URL链接 ); StandardLegend legend = (StandardLegend) chart.getLegend();//生成图例 legend.setDisplaySeriesShapes(true);//显示图例形状 legend.setShapeScaleX(1.5);//设置图例X轴的大小 legend.setShapeScaleY(1.5);//设置图例Y轴的大小 legend.setDisplaySeriesLines(true);//显示图示项的横线 //设置图片的背景色 chart.setBackgroundPaint(new java.awt.Color(189,235,255)); CategoryPlot plot = (CategoryPlot) chart.getPlot(); plot.setBackgroundPaint(new Color(239,251,255));//生成图片中墙体的背景色 plot.setRangeGridlinePaint(Color.black);//生成图片中格子线的颜色 // 没有数据时显示的消息 plot.setNoDataMessage("没有相关统计数据"); plot.setNoDataMessageFont(new Font("黑体", Font.CENTER_BASELINE, 16)); plot.setNoDataMessagePaint(Color.RED); NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//设置图中的刻度线的单位 rangeAxis.setAutoRangeIncludesZero(true);//强制在自动选择的数据范围中包含0 LineAndShapeRenderer renderer = (LineAndShapeRenderer) plot.getRenderer(); renderer.setDrawShapes(true);//折线的数据点根据分类使用不同的形状 renderer.setItemLabelsVisible(true);//显示每个点上的数据值的提示工具,数据标签是否可见 ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 filename = ServletUtilities.saveChartAsPNG(chart, wPhoto, hPhoto, info, session); ChartUtilities.writeImageMap(pw, filename, info); pw.flush(); } catch(Exception e) { e.printStackTrace(); } return filename; } }
3. 配置web.xml,在web.xml中添加如下内空:
<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>
4. 在要显示图片的jsp中添加内容:
<%@ page language="java" import="java.util.*,com.mengya.util.Line,com.mengya.bean.StuBean" pageEncoding="gbk"%> <%@page import="java.io.PrintWriter;"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>折线图</title> </head> <body> <% Line tt = new Line(); //第一条折线 List ar = (ArrayList) request.getAttribute("arr1"); for (int i = 0; i < ar.size(); i++) { StuBean bean = (StuBean) ar.get(i); tt.setValue((int) bean.getS_money(), "张明学个人消费比例图", bean.getS_month()+ ""); } //条二条折线 ar = (ArrayList) request.getAttribute("arr2"); for (int i = 0; i < ar.size(); i++) { StuBean bean = (StuBean) ar.get(i); tt.setValue((int) bean.getS_money(), "梦娅个人消费比例图", bean.getS_month()+ ""); } //说明:tt.setValue(int a,String b,String c) //其中的第二个参数b是该条折线的名字,每条折线的名字最好不要一样. String filename = tt.generateLineChart("个人消费比例图", "月 份", "费 用", session, new PrintWriter(out), 800, 550); String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; %> <P ALIGN="CENTER"> <img src="<%=graphURL %>" width=800 height=550 border=1 usemap="#<%=filename%>"> </body> </html>
希望本文所述对大家JSP程序设计有所帮助。
本文地址:http://www.45fan.com/bcdm/66018.html