1.只需要引入三个jar包如下图
然后分别有已经写好的三个绘制工具类
1.饼状图代码
package com.lisheng.util;
import java.awt.Font;
import java.text.DecimalFormat;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.util.Rotation;
public class Bzt
{
/**
* 创建饼状图
*
* @param title
*标题
* @param map
*键值对(水果名称 水果数量)
* @return
*/
public static JFreeChart createPieChart(String title, Map map)
{
DefaultPieDataset dpd = createPieDataset(map);
// 创建PieChart对象(标题,数据集,是否显示图例,是否生成工具提示,是否生成URL链接)
JFreeChart chart = ChartFactory.createPieChart3D(title, dpd, true,true,false);
setPie3DStyle(chart);
return chart;
}
/**
* 创建饼状图数据
*
* @param map
* @return
*/
private static DefaultPieDataset createPieDataset(Map map)
{
DefaultPieDataset dpd = new DefaultPieDataset();
Set<String> set = map.keySet();
for (Iterator<String> itor = set.iterator(); itor.hasNext();)
{
String key = itor.next();
double x = Double.parseDouble(map.get(key).toString());
dpd.setValue(key, x);
}
return dpd;
}
/**
* 设置饼状图3D样式
*
* @param chart
*/
private static void setPie3DStyle(JFreeChart chart)
{
// 获得3D的水晶饼图对象
PiePlot3D pieplot3d = (PiePlot3D) chart.getPlot();
// 设置开始角度
pieplot3d.setStartAngle(150D);
// 设置方向为”顺时针方向“
pieplot3d.setDirection(Rotation.CLOCKWISE);
// 设置透明度,0.5F为半透明,1为不透明,0为全透明
pieplot3d.setForegroundAlpha(0.5F);
pieplot3d.setNoDataMessage("没有相应的数据显示");
Font font = new Font("宋体", Font.ITALIC, 12);
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 22));
pieplot3d.setLabelFont(font);
chart.getLegend().setItemFont(font);
StandardPieSectionLabelGenerator standarPieIG = new StandardPieSectionLabelGenerator("{0}:({1},{2})",
new DecimalFormat("0.0"), new DecimalFormat("0.0%"));
pieplot3d.setLabelGenerator(standarPieIG);
}
}
2.折线图代码
package com.lisheng.util;
import java.awt.Font;
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.axis.NumberAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.DefaultCategoryDataset;
public class Zxt {
/**
* 创建折线图
* @param title
* @param XLabel
* @param YLabel
* @return
*/
public static JFreeChart createLineChart(String title,String XLabel,String YLabel,DefaultCategoryDataset dataset) {
//该数据的Demo由系统提供,故不传入相应的Map或List数据了
// 定义图表对象(折线图名称,横坐标名称,纵坐标名称,数据, 水平显示图像)
JFreeChart chart = ChartFactory.createLineChart(title,XLabel,YLabel,dataset,PlotOrientation.VERTICAL,true,true,true);
setLineStyle(chart);
return chart;
}
/**
* 设置折线图样式
*
* @param chart
*/
private static void setLineStyle(JFreeChart chart){
CategoryPlot plot = chart.getCategoryPlot();
//设置标题字体样式
chart.getTitle().setFont(new Font("黑体", Font.ITALIC,14));
//取得横轴和设置横轴样式
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
//横轴分类标签
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 14));
plot.setRangeGridlinesVisible(true); //是否显示格子线
plot.setBackgroundAlpha(0.3f); //设置背景透明度
NumberAxis rangeAxis = (NumberAxis)plot.getRangeAxis();
rangeAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
//设置工具工具提示字体样式
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 14));
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
rangeAxis.setAutoRangeIncludesZero(true);
rangeAxis.setUpperMargin(0.20);
rangeAxis.setLabelAngle(Math.PI / 2.0);
}
}
3.柱状图
package com.lisheng.util;
import java.awt.Font;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
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.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;
public class Tzt {
/**
* 创建柱状图
* @param title 标题
* @param XLabel X轴标签
* @param YLabel Y轴标签
* @param map 键值对
* @return
*/
public static JFreeChart createBarChart(String title, String XLabel, String YLabel,DefaultCategoryDataset dataset) {
// 创建一个柱状图(图表标题,X轴显示标签,Y轴显示标签,数据集,图表方向(水平or垂直),是否显示图例[对于简单图应为false],是否生成工具,是否生成url链接)
JFreeChart chart = ChartFactory.createBarChart3D(title, XLabel, YLabel, dataset, PlotOrientation.VERTICAL,
true, true, true);
setBarStyle(chart);
return chart;
}
/**
* 设置柱状图样式
*
* @param chart
*/
private static void setBarStyle(JFreeChart chart) {
CategoryPlot plot = chart.getCategoryPlot();
// 设置标题字体样式
chart.getTitle().setFont(new Font("黑体", Font.ITALIC, 14));
// 取得横轴和设置横轴样式
CategoryAxis categoryAxis = plot.getDomainAxis();
categoryAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
// 横轴分类标签
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);
categoryAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 14));
// 取得纵轴和设置纵轴样式
NumberAxis numberAxis = (NumberAxis) plot.getRangeAxis();
numberAxis.setLabelFont(new Font("宋体", Font.BOLD, 14));
// 显示每个柱的数值,并修改该数值的字体属性
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题 ,将数字显示在柱状图上面
renderer.setBasePositiveItemLabelPosition(
new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
renderer.setItemLabelAnchorOffset(10D);
// 设置每个地区所包含的平行柱的之间距离
// renderer.setItemMargin(0.3);
plot.setRenderer(renderer);
}
}
饼状图只需要将数据储存为map类型即可如下
private PicService picService=new Picimpl();
private JFreeChart chart;
//得到饼状图
public String all1()
{
List<PicEntity> pic = picService.getAllpic();----获取数据
Map map=new HashMap(); ---创建map集合
for (PicEntity p : pic)
{
map.put(p.getEename(),p.getPpcount()); 循环将数据放入map中。姓名为key 数量为value
}
Bzt bzt=new Bzt(); --得到饼图工具类
chart=bzt.createPieChart("各个区域客户分布图", map); ------- (“标题”,数据(map))
return "all1";
}
public JFreeChart getChart()
{
return chart;
}
public void setChart(JFreeChart chart)
{
this.chart = chart;
}
2.创建折线图
private StuService stuService = new StuImpl();
private JFreeChart chart;
public JFreeChart getChart()
{
return chart;
}
public void setChart(JFreeChart chart)
{
this.chart = chart;
}
public String all()
{
Zxt zxt=new Zxt();
DefaultCategoryDataset data=new DefaultCategoryDataset(); ----创建一个对象
List<Stu> ar=this.stuService.getAllCost("龚小俊"); 条件得到对应数据
for(Stu s:ar)
{
data.addValue(s.getScost(), s.getSname(), s.getSmonth()+"/月");
}
List<Stu> ar1=this.stuService.getAllCost("龚大俊");
for(Stu s:ar1)
{
data.addValue(s.getScost(), s.getSname(), s.getSmonth()+"/月");
}
this.chart=zxt.createLineChart("个人消费比例图","月分","金额", data);
return "myall";
}
3.条状图
package com.lisheng.action;
import java.util.List;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.DefaultCategoryDataset;
import com.lisheng.entity.School;
import com.lisheng.impl.SchoolImpl;
import com.lisheng.service.SchoolService;
import com.lisheng.util.Tzt;
public class SchoolAction
{
private SchoolService schoolService = new SchoolImpl();
private JFreeChart chart;
public JFreeChart getChart()
{
return chart;
}
public void setChart(JFreeChart chart)
{
this.chart = chart;
}
public String all()
{
Tzt tzt=new Tzt();
DefaultCategoryDataset data=new DefaultCategoryDataset();
List<School> ar=this.schoolService.getAllSchool(2002);
for(School s:ar)
{
data.addValue(s.getScount(), s.getSname(), s.getSyear()+"/年");
}
List<School> ar1=this.schoolService.getAllSchool();
for(School s:ar1)
{
data.addValue(s.getScount(), s.getSname(), s.getSyear()+"/年");
}
this.chart=tzt.createBarChart("红安县2002年各校人数比例图", "学校", "人数", data);
return "myall";
}
}