以前用JFreeChart的时候每次都需要修改一些显示样式和代码前段时间项目组要用于是就进行了一次封装把几个常用的样式都调整好了包括三种常用的图表类型都封装到一起了项目组的其他人员只要处理好要显示的数据就行了 webxml设置 <servlet> <servletname>DisplayChart</servletname> <servletclass> orgjfreechartservletDisplayChart </servletclass> </servlet> <servletmapping> <servletname>DisplayChart</servletname> <urlpattern>/servlet/DisplayChart</urlpattern> </servletmapping> 柱状图封装和设置 public static JFreeChart getLineChart(String title String xSubject String ySubject CategoryDataset dataset) { JFreeChart chart = ChartFactorycreateLineChart(title xSubject ySubject dataset PlotOrientationVERTICAL true true false); LineRendererD renderer = new LineRendererD(); renderersetItemLabelGenerator(new StandardCategoryItemLabelGenerator()); renderersetItemLabelsVisible(true); renderersetPositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchorOUTSIDE TextAnchorBOTTOM_CENTER)); CategoryPlot plot = chartgetCategoryPlot(); CategoryAxis domainAxis = plotgetDomainAxis(); //domainAxissetVerticalCategoryLabels(false); plotsetDomainAxis(domainAxis); ValueAxis rangeAxis = plotgetRangeAxis(); //设置最高的一个 Item 与图片顶端的距离 rangeAxissetUpperMargin(); //设置最低的一个 Item 与图片底端的距离 rangeAxissetLowerMargin(); plotsetRangeAxis(rangeAxis); plotsetRenderer(renderer); //设置透明度 plotsetForegroundAlpha(f); //设置背景透明度 plotsetBackgroundAlpha(f); //设置背景颜色 chartsetBackgroundPaint(new Color()); chartsetBackgroundImageAlpha(f); return chart; } 调用代码 public JFreeChart getSingleChart(String chartType) { //构造数据 double[][] data = new double[][]; for(int i = ; i < ; i ++) { data[][i] = Mathround(Mathrandom()*); } //根据类型显示不同类型图表 String[] rowKeys = new String[]{Rule}; String[] columnKeys = new String[]{日日日日日日日}; CategoryDataset dataset = DatasetUtilitiescreateCategoryDataset(rowKeys columnKeys data); if(chartTypeequals(pole)) return comutilMyChartFactorygetBarChart(校验规则统计 日期 案例(个) dataset); else if(chartTypeequals(line)) return comutilMyChartFactorygetLineChart(校验规则统计 日期 案例(个) dataset); else //if(chartTypeequals(area)) return comutilMyChartFactorygetAreaChart(校验规则统计 日期 案例(个) dataset); } 页面代码 <%@ page contentType=text/html; charset=gb language=java errorPage=%> <%@ page import=orgjfreechart*orgjfreechartservletServletUtilities%> <jsp:useBean scope=page id=chartTest class=testChartTest /> <% String chartType = requestgetParameter(chartType); if(chartType == null) chartType = line; JFreeChart chart = chartTestgetSingleChart(chartType); String filename = ; String graphURL = /images/nodatagif; if(chart != null) { filename = ServletUtilitiessaveChartAsPNG(chart null session); graphURL = requestgetContextPath() + /servlet/DisplayChart?filename= + filename; } %> <HTML> <HEAD> <title></title> <LINK type=text/css rel=stylesheet> </HEAD> <body bottomMargin= leftMargin= topMargin= rightMargin=> <form name=myform method=post action=> <table width=% border= align=center cellpadding= cellspacing=> <tr> <td valign=top> <TABLE class=td cellSpacing= cellPadding= border=> <TR> <TD class=tttable colspan=> <TABLE id=Table cellSpacing= cellPadding= width=% align=right border=> <TR> <TD vAlign=middle noWrap> <TABLE id=Table cellSpacing= cellPadding= width=% border=> <TR> <TD noWrap align=center noWrap> 图表类型 </TD> <TD noWrap> <select name=chartType> <option value=line <%if(chartTypeequals(line))outprint(selected);%>>折线图</option> <option value=pole <%if(chartTypeequals(pole))outprint(selected);%>>柱状图</option> <option value=area <%if(chartTypeequals(area))outprint(selected);%>>面积图</option> </select> </TD> <TD> <a # onclick=chkFrm();> <img src=/images/btnQuerygif border= align=absmiddle></a> </TD> </TR> </TABLE> </TD> </TR> </TABLE> </TD> </TR> <TR> <TD class=tttable colspan=> <table width=% border= class=mainFunctionTable cellpadding= cellspacing=> <tr height=> <td> <img src=<%=graphURL%> border= usemap=#<%=filename%> align=center> </td> </tr> </table> </TD> </TR> </TABLE> </td> </tr> </table> </form> <script> function chkFrm() { documentmyformsubmit(); } </script> </body> </HTML> |