Java-jfree报表(学习整理)----饼状图、柱状图、折线统计图
1、使用的报表工具:
jfree报表
2、下载网址:
http://www.jfree.org/
下载之后先解压:如下图:
下载后:需要的jar包!如下图:
开:找到以下的两个jar包
再导入开发项目中:
之后就可以正常使用jfree报表了:
3、jfree报表API文档:
http://tool.oschina.net/apidocs/apidoc?api=jfreechart
参考博文:Bannings博客
http://blog.csdn.net/zhangao0086/article/details/6365468
4、直接上测试源码:
4.1饼状图:
[Java] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
|
import java.awt.Font;
import java.awt.Image;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.data.general.DefaultPieDataset;
/**
* 测试生成报表图形
* Created by Jason 2016-7-18 上午7:14:59
*/
public class CharReport_BZT {
/**
* 提供静态方法:获取报表图形1:饼状图
* @param title 标题
* @param datas 数据
* @param font 字体
*/
public static void createPort(String title,Map<String,Double> datas,Font font){
try {
//如果不使用Font,中文将显示不出来
DefaultPieDataset pds = new DefaultPieDataset();
//获取迭代器:
Set<Entry<String, Double>> set = datas.entrySet();
Iterator iterator=(Iterator) set.iterator();
Entry entry= null ;
while (iterator.hasNext()){
entry=(Entry) iterator.next();
pds.setValue(entry.getKey().toString(),Double.parseDouble(entry.getValue().toString()));
}
/**
* 生成一个饼图的图表
*
* 分别是:显示图表的标题、需要提供对应图表的DateSet对象、是否显示图例、是否生成贴士以及是否生成URL链接
*/
JFreeChart chart = ChartFactory.createPieChart(title, pds, true , false , true );
//设置图片标题的字体
chart.getTitle().setFont(font);
//得到图块,准备设置标签的字体
PiePlot plot = (PiePlot) chart.getPlot();
//设置分裂效果,需要指定分裂出去的key
plot.setExplodePercent( "天使-彦" , 0.1 );
//设置标签字体
plot.setLabelFont(font);
//设置图例项目字体
chart.getLegend().setItemFont(font);
/**
* 设置开始角度(弧度计算)
*
* 度 0° 30° 45° 60° 90° 120° 135° 150° 180° 270° 360°
* 弧度 0 π/6 π/4 π/3 π/2 2π/3 3π/4 5π/6 π 3π/2 2π
*/
plot.setStartAngle( new Float( 3 .14f / 2f));
//设置背景图片,设置最大的背景
Image img = ImageIO.read( new File( "f:/test/1.jpg" ));
chart.setBackgroundImage(img);
//设置plot的背景图片
img = ImageIO.read( new File( "f:/test/2.jpg" ));
plot.setBackgroundImage(img);
//设置plot的前景色透明度
plot.setForegroundAlpha( 0 .7f);
//设置plot的背景色透明度
plot.setBackgroundAlpha( 0 .0f);
//设置标签生成器(默认{0})
//{0}:key {1}:value {2}:百分比 {3}:sum
plot.setLabelGenerator( new StandardPieSectionLabelGenerator( "{0}({1}票)/{2}" ));
//将内存中的图片写到本地硬盘
ChartUtilities.saveChartAsJPEG( new File( "f:/test/aa.png" ), chart, 600 , 300 );
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Font font = new Font( "新宋体" , Font.BOLD, 15 );
Map<String, Double> map= new HashMap<String, Double>();
map.put( "天使-彦" , ( double ) 1000 );
map.put( "雄兵连-蔷薇" , ( double ) 700 );
map.put( "太阳之光-蕾娜" , ( double ) 600 );
map.put( "辅助-琴女" , ( double ) 400 );
CharReport_BZT.createPort( "超神学院女神投票结果" , map, font);
}
}
|
运行结果:
4.2柱状图:
[Java] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
import java.awt.Font;
import java.io.File;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
import java.util.List;
import java.util.Map.Entry;
/**
* Created by Jason 2016-7-18 上午8:12:38
*/
public class CharReport_ZZT {
/**
* 提供静态方法:获取报表图形2:柱状图
* @param title 标题
* @param datas 数据
* @param type 分类(第一季,第二季.....)
* @param danwei 柱状图的数量单位
* @param font 字体
*/
public static void createPort(String title,Map<String,Map<String,Double>> datas,String type,String danwei,Font font){
try {
//种类数据集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
//获取迭代器:
Set<Entry<String, Map<String, Double>>> set1 = datas.entrySet(); //总数据
Iterator iterator1=(Iterator) set1.iterator(); //第一次迭代
Iterator iterator2= null ;
HashMap<String, Double> map = null ;
Set<Entry<String,Double>> set2= null ;
Entry entry1= null ;
Entry entry2= null ;
while (iterator1.hasNext()){
entry1=(Entry) iterator1.next(); //遍历分类
map=(HashMap<String, Double>) entry1.getValue(); //得到每次分类的详细信息
set2=map.entrySet(); //获取键值对集合
iterator2=set2.iterator(); //再次迭代遍历
while (iterator2.hasNext()) {
entry2= (Entry) iterator2.next();
ds.setValue(Double.parseDouble(entry2.getValue().toString()), //每次统计数量
entry2.getKey().toString(), //名称
entry1.getKey().toString()); //分类
System.out.println( "当前:--- " +entry2.getKey().toString()+ "--"
+entry2.getValue().toString()+ "--"
+entry1.getKey().toString());
}
System.out.println( "-------------------------------------" );
}
//创建柱状图,柱状图分水平显示和垂直显示两种
JFreeChart chart = ChartFactory.createBarChart(title, type, danwei, ds, PlotOrientation.VERTICAL, true , true , true );
//设置整个图片的标题字体
chart.getTitle().setFont(font);
//设置提示条字体
font = new Font( "宋体" , Font.BOLD, 15 );
chart.getLegend().setItemFont(font);
//得到绘图区
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到绘图区的域轴(横轴),设置标签的字体
plot.getDomainAxis().setLabelFont(font);
//设置横轴标签项字体
plot.getDomainAxis().setTickLabelFont(font);
//设置范围轴(纵轴)字体
plot.getRangeAxis().setLabelFont(font);
//存储成图片
//设置chart的背景图片
chart.setBackgroundImage(ImageIO.read( new File( "f:/test/1.jpg" )));
plot.setBackgroundImage(ImageIO.read( new File( "f:/test/2.jpg" )));
plot.setForegroundAlpha( 1 .0f);
ChartUtilities.saveChartAsJPEG( new File( "f:/test/bb.png" ), chart, 600 , 400 );
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Map<String, Map<String, Double>> datas = new HashMap<String, Map<String,Double>>();
Map<String, Double> map1= new HashMap<String, Double>();
Map<String, Double> map2= new HashMap<String, Double>();
Map<String, Double> map3= new HashMap<String, Double>();
Map<String, Double> map4= new HashMap<String, Double>();
//设置第一期的投票信息
map1.put( "天使-彦" , ( double ) 1000 );
map1.put( "雄兵连-蔷薇" , ( double ) 700 );
map1.put( "太阳之光-蕾娜" , ( double ) 600 );
map1.put( "辅助-琴女" , ( double ) 400 );
//设置第二期的投票信息
map2.put( "天使-彦" , ( double ) 1300 );
map2.put( "雄兵连-蔷薇" , ( double ) 900 );
map2.put( "太阳之光-蕾娜" , ( double ) 800 );
map2.put( "辅助-琴女" , ( double ) 500 );
//设置第三期的投票信息
map2.put( "天使-彦" , ( double ) 2000 );
map3.put( "雄兵连-蔷薇" , ( double ) 1700 );
map3.put( "太阳之光-蕾娜" , ( double ) 1000 );
map3.put( "辅助-琴女" , ( double ) 1000 );
//设置第四期的投票信息
map4.put( "天使-彦" , ( double ) 3000 );
map4.put( "雄兵连-蔷薇" , ( double ) 2500 );
map4.put( "太阳之光-蕾娜" , ( double ) 1600 );
map4.put( "辅助-琴女" , ( double ) 1400 );
//压入数据
datas.put( "第一季" , map1);
datas.put( "第二季" , map2);
datas.put( "第三季-神与神" , map3);
// datas.put("第四季-黑甲", map4);
Font font = new Font( "宋体" , Font.BOLD, 20 );
CharReport_ZZT.createPort( "超神学院前四季最受欢迎的女性角色投票结果" ,datas, "超神纪元" , "数量单位(票)" ,font);
}
}
|
运行效果:
4.3折线图
[Java] 纯文本查看 复制代码
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
import java.awt.Color;
import java.awt.Font;
import java.io.File;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
/**
* Created by Jason 2016-7-18 上午9:13:20
*/
public class CharReport_ZXT {
/**
* 提供静态方法:获取报表图形3:折线图
* @param title 标题
* @param datas 数据
* @param type 分类(第一季,第二季.....)
* @param danwei 柱状图的数量单位
* @param font 字体
*/
public static void createPort(String title,Map<String,Map<String,Double>> datas,String type,String danwei,Font font){
try {
//种类数据集
DefaultCategoryDataset ds = new DefaultCategoryDataset();
//获取迭代器:
Set<Entry<String, Map<String, Double>>> set1 = datas.entrySet(); //总数据
Iterator iterator1=(Iterator) set1.iterator(); //第一次迭代
Iterator iterator2= null ;
HashMap<String, Double> map = null ;
Set<Entry<String,Double>> set2= null ;
Entry entry1= null ;
Entry entry2= null ;
int index= 1 ;
while (iterator1.hasNext()){ //4种分类
entry1=(Entry) iterator1.next(); //遍历分类
map=(HashMap<String, Double>) entry1.getValue(); //得到每次分类的详细信息
System.err.println(map.size());
set2=map.entrySet(); //获取键值对集合
iterator2=set2.iterator(); //再次迭代遍历
while (iterator2.hasNext()) { //4-4
entry2= (Entry) iterator2.next();
ds.setValue(Double.parseDouble(entry2.getValue().toString()), //每次统计数量
entry2.getKey().toString(), //名称
entry1.getKey().toString()); //分类
System.out.println( "当前:--- " +entry2.getKey().toString()+ "--"
+entry2.getValue().toString()+ "--"
+entry1.getKey().toString());
}
System.out.println( "--------------------------" );
}
//创建折线图,折线图分水平显示和垂直显示两种
// JFreeChart chart2 = ChartFactory.createLineChart(title, type, danwei, ds,//2D折线图
// PlotOrientation.VERTICAL,
// true, true, true);
JFreeChart chart = ChartFactory.createLineChart3D(title, type, danwei, ds, //3D折线图
PlotOrientation.VERTICAL,
true , true , false );
//设置整个图片的标题字体
chart.getTitle().setFont(font);
//设置提示条字体
font = new Font( "宋体" , Font.BOLD, 15 );
chart.getLegend().setItemFont(font);
//得到绘图区
CategoryPlot plot = (CategoryPlot) chart.getPlot();
//得到绘图区的域轴(横轴),设置标签的字体
plot.getDomainAxis().setLabelFont(font);
//设置横轴标签项字体
plot.getDomainAxis().setTickLabelFont(font);
//设置范围轴(纵轴)字体
font = new Font( "宋体" , Font.BOLD, 18 );
plot.getRangeAxis().setLabelFont(font);
//存储成图片
//设置chart的背景图片
chart.setBackgroundImage(ImageIO.read( new File( "f:/test/1.jpg" )));
// plot.setBackgroundImage(ImageIO.read(new File("f:/test/2.jpg")));
plot.setForegroundAlpha( 1 .0f);
ChartUtilities.saveChartAsJPEG( new File( "f:/test/cc.png" ), chart, 800 , 400 );
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Map<String, Map<String, Double>> datas = new HashMap<String, Map<String,Double>>();
Map<String, Double> map1= new HashMap<String, Double>();
Map<String, Double> map2= new HashMap<String, Double>();
Map<String, Double> map3= new HashMap<String, Double>();
Map<String, Double> map4= new HashMap<String, Double>();
//设置第一期的投票信息
map1.put( "蔷薇" , ( double ) 700 );
map1.put( "彦" , ( double ) 1000 );
map1.put( "蕾娜" , ( double ) 600 );
map1.put( "琴女" , ( double ) 400 );
//设置第二期的投票信息
map2.put( "蔷薇" , ( double ) 900 );
map2.put( "彦" , ( double ) 2000 );
map2.put( "蕾娜" , ( double ) 800 );
map2.put( "琴女" , ( double ) 500 );
//设置第三期的投票信息
map3.put( "蔷薇" , ( double ) 1700 );
map3.put( "彦" , ( double ) 1000 );
map3.put( "蕾娜" , ( double ) 1400 );
map3.put( "琴女" , ( double ) 1000 );
//设置第四期的投票信息
map4.put( "蔷薇" , ( double ) 2500 );
map4.put( "彦" , ( double ) 3000 );
map4.put( "蕾娜" , ( double ) 1600 );
map4.put( "琴女" , ( double ) 1400 );
//压入数据
datas.put( "第一季" , map1);
datas.put( "第二季" , map2);
datas.put( "第三季" , map3);
datas.put( "第四季" , map4);
Font font = new Font( "宋体" , Font.BOLD, 20 );
CharReport_ZXT.createPort( "超神学院前四季最受欢迎的女性角色投票结果" ,datas, "超神纪元" , "数量单位(票)" ,font);
}
}
|
运行效果: