运用JFreeChart生成XY轴折线图
使用JFreeChart生成XY轴折线图第一步:?Java代码??//生成折线图的处理类(其他DOMAIN类代码省略)?????public
使用JFreeChart生成XY轴折线图
第一步:?
Java代码??

- //生成折线图的处理类(其他DOMAIN类代码省略)?????
- public?class?CreateJFreeChartXYLine?{??
- ????//?保存为文件??
- ????public?static?void?saveAsFile(JFreeChart?chart,?String?outputPath,??
- ????????????int?weight,?int?height)?{??
- ????????FileOutputStream?out?=?null;??
- ????????try?{??
- ????????????File?outFile?=?new?File(outputPath);??
- ????????????if?(!outFile.getParentFile().exists())?{??
- ????????????????outFile.getParentFile().mkdirs();??
- ????????????}??
- ????????????out?=?new?FileOutputStream(outputPath);??
- ??
- ????????????//?保存为PNG??
- ????????????ChartUtilities.writeChartAsPNG(out,?chart,500,?400);??
- ????????????//?保存为JPEG??
- ????????????//?ChartUtilities.writeChartAsJPEG(out,?chart,?500,?400);??
- ????????????out.flush();??
- ????????}?catch?(FileNotFoundException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?catch?(IOException?e)?{??
- ????????????e.printStackTrace();??
- ????????}?finally?{??
- ????????????if?(out?!=?null)?{??
- ????????????????try?{??
- ????????????????????out.close();??
- ????????????????}?catch?(IOException?e)?{??
- ????????????????????//?do?nothing??
- ????????????????}??
- ????????????}??
- ????????}??
- ????}??
- ??
- ????//?根据XYDataset创建JFreeChart对象??
- ????public?static?JFreeChart?createChart(XYDataset?dataset)?{??
- ????????//?创建JFreeChart对象:ChartFactory.createXYLineChart??
- ????????JFreeChart?jfreechart?=?ChartFactory.createTimeSeriesChart("折线模型图",??
- ????????????????"年份",?"数量",?dataset,?false,?true,?false);??
- ????????jfreechart.getTitle().setFont(new?Font("宋体",?Font.BOLD,?12));??
- ????????//?使用CategoryPlot设置各种参数。以下设置可以省略。??
- ????????XYPlot?plot?=?(XYPlot)?jfreechart.getPlot();??
- ????????//?背景色?透明度??
- ????????plot.setBackgroundAlpha(0.5f);??
- ????????//?前景色?透明度??
- ????????plot.setForegroundAlpha(0.5f);??
- ????????//?其它设置可以参考XYPlot类??
- ????????ValueAxis?categoryaxis?=?plot.getDomainAxis();?//?横轴上的??
- ????????categoryaxis.setPositiveArrowVisible(true);//?增加横轴的箭头??
- ????????plot.getRangeAxis().setPositiveArrowVisible(true);//?增加纵轴的箭头??
- ????????categoryaxis.setTickLabelFont(new?Font("宋体",?10,?12));//?设定字体、类型、字号??
- ????????DateAxis?axis?=?(DateAxis)?plot.getDomainAxis();??
- ????????axis.setDateFormatOverride(new?SimpleDateFormat("MM月"));??
- ????????NumberAxis?numberaxis?=?new?NumberAxis("统计报表");//侧面显示的标题??
- ????????numberaxis.setAutoRangeIncludesZero(false);??
- ????????plot.setRangeAxis(1,?numberaxis);??
- ????????plot.setDataset(1,?dataset);??
- ????????plot.mapDatasetToRangeAxis(1,?1);??
- ????????XYItemRenderer?xyitemrenderer?=?plot.getRenderer();??
- ????????StandardXYItemRenderer?standardxyitemrenderer1?=?new?StandardXYItemRenderer();??
- ????????standardxyitemrenderer1.setSeriesPaint(0,?Color.black);??
- ????????standardxyitemrenderer1.setSeriesPaint(0,?Color.black);??
- ????????standardxyitemrenderer1.setPlotLines(true);??
- ????????LegendTitle?legendtitle?=?new?LegendTitle(xyitemrenderer);??
- ????????LegendTitle?legendtitle1?=?new?LegendTitle(standardxyitemrenderer1);??
- ????????BlockContainer?blockcontainer?=?new?BlockContainer(??
- ????????????????new?BorderArrangement());??
- ????????blockcontainer.add(legendtitle,?RectangleEdge.LEFT);??
- ????????blockcontainer.add(legendtitle1,?RectangleEdge.RIGHT);//这两行代码可以控制位置??
- ????????blockcontainer.add(new?EmptyBlock(2000D,?0.0D));??
- ????????CompositeTitle?compositetitle?=?new?CompositeTitle(blockcontainer);??
- ????????compositetitle.setPosition(RectangleEdge.BOTTOM);//放置图形代表区域位置的代码??
- ????????jfreechart.addSubtitle(compositetitle);??
- ????????Font?font2?=?new?Font("宋体",?10,?16);?//?设定字体、类型、字号??
- ????????plot.getDomainAxis().setLabelFont(font2);//?相当于横轴或理解为X轴??
- ????????plot.getRangeAxis().setLabelFont(font2);//?相当于竖轴理解为Y轴??
- ????????return?jfreechart;??
- ????}??
- ??
- ????/**?
- ?????*?创建XYDataset对象?
- ?????*??
- ?????*/??
- ????public?static?XYDataset?createXYDataset()?{??
- ????????MyChartService?ms?=?new?MyChartService();??
- ????????//?XYSeriesCollection?xySeriesCollection?=?new?XYSeriesCollection();??
- ????????TimeSeriesCollection?timeseriescollection?=?new?TimeSeriesCollection();??
- ????????List<String>?names?=?ms.listall();??
- ????????for?(String?name?:?names)?{??
- ????????????System.out.println(name);??
- ????????????TimeSeries?timeseries?=?new?TimeSeries(name);??
- ????????????List<MyChart>?mcs?=?ms.FindByName(name);??
- ????????????for?(MyChart?mc?:?mcs)?{??
- ????????????????Calendar?cc?=?Calendar.getInstance();??
- ????????????????System.out.println(mc.getScore());??
- ????????????????cc.setTime(mc.getDate());??
- ????????????????timeseries.add(new?Month(cc.get(Calendar.MONTH),?cc??
- ????????????????????????.get(Calendar.YEAR)),?mc.getScore());??
- ????????????????//?xyseries.add(mc.getDate(),mc.getScore());??
- ??
- ????????????}??
- ????????????//?xySeriesCollection.addSeries(timeseries);??
- ????????????timeseriescollection.addSeries(timeseries);??
- ??
- ????????}??
- ??
- ????????return?timeseriescollection;??
- ????} ?
?
第二步:如果配合Struts1使用的话
Java代码??

- request.setCharacterEncoding("gbk");???
- response.setContentType("image/jpeg;charset=gbk");???
- MyChartService?ms=new?MyChartService();???
- //步骤1:创建XYDataset对象(准备数据)?????
- ????????XYDataset?dataset?=CreateJFreeChartXYLine.createXYDataset();?????
- ????????//步骤2:根据Dataset?生成JFreeChart对象,以及做相应的设置?????
- ????????JFreeChart?freeChart?=CreateJFreeChartXYLine.createChart(dataset);?????
- ????????//步骤3:将JFreeChart对象输出到文件,Servlet输出流等?????
- ????????String?url=?"c:\\jfreechart\\lineXY.png";???
- ????????CreateJFreeChartXYLine.saveAsFile(freeChart,url,?900,?700);????
- ????????request.setAttribute("url",?url);???
- ????????request.setAttribute("lists",?ms.listall());???
- return?mapping.findForward("list"); ??
?
直接将路径发到JSP页面,从而达到显示图片效果