JFreeChart生成饼图,柱图并与Sturts2整合
jopen
10年前
首先要去官网下载所需要的jar包 http://www.jfree.org/jfreechart/
生成饼图:package jFreeTest; import java.awt.Font; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; public class Test1 { /** * @param args */ public static void main(String[] args) { //默认饼图数据集 DefaultPieDataset dpd=new DefaultPieDataset(); dpd.setValue("市场部",25); dpd.setValue("IT部",50); dpd.setValue("财务部",30); dpd.setValue("其它部",25); JFreeChart jfc=ChartFactory.createPieChart3D ("某公司人员组织结构图",dpd,true,true,false); Font font = new Font("黑体", 1, 20); jfc.getTitle().setFont(font); ((PiePlot)jfc.getPlot()).setLabelFont(font); /* PiePlot pieplot = (PiePlot)jfc.getPlot(); pieplot.setLabelFont(font); */ jfc.getLegend().setItemFont(new Font("宋体",2,18)); ChartFrame chartFrame=new ChartFrame("s司人员结构图s",jfc); chartFrame.pack(); //chartFrame.setFont(font); chartFrame.setVisible(true); } }
柱图
package jFreeTest; import java.awt.Font; import javax.swing.JPanel; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartPanel; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import org.jfree.ui.ApplicationFrame; public class Test2 extends ApplicationFrame { //构造函数 public Test2(String title) { super(title); //将面版存放到 内容面版中 this.setContentPane(createJPanel()); } //创建数据集category 分类 public static CategoryDataset createDataset(){ DefaultCategoryDataset category =new DefaultCategoryDataset(); category.setValue(20, "管理人员a", "管理人员"); category.setValue(30, "IT人员b", "IT人员"); category.setValue(50, "人事人员c", "人事人员"); category.setValue(20, "财务人员d", "财务人员"); return category; } //创建JFreeChart 图对象 public static JFreeChart createJFreeChart(CategoryDataset category){ //构建JFreeChart 对象 JFreeChart jfreeChart=ChartFactory.createBarChart3D("hello", "人员分布","人员数量", category, PlotOrientation.VERTICAL, true, true,false); //对值重置 jfreeChart.setTitle(new TextTitle("某公司人员图", new Font("黑体",Font.BOLD +Font.ITALIC, 20))); //分类图 CategoryPlot plot=(CategoryPlot)jfreeChart.getPlot(); //领域轴 CategoryAxis axis=plot.getDomainAxis(); axis.setLabelFont(new Font("宋体",Font.PLAIN,18)); axis.setTickLabelFont(new Font("宋体",Font.PLAIN,15)); //设置Y坐轴 plot.getRangeAxis().setLabelFont(new Font("宋体",Font.BOLD,18));//设置y轴坐标上的标题的字体 //设置y轴坐标上的字体 plot.getRangeAxis().setTickLabelFont(new Font("宋体",Font.BOLD,15)); //lagend jfreeChart.getLegend().setItemFont(new Font("宋体",Font.BOLD,12)); return jfreeChart; } /** * 面版 存放jfree cahrt * @param args */ public static JPanel createJPanel(){ //创建JFreeChart JFreeChart jfc=createJFreeChart(createDataset()); JPanel j= new ChartPanel(jfc); return j; } public static void main(String[] args) { //执行 生成类的实列 Test2 test2=new Test2("hello world..."); test2.pack(); test2.setVisible(true); } }
生成图片
:
package jFreeTest; import java.awt.Font; import java.io.FileOutputStream; import java.io.OutputStream; import org.jfree.chart.ChartFactory; import org.jfree.chart.ChartFrame; import org.jfree.chart.ChartUtilities; import org.jfree.chart.JFreeChart; import org.jfree.chart.plot.PiePlot; import org.jfree.chart.title.TextTitle; import org.jfree.data.general.DefaultPieDataset; public class ImageTest3 { //Dataset public static DefaultPieDataset createPinDateset(){ DefaultPieDataset dpd=new DefaultPieDataset(); dpd.setValue("市场部",15); dpd.setValue("IT部",20); dpd.setValue("财务部",120); dpd.setValue("其它部",125); return dpd; } /** * @param args */ public static void main(String[] args) throws Exception { JFreeChart jfc=ChartFactory.createPieChart3D("公司部门统计图", createPinDateset(), true, true, false); //设置 标题字体 jfc.getTitle().setFont(new Font("微软雅黑",Font.BOLD,25)); //polt 图 PiePlot pp=(PiePlot) jfc.getPlot(); pp.setLabelFont(new Font("微软雅黑",Font.BOLD,15)); //图例字体 jfc.getLegend().setItemFont(new Font("微软雅黑",Font.BOLD,15)); /* ChartFrame cf=new ChartFrame("",jfc); cf.pack(); cf.setVisible(true);*/ //输出图片 OutputStream os=new FileOutputStream("Company.jpg"); //图表 实用工具 ChartUtilities.writeChartAsJPEG(os, jfc, 1000, 800); os.close(); } }
和strtus2整合,在项目中应用:
页面代码:
<%@ page language="java" contentType="text/html" pageEncoding="UTF-8"%> <%@ taglib prefix="s" uri="/struts-tags" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <h1>Chart Test</h1> <s:form action="showTick.action" method="post"> <s:checkbox name="interest" label="蓝球" fieldValue="nanqiu" labelposition="left"/> <s:checkbox name="interest" label="足球" fieldValue="zhuqiu" labelposition="left"/> <s:checkbox name="interest" label="羽毛球" fieldValue="ymq" labelposition="left"/> <s:checkbox name="interest" label="游泳" fieldValue="youyong" labelposition="left"/> <s:submit value="提交"/> </s:form> </body> </html>
struts.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN" "http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="default" extends="jfreechart-default, jasperreports-default"> <action name="showTick" class="strutsAction.ShowTick"> <result name="success" type="chart"> <param name="width">800</param> <param name="height">600</param> </result> </action> <!-- jasperReport --> <action name="showReport" class="strutsAction.ShowRepeort"> <result name="success" type="jasper"> <param name="location">test.jasper</param> <param name="dataSource">list</param> <param name="format">PDF</param> </result> </action> </package> </struts>
action实现类代码:
package strutsAction; import java.awt.Font; import java.util.List; import java.util.Map; import org.jfree.chart.ChartFactory; import org.jfree.chart.JFreeChart; import org.jfree.chart.axis.CategoryAxis; import org.jfree.chart.axis.CategoryLabelPositions; import org.jfree.chart.plot.CategoryPlot; import org.jfree.chart.plot.PlotOrientation; import org.jfree.chart.title.TextTitle; import org.jfree.data.category.CategoryDataset; import org.jfree.data.category.DefaultCategoryDataset; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; public class ShowTick extends ActionSupport { //jfreecha private JFreeChart chart; public JFreeChart getChart() { chart=ChartFactory.createBarChart("兴趣统计结果", "项目","结果", this.createDatasetss(), PlotOrientation.VERTICAL, true, true, false); if(null==chart){ throw new RuntimeException("chart怎么会是空呢,你大爷爷的---------"); } //设置字体 chart.setTitle(new TextTitle("兴趣统计结果",new Font("宋体",Font.BOLD,20))); //x CategoryPlot cp=(CategoryPlot)chart.getPlot(); CategoryAxis axis=cp.getDomainAxis(); axis.setLabelFont(new Font("宋体",Font.BOLD,18)); axis.setTickLabelFont(new Font("宋体",Font.BOLD,15)); //设置文字倾多少度 axis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //y cp.getRangeAxis().setLabelFont(new Font("宋体",Font.BOLD,18)); cp.getRangeAxis().setTickLabelFont(new Font("宋体",Font.BOLD,15)); //legend chart.getLegend().setItemFont(new Font("宋体",Font.BOLD,20)); return chart; } private List<String> interest; public List<String> getInterest() { return interest; } public void setInterest(List<String> interest) { this.interest = interest; } @Override public String execute() throws Exception { return SUCCESS; } ////计算投票 并入入aplication public void interestResult(List<String> interests){ ActionContext ac=ActionContext.getContext(); Map mp=ac.getApplication(); for(String str:interests){ if(!mp.containsKey(str)){ mp.put(str, 1); }else{ mp.put(str, (Integer)mp.get(str)+1); } } } //获取数据集 public CategoryDataset createDatasetss(){ DefaultCategoryDataset dcd=new DefaultCategoryDataset(); //计算投票 this.interestResult(this.getInterest()); //获取application ActionContext ac=ActionContext.getContext(); Map map =ac.getApplication(); //设置值 //System.out.println("1:"+(Integer)map.get("nanqiu")); dcd.setValue((Integer)map.get("nanqiu"), "蓝","蓝球"); dcd.setValue((Integer)map.get("zhuqiu"), "足","足球"); dcd.setValue((Integer)map.get("ymq"), "羽毛","羽毛球"); dcd.setValue((Integer)map.get("youyong"), "游泳","游泳"); /*dcd.setValue(5,"","蓝球"); dcd.setValue(6, "","足球"); dcd.setValue(34, "","羽毛球"); dcd.setValue(21, "","游泳");*/ return dcd; } }