JavaWeb实现图形报表折线图的方法
本文实例讲述了JavaWeb实现图形报表折线图的方法。分享给大家供大家参考,具体如下:
步骤说明:
1.导入log4j.jar,jfreechart-0.9.18.jar,jdom.jar,jcommon-0.9.3.jar四个jar包
2.在某个包中写入Tuxin.Java类
packagecom.mengya.util; importjava.awt.Color; importjava.awt.Font; importjava.io.PrintWriter; importjavax.servlet.http.HttpSession; importorg.jfree.chart.ChartFactory; importorg.jfree.chart.ChartRenderingInfo; importorg.jfree.chart.ChartUtilities; importorg.jfree.chart.JFreeChart; importorg.jfree.chart.StandardLegend; importorg.jfree.chart.axis.NumberAxis; importorg.jfree.chart.entity.StandardEntityCollection; importorg.jfree.chart.plot.CategoryPlot; importorg.jfree.chart.plot.PlotOrientation; importorg.jfree.chart.renderer.LineAndShapeRenderer; importorg.jfree.chart.servlet.ServletUtilities; importorg.jfree.data.DefaultCategoryDataset; publicclassLine { privateDefaultCategoryDatasetdataset=newDefaultCategoryDataset(); publicvoidsetValue(intsum,Stringline,Stringwfield) { dataset.addValue(sum,line,wfield); } publicStringgenerateLineChart(Stringtitle,Stringwfield,Stringhfield,HttpSessionsession,PrintWriterpw,intwPhoto,inthPhoto) { Stringfilename=null; try { finalJFreeChartchart=ChartFactory.createLineChart ( title,//图表标题 wfield,//横轴的显示标签 hfield,//纵轴的显示标签 dataset,//数据集 PlotOrientation.VERTICAL,//图表方向:水平、垂直 true,//是否显示图例 true,//是否生成提示工具tooltips false//是否生成URL链接 ); StandardLegendlegend=(StandardLegend)chart.getLegend();//生成图例 legend.setDisplaySeriesShapes(true);//显示图例形状 legend.setShapeScaleX(1.5);//设置图例X轴的大小 legend.setShapeScaleY(1.5);//设置图例Y轴的大小 legend.setDisplaySeriesLines(true);//显示图示项的横线 //设置图片的背景色 chart.setBackgroundPaint(newjava.awt.Color(189,235,255)); CategoryPlotplot=(CategoryPlot)chart.getPlot(); plot.setBackgroundPaint(newColor(239,251,255));//生成图片中墙体的背景色 plot.setRangeGridlinePaint(Color.black);//生成图片中格子线的颜色 //没有数据时显示的消息 plot.setNoDataMessage("没有相关统计数据"); plot.setNoDataMessageFont(newFont("黑体",Font.CENTER_BASELINE,16)); plot.setNoDataMessagePaint(Color.RED); NumberAxisrangeAxis=(NumberAxis)plot.getRangeAxis(); rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());//设置图中的刻度线的单位 rangeAxis.setAutoRangeIncludesZero(true);//强制在自动选择的数据范围中包含0 LineAndShapeRendererrenderer=(LineAndShapeRenderer)plot.getRenderer(); renderer.setDrawShapes(true);//折线的数据点根据分类使用不同的形状 renderer.setItemLabelsVisible(true);//显示每个点上的数据值的提示工具,数据标签是否可见 ChartRenderingInfoinfo=newChartRenderingInfo(new StandardEntityCollection()); //500是图片长度,300是图片高度 filename=ServletUtilities.saveChartAsPNG(chart,wPhoto,hPhoto,info,session); ChartUtilities.writeImageMap(pw,filename,info); pw.flush(); } catch(Exceptione) { e.printStackTrace(); } returnfilename; } }
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中添加内容:
<%@pagelanguage="java"import="java.util.*,com.mengya.util.Line,com.mengya.bean.StuBean"pageEncoding="gbk"%> <%@pageimport="java.io.PrintWriter;"%> <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN"> <html> <head> <title>折线图</title> </head> <body> <% Linett=newLine(); //第一条折线 Listar=(ArrayList)request.getAttribute("arr1"); for(inti=0;i<ar.size();i++){ StuBeanbean=(StuBean)ar.get(i); tt.setValue((int)bean.getS_money(),"张明学个人消费比例图",bean.getS_month()+""); } //条二条折线 ar=(ArrayList)request.getAttribute("arr2"); for(inti=0;i<ar.size();i++){ StuBeanbean=(StuBean)ar.get(i); tt.setValue((int)bean.getS_money(),"梦娅个人消费比例图",bean.getS_month()+""); } //说明:tt.setValue(inta,Stringb,Stringc) //其中的第二个参数b是该条折线的名字,每条折线的名字最好不要一样. Stringfilename=tt.generateLineChart("个人消费比例图","月份", "费用",session,newPrintWriter(out),800,550); StringgraphURL=request.getContextPath() +"/DisplayChart?filename="+filename; %> <PALIGN="CENTER"> <imgsrc="<%=graphURL%>"width=800height=550border=1usemap="#<%=filename%>"> </body> </html>
希望本文所述对大家JSP程序设计有所帮助。