JFreeChart生成餅圖,柱圖并與Sturts2整合
首先要去官網下載所需要的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;
}
}
本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!