`

主题:struts2 与 jfreechart的整合

阅读更多

显示效果:

 先引入相关的jar包:

    jcommon-1.0.12.jar     jfreechart-1.0.9.jar     struts2-jfreechart-plugin-2.1.6.jar

Jfreecharaction.java代码 复制代码 收藏代码
  1. package com.example.struts.action;   
  2. import jfreeChart.JfreeChartTest;   
  3.   
  4. import org.jfree.chart.JFreeChart;   
  5.   
  6. import com.opensymphony.xwork2.ActionSupport;   
  7.   
  8. @SuppressWarnings("serial")   
  9. public class JfreeCharAction extends ActionSupport {   
  10.   
  11.     /**   
  12.      * 定义JFreeChart对象 大家请注意在这里JFreeChart对象名只能为chart    
  13.      * 不能是别的    
  14.      * 关于这点   
  15.      * 大家可以上struts2网站上面查看一下   
  16.      *    
  17.      * http://struts.apache.org/2.x/docs/jfreechart-plugin.html   
  18.      */   
  19.     private JFreeChart chart;   
  20.   
  21.     public JFreeChart getChart() {   
  22.         return chart;   
  23.     }   
  24.   
  25.     public void setChart(JFreeChart chart) {   
  26.         this.chart = chart;   
  27.     }   
  28.   
  29.     @Override   
  30.     public String execute() throws Exception {   
  31.         // 调用方法   
  32.         this.chart = JfreeChartTest.createChart();   
  33.         return SUCCESS;   
  34.     }   
  35. }  
package com.example.struts.action;
import jfreeChart.JfreeChartTest;

import org.jfree.chart.JFreeChart;

import com.opensymphony.xwork2.ActionSupport;

@SuppressWarnings("serial")
public class JfreeCharAction extends ActionSupport {

	/**
	 * 定义JFreeChart对象 大家请注意在这里JFreeChart对象名只能为chart 
	 * 不能是别的 
	 * 关于这点
	 * 大家可以上struts2网站上面查看一下
	 * 
	 * http://struts.apache.org/2.x/docs/jfreechart-plugin.html
	 */
	private JFreeChart chart;

	public JFreeChart getChart() {
		return chart;
	}

	public void setChart(JFreeChart chart) {
		this.chart = chart;
	}

	@Override
	public String execute() throws Exception {
		// 调用方法
		this.chart = JfreeChartTest.createChart();
		return SUCCESS;
	}
}

 

Jfreecharttest.java代码 复制代码 收藏代码
  1. package jfreeChart;   
  2.   
  3. import java.awt.Font;   
  4. import java.io.IOException;   
  5.   
  6. import org.jfree.chart.ChartFactory;   
  7. import org.jfree.chart.JFreeChart;   
  8. import org.jfree.chart.plot.PiePlot;   
  9. import org.jfree.data.general.DefaultPieDataset;   
  10.   
  11. public class JfreeChartTest {   
  12.   
  13.     public static JFreeChart createChart() throws IOException {   
  14.         // 数据集   
  15.         DefaultPieDataset dpd = new DefaultPieDataset();   
  16.         dpd.setValue("管理人员"25);   
  17.         dpd.setValue("市场人员"25);   
  18.         dpd.setValue("开发人员"45);   
  19.         dpd.setValue("其它人员"10);   
  20.         // 创建PieChart对象   
  21.         JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图", dpd,   
  22.                 true, true, false);   
  23.         utils.setFont(chart);   
  24.         return chart;   
  25.     }   
  26. }   
  27.   
  28. /**   
  29.  * 设置字体   
  30.  *    
  31.  * @author zyong   
  32.  *    
  33.  */   
  34. class utils {   
  35.     public static void setFont(JFreeChart chart) {   
  36.         Font font = new Font("宋体", Font.ITALIC, 12);   
  37.         PiePlot plot = (PiePlot) chart.getPlot();   
  38.         chart.getTitle().setFont(font);   
  39.         plot.setLabelFont(font);   
  40.         chart.getLegend().setItemFont(font);   
  41.     }   
  42. }  
package jfreeChart;

import java.awt.Font;
import java.io.IOException;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;

public class JfreeChartTest {

	public static JFreeChart createChart() throws IOException {
		// 数据集
		DefaultPieDataset dpd = new DefaultPieDataset();
		dpd.setValue("管理人员", 25);
		dpd.setValue("市场人员", 25);
		dpd.setValue("开发人员", 45);
		dpd.setValue("其它人员", 10);
		// 创建PieChart对象
		JFreeChart chart = ChartFactory.createPieChart3D("某公司人员组织结构图", dpd,
				true, true, false);
		utils.setFont(chart);
		return chart;
	}
}

/**
 * 设置字体
 * 
 * @author zyong
 * 
 */
class utils {
	public static void setFont(JFreeChart chart) {
		Font font = new Font("宋体", Font.ITALIC, 12);
		PiePlot plot = (PiePlot) chart.getPlot();
		chart.getTitle().setFont(font);
		plot.setLabelFont(font);
		chart.getLegend().setItemFont(font);
	}
}

 

Struts.xml代码 复制代码 收藏代码
  1. <struts>   
  2.     <!--    
  3.         关于extends继承jfreechart-default这点请大家注意   
  4.         因为在struts-default这个包里并没有result-type为chart的   
  5.         chart 定义在前面我们导入的struts2-jfreechart-plugin-2.1.6.jar   
  6.         下面的struts-plugin.xml文件中   
  7.     -->   
  8.     <package name="jfreechart" extends="jfreechart-default">   
  9.         <action name="jfreechart" class="com.example.struts.action.JfreeCharAction">   
  10.             <result name="success" type="chart">   
  11.                 <param name="width">600</param>   
  12.                 <param name="height">400</param>   
  13.             </result>   
  14.         </action>   
  15.     </package>   
  16. </struts>  
<struts>
	<!-- 
		关于extends继承jfreechart-default这点请大家注意
		因为在struts-default这个包里并没有result-type为chart的
		chart 定义在前面我们导入的struts2-jfreechart-plugin-2.1.6.jar
		下面的struts-plugin.xml文件中
	-->
	<package name="jfreechart" extends="jfreechart-default">
		<action name="jfreechart" class="com.example.struts.action.JfreeCharAction">
			<result name="success" type="chart">
				<param name="width">600</param>
				<param name="height">400</param>
			</result>
		</action>
	</package>
</struts>

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics