Struts2整合JFreeChart图表

来源:互联网 发布:shell脚本语言入门知乎 编辑:程序博客网 时间:2024/04/25 15:57


本例源代码:http://download.csdn.net/source/852878


首先去官方网站http://www.jfree.org/jfreechart/download.html,将对应的资源包:jfreechart-1.0.11.zip、jfreechart-1.0.11-javadocs.zip、jcommon-1.0.14.zip下载下来,并配置到项目中。

本例源代码:http://download.csdn.net/source/852878

生成3D饼图

  1. public class PieChartDemo
  2. {
  3.     public static void main(String[] args) throws IOException
  4.     {
  5.         DefaultPieDataset data = getDataSet();
  6.         //JFreeChart chart = ChartFactory.createPieChart(
  7.         //生成3D饼图
  8.         JFreeChart chart = ChartFactory.createPieChart3D(
  9.             "图书销量统计图",  // 图表标题
  10.             getDataSet(), //数据
  11.             true// 是否显示图例
  12.             false//是否显示工具提示
  13.             false //是否生成URL
  14.         );
  15.         //重新设置图标标题,改变字体
  16.         chart.setTitle(new TextTitle("图书销量统计图"new Font("黑体", Font.ITALIC , 22))); 
  17.         //取得统计图标的第一个图例
  18.         LegendTitle legend = chart.getLegend(0);
  19.         //修改图例的字体
  20.         legend.setItemFont(new Font("宋体", Font.BOLD, 14)); 
  21.         //获得饼图的Plot对象
  22.         PiePlot plot = (PiePlot)chart.getPlot();
  23.         //设置饼图各部分的标签字体
  24.         plot.setLabelFont(new Font("隶书", Font.BOLD, 18)); 
  25.         //设定背景透明度(0-1.0之间)
  26.         plot.setBackgroundAlpha(0.9f);
  27.         //设定前景透明度(0-1.0之间)
  28.         plot.setForegroundAlpha(0.50f);
  29.         FileOutputStream fos = new FileOutputStream("book.jpg");
  30.         ChartUtilities.writeChartAsJPEG(
  31.             fos, //输出到哪个输出流
  32.             1//JPEG图片的质量,0~1之间
  33.             chart, //统计图标对象
  34.             800//宽
  35.             600,//宽
  36.             null //ChartRenderingInfo 信息
  37.             );
  38.         fos.close();
  39.     }
  40.     private static DefaultPieDataset getDataSet()
  41.     {
  42.         DefaultPieDataset dataset = new DefaultPieDataset();
  43.         dataset.setValue("J2ME嵌入式开发",47000);
  44.         dataset.setValue("J2EE web应用开发",38000);
  45.         dataset.setValue("基于J2EE的Ajax开发",31000);
  46.         dataset.setValue("JavaScript权威指南",29000);
  47.         dataset.setValue("J2SE应用开发",25000);
  48.         return dataset;
  49.     }
  50. }

LineChartDemo

  1. public class LineChartDemo
  2. {
  3.     public static void main(String[] args) throws IOException
  4.     {
  5.         JFreeChart chart = ChartFactory.createTimeSeriesChart(
  6.                             "水果销量统计图"// 图表标题
  7.                             "水果"// 目录轴的显示标签
  8.                             "销量"// 数值轴的显示标签
  9.                             getDataSet(), // 数据集
  10.                             //PlotOrientation.HORIZONTAL , // 图表方向:水平
  11.                             //PlotOrientation.VERTICAL , // 图表方向:垂直
  12.                             true,   // 是否显示图例(对于简单的柱状图必须是false)
  13.                             false,  // 是否生成工具
  14.                             false   // 是否生成URL链接
  15.                             );
  16.                             
  17.         //重新设置图标标题,改变字体
  18.         chart.setTitle(new TextTitle("水果销量统计图"new Font("黑体", Font.ITALIC , 22))); 
  19.         //取得统计图标的第一个图例
  20.         LegendTitle legend = chart.getLegend(0);
  21.         //修改图例的字体
  22.         legend.setItemFont(new Font("宋体", Font.BOLD, 14)); 
  23.         XYPlot plot = (XYPlot)chart.getPlot();
  24.         //取得横轴
  25.         ValueAxis categoryAxis = plot.getDomainAxis();
  26.         //设置横轴显示标签的字体
  27.         categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  28.         categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
  29.         //取得纵轴
  30.         NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
  31.         //设置纵轴显示标签的字体
  32.         numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  33.         FileOutputStream fos = null;
  34.         fos = new FileOutputStream("fruitLine.jpg");
  35.         //将统计图标输出成JPG文件
  36.         ChartUtilities.writeChartAsJPEG(
  37.             fos, //输出到哪个输出流
  38.             1//JPEG图片的质量,0~1之间
  39.             chart, //统计图标对象
  40.             800//宽
  41.             600,//宽
  42.             null //ChartRenderingInfo 信息
  43.             );
  44.         fos.close();
  45.     }
  46.     //返回一个CategoryDataset实例
  47.     private static XYDataset getDataSet()
  48.     {
  49.         TimeSeries apple =new TimeSeries("苹果",Month.class);
  50.         apple.add(new Month(10,2007),3900);
  51.         apple.add(new Month(11,2007),900);
  52.         apple.add(new Month(12,2007),2500);
  53.         apple.add(new Month(1,2008),3900);
  54.         apple.add(new Month(2,2008),2000);
  55.         apple.add(new Month(3,2008),3300);
  56.         
  57.         TimeSeries orange=new TimeSeries("桔子",Month.class);
  58.         orange.add(new Month(10,2007),3300);
  59.         orange.add(new Month(11,2007),2680);
  60.         orange.add(new Month(12,2007),2000);
  61.         orange.add(new Month(1,2008),1900);
  62.         orange.add(new Month(2,2008),2000);
  63.         orange.add(new Month(3,2008),2300);
  64.         
  65.         TimeSeriesCollection dataset=new TimeSeriesCollection();
  66.         dataset.addSeries(apple);
  67.         dataset.addSeries(orange);
  68.         return dataset;
  69.     }
  70. }

上面是测试,接下来是Struts2集成jfreechart,本例源代码:http://download.csdn.net/source/852878

配置struts2,web.xml

  1.     <!-- 定义Struts2的FilterDispathcer的Filter -->
  2.     <filter>
  3.         <filter-name>struts2</filter-name>
  4.         <filter-class>org.apache.struts2.dispatcher.FilterDispatcher</filter-class>
  5.     </filter>
  6.     <!-- FilterDispatcher用来初始化struts2并且处理所有的WEB请求。 -->
  7.     <filter-mapping>
  8.         <filter-name>struts2</filter-name>
  9.         <url-pattern>/*</url-pattern>
  10.     </filter-mapping>

struts.xml

  1.     <package name="jCuckoo" namespace="" extends="jfreechart-default">//此处需要注意
  2.         <action name="bookChart" class="jCuckoo.ChartAction">
  3.             <result type="chart">
  4.                 <param name="width">600</param>
  5.                 <param name="height">450</param>
  6.             </result>
  7.         </action>
  8.         <action name="barChart3Dt" class="jCuckoo.BarChart3DAction">
  9.             <result type="chart">
  10.                 <param name="width">600</param>
  11.                 <param name="height">450</param>
  12.             </result>
  13.         </action>
  14.     </package>

同时修改struts2-jfreechart-plugin-2.0.14.jar中的struts2-jfreechart-plugin-2.0.14.jar

  1.     <package name="jfreechart-default" extends="struts-default">//此处注意
  2.     
  3.         <result-types>
  4.             <result-type name="chart" class="org.apache.struts2.dispatcher.ChartResult">
  5.                 <param name="height">150</param>
  6.                 <param name="width">200</param>
  7.             </result-type>
  8.         </result-types>
  9.     </package>

ChartAction

  1. public class ChartAction extends ActionSupport {
  2.     private JFreeChart chart;
  3.     public JFreeChart getChart()
  4.     {
  5.         chart = ChartFactory.createPieChart3D(
  6.             "图书销量统计图",  // 图表标题
  7.             getDataSet(), //数据
  8.             true// 是否显示图例
  9.             false//是否显示工具提示
  10.             false //是否生成URL
  11.         );
  12.         //重新设置图标标题,改变字体
  13.         chart.setTitle(new TextTitle("图书销量统计图"new Font("黑体", Font.ITALIC , 22))); 
  14.         //取得统计图标的第一个图例
  15.         LegendTitle legend = chart.getLegend(0);
  16.         //修改图例的字体
  17.         legend.setItemFont(new Font("宋体", Font.BOLD, 14)); 
  18.         //获得饼图的Plot对象
  19.         PiePlot plot = (PiePlot)chart.getPlot();
  20.         //设置饼图各部分的标签字体
  21.         plot.setLabelFont(new Font("隶书", Font.BOLD, 18)); 
  22.         //设定背景透明度(0-1.0之间)
  23.         plot.setBackgroundAlpha(0.9f);
  24.         //设定前景透明度(0-1.0之间)
  25.         plot.setForegroundAlpha(0.50f);
  26.         return chart;
  27.     }
  28.     private DefaultPieDataset getDataSet()
  29.     {
  30.         DefaultPieDataset dataset = new DefaultPieDataset();
  31.         dataset.setValue("J2ME嵌入式开发",47000);
  32.         dataset.setValue("J2EE web应用开发",38000);
  33.         dataset.setValue("基于J2EE的Ajax开发",31000);
  34.         dataset.setValue("JavaScript权威指南",29000);
  35.         dataset.setValue("J2SE应用开发",25000);
  36.         return dataset;
  37.     }
  38. }

BarChart3DAction

  1. public class BarChart3DAction  extends ActionSupport
  2. {
  3.     private JFreeChart chart;
  4.     public JFreeChart getChart(){
  5.         chart = ChartFactory.createBarChart3D(
  6.                             "图书销量统计图"// 图表标题
  7.                             "图书"// 目录轴的显示标签
  8.                             "销量"// 数值轴的显示标签
  9.                             getDataSet(), // 数据集
  10.                             //PlotOrientation.HORIZONTAL , // 图表方向:水平
  11.                             PlotOrientation.VERTICAL , // 图表方向:垂直
  12.                             false,  // 是否显示图例(对于简单的柱状图必须是false)
  13.                             false,  // 是否生成工具
  14.                             false   // 是否生成URL链接
  15.                             );
  16.                             
  17.         //重新设置图标标题,改变字体
  18.         chart.setTitle(new TextTitle("图书销量统计图"new Font("黑体", Font.ITALIC , 22))); 
  19.         CategoryPlot plot = (CategoryPlot)chart.getPlot();
  20.         //取得横轴
  21.         CategoryAxis categoryAxis = plot.getDomainAxis();
  22.         //设置横轴显示标签的字体
  23.         categoryAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  24.         //分类标签以45度角倾斜
  25.         categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
  26.         categoryAxis.setTickLabelFont(new Font("宋体" , Font.BOLD , 18));
  27.         //取得纵轴
  28.         NumberAxis numberAxis = (NumberAxis)plot.getRangeAxis();
  29.         //设置纵轴显示标签的字体
  30.         numberAxis.setLabelFont(new Font("宋体" , Font.BOLD , 22));
  31.         return chart;
  32.     }
  33.     //返回一个CategoryDataset实例
  34.     private  CategoryDataset getDataSet()
  35.     {
  36.         DefaultCategoryDataset dataset = new DefaultCategoryDataset();
  37.         dataset.addValue(47000 , "" , "J2ME嵌入式开发");
  38.         dataset.addValue(38000 , "" , "J2EE web应用开发");
  39.         dataset.addValue(31000 , "" , "基于J2EE的Ajax开发");
  40.         dataset.addValue(29000 , "" , "JavaScript权威指南");
  41.         dataset.addValue(25000 , "" , "J2SE应用开发");
  42.         return dataset;
  43.     }

index.jsp

  1.   <body>
  2.     <img src="bookChart.action"/>
  3.     <img src="barChart3Dt.action"/>
  4.   </body>

本例源代码:http://download.csdn.net/source/852878