读书人

运用JFreeChart生成XY轴折线图

发布时间: 2012-10-29 10:03:53 作者: rapoo

使用JFreeChart生成XY轴折线图

第一步:?

Java代码??运用JFreeChart生成XY轴折线图
  1. //生成折线图的处理类(其他DOMAIN类代码省略)?????
  2. public?class?CreateJFreeChartXYLine?{??
  3. ????//?保存为文件??
  4. ????public?static?void?saveAsFile(JFreeChart?chart,?String?outputPath,??
  5. ????????????int?weight,?int?height)?{??
  6. ????????FileOutputStream?out?=?null;??
  7. ????????try?{??
  8. ????????????File?outFile?=?new?File(outputPath);??
  9. ????????????if?(!outFile.getParentFile().exists())?{??
  10. ????????????????outFile.getParentFile().mkdirs();??
  11. ????????????}??
  12. ????????????out?=?new?FileOutputStream(outputPath);??
  13. ??
  14. ????????????//?保存为PNG??
  15. ????????????ChartUtilities.writeChartAsPNG(out,?chart,500,?400);??
  16. ????????????//?保存为JPEG??
  17. ????????????//?ChartUtilities.writeChartAsJPEG(out,?chart,?500,?400);??
  18. ????????????out.flush();??
  19. ????????}?catch?(FileNotFoundException?e)?{??
  20. ????????????e.printStackTrace();??
  21. ????????}?catch?(IOException?e)?{??
  22. ????????????e.printStackTrace();??
  23. ????????}?finally?{??
  24. ????????????if?(out?!=?null)?{??
  25. ????????????????try?{??
  26. ????????????????????out.close();??
  27. ????????????????}?catch?(IOException?e)?{??
  28. ????????????????????//?do?nothing??
  29. ????????????????}??
  30. ????????????}??
  31. ????????}??
  32. ????}??
  33. ??
  34. ????//?根据XYDataset创建JFreeChart对象??
  35. ????public?static?JFreeChart?createChart(XYDataset?dataset)?{??
  36. ????????//?创建JFreeChart对象:ChartFactory.createXYLineChart??
  37. ????????JFreeChart?jfreechart?=?ChartFactory.createTimeSeriesChart("折线模型图",??
  38. ????????????????"年份",?"数量",?dataset,?false,?true,?false);??
  39. ????????jfreechart.getTitle().setFont(new?Font("宋体",?Font.BOLD,?12));??
  40. ????????//?使用CategoryPlot设置各种参数。以下设置可以省略。??
  41. ????????XYPlot?plot?=?(XYPlot)?jfreechart.getPlot();??
  42. ????????//?背景色?透明度??
  43. ????????plot.setBackgroundAlpha(0.5f);??
  44. ????????//?前景色?透明度??
  45. ????????plot.setForegroundAlpha(0.5f);??
  46. ????????//?其它设置可以参考XYPlot类??
  47. ????????ValueAxis?categoryaxis?=?plot.getDomainAxis();?//?横轴上的??
  48. ????????categoryaxis.setPositiveArrowVisible(true);//?增加横轴的箭头??
  49. ????????plot.getRangeAxis().setPositiveArrowVisible(true);//?增加纵轴的箭头??
  50. ????????categoryaxis.setTickLabelFont(new?Font("宋体",?10,?12));//?设定字体、类型、字号??
  51. ????????DateAxis?axis?=?(DateAxis)?plot.getDomainAxis();??
  52. ????????axis.setDateFormatOverride(new?SimpleDateFormat("MM月"));??
  53. ????????NumberAxis?numberaxis?=?new?NumberAxis("统计报表");//侧面显示的标题??
  54. ????????numberaxis.setAutoRangeIncludesZero(false);??
  55. ????????plot.setRangeAxis(1,?numberaxis);??
  56. ????????plot.setDataset(1,?dataset);??
  57. ????????plot.mapDatasetToRangeAxis(1,?1);??
  58. ????????XYItemRenderer?xyitemrenderer?=?plot.getRenderer();??
  59. ????????StandardXYItemRenderer?standardxyitemrenderer1?=?new?StandardXYItemRenderer();??
  60. ????????standardxyitemrenderer1.setSeriesPaint(0,?Color.black);??
  61. ????????standardxyitemrenderer1.setSeriesPaint(0,?Color.black);??
  62. ????????standardxyitemrenderer1.setPlotLines(true);??
  63. ????????LegendTitle?legendtitle?=?new?LegendTitle(xyitemrenderer);??
  64. ????????LegendTitle?legendtitle1?=?new?LegendTitle(standardxyitemrenderer1);??
  65. ????????BlockContainer?blockcontainer?=?new?BlockContainer(??
  66. ????????????????new?BorderArrangement());??
  67. ????????blockcontainer.add(legendtitle,?RectangleEdge.LEFT);??
  68. ????????blockcontainer.add(legendtitle1,?RectangleEdge.RIGHT);//这两行代码可以控制位置??
  69. ????????blockcontainer.add(new?EmptyBlock(2000D,?0.0D));??
  70. ????????CompositeTitle?compositetitle?=?new?CompositeTitle(blockcontainer);??
  71. ????????compositetitle.setPosition(RectangleEdge.BOTTOM);//放置图形代表区域位置的代码??
  72. ????????jfreechart.addSubtitle(compositetitle);??
  73. ????????Font?font2?=?new?Font("宋体",?10,?16);?//?设定字体、类型、字号??
  74. ????????plot.getDomainAxis().setLabelFont(font2);//?相当于横轴或理解为X轴??
  75. ????????plot.getRangeAxis().setLabelFont(font2);//?相当于竖轴理解为Y轴??
  76. ????????return?jfreechart;??
  77. ????}??
  78. ??
  79. ????/**?
  80. ?????*?创建XYDataset对象?
  81. ?????*??
  82. ?????*/??
  83. ????public?static?XYDataset?createXYDataset()?{??
  84. ????????MyChartService?ms?=?new?MyChartService();??
  85. ????????//?XYSeriesCollection?xySeriesCollection?=?new?XYSeriesCollection();??
  86. ????????TimeSeriesCollection?timeseriescollection?=?new?TimeSeriesCollection();??
  87. ????????List<String>?names?=?ms.listall();??
  88. ????????for?(String?name?:?names)?{??
  89. ????????????System.out.println(name);??
  90. ????????????TimeSeries?timeseries?=?new?TimeSeries(name);??
  91. ????????????List<MyChart>?mcs?=?ms.FindByName(name);??
  92. ????????????for?(MyChart?mc?:?mcs)?{??
  93. ????????????????Calendar?cc?=?Calendar.getInstance();??
  94. ????????????????System.out.println(mc.getScore());??
  95. ????????????????cc.setTime(mc.getDate());??
  96. ????????????????timeseries.add(new?Month(cc.get(Calendar.MONTH),?cc??
  97. ????????????????????????.get(Calendar.YEAR)),?mc.getScore());??
  98. ????????????????//?xyseries.add(mc.getDate(),mc.getScore());??
  99. ??
  100. ????????????}??
  101. ????????????//?xySeriesCollection.addSeries(timeseries);??
  102. ????????????timeseriescollection.addSeries(timeseries);??
  103. ??
  104. ????????}??
  105. ??
  106. ????????return?timeseriescollection;??
  107. ????} ?

?
第二步:如果配合Struts1使用的话

Java代码??运用JFreeChart生成XY轴折线图
  1. request.setCharacterEncoding("gbk");???
  2. response.setContentType("image/jpeg;charset=gbk");???
  3. MyChartService?ms=new?MyChartService();???
  4. //步骤1:创建XYDataset对象(准备数据)?????
  5. ????????XYDataset?dataset?=CreateJFreeChartXYLine.createXYDataset();?????
  6. ????????//步骤2:根据Dataset?生成JFreeChart对象,以及做相应的设置?????
  7. ????????JFreeChart?freeChart?=CreateJFreeChartXYLine.createChart(dataset);?????
  8. ????????//步骤3:将JFreeChart对象输出到文件,Servlet输出流等?????
  9. ????????String?url=?"c:\\jfreechart\\lineXY.png";???
  10. ????????CreateJFreeChartXYLine.saveAsFile(freeChart,url,?900,?700);????
  11. ????????request.setAttribute("url",?url);???
  12. ????????request.setAttribute("lists",?ms.listall());???
  13. return?mapping.findForward("list"); ??

?
直接将路径发到JSP页面,从而达到显示图片效果

读书人网 >编程

热点推荐