怎么用java的JFreeChart jar包中的相关方法画一个柱状图?请知道的亲们尽管说的详细一点啊!
又说可以swing中的相关方法的,但是看了也没看懂,
     或者有其他的方法也可以啊。能附上代码就很好了,但是希望知道的高手们附上代码的时候把代码相关注释,写清楚一点啊。比如要建其他的文件,具体的文件命名是什么样的?文件放在什么地方?
     以下是我在网上搜到的相关一个帮助,但是不知道jsp文件应该放在什么地方,怎么命名?也不知道那个web.xml应该放在什么地方?是在项目工程文件夹下吗?
真诚的,谢谢了
/*java文件*/ 
package com.cn; import java.awt.Color; 
import java.awt.Paint; 
import java.awt.RenderingHints; 
import java.io.*; import javax.servlet.http.HttpSession; import org.jfree.data.category.CategoryDataset; 
import org.jfree.data.category.DefaultCategoryDataset; 
import org.jfree.data.general.DefaultPieDataset; 
import org.jfree.data.general.PieDataset; 
import org.jfree.chart.*; 
import org.jfree.chart.entity.StandardEntityCollection; 
import org.jfree.chart.plot.*; 
import org.jfree.chart.servlet.ServletUtilities; 
public class BarChartDemo { /** 
* 饼状图 
*/ 
public static String generatePieChart(HttpSession session, PrintWriter pw,int w, int h){ 
String filename = null; 
PieDataset dataset = getDataSet(); 
JFreeChart chart = ChartFactory.createPieChart3D( 
"水果产量图", // 图表标题 
dataset, // 数据集 
true, // 是否显示图例 
false, // 是否生成工具 
false // 是否生成URL链接 
); 
chart.setBackgroundPaint(Color.pink); 
try { 
/*------得到chart的保存路径----*/ 
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session); 
/*------使用printWriter将文件写出----*/ 
ChartUtilities.writeImageMap(pw, filename, info, true); 
pw.flush(); 
} catch (IOException e) { 
e.printStackTrace(); 

return filename; 

/** 
* 柱状图 
*/ 
public static String generateBarChart(HttpSession session, PrintWriter pw,int w, int h){ 
String filename = null; 
CategoryDataset dataset = getDataSet2(); 
JFreeChart chart = ChartFactory.createBarChart3D( 
"水果产量图", // 图表标题 
"水果", // 目录轴的显示标签 
"产量", // 数值轴的显示标签 
dataset, // 数据集 
PlotOrientation.VERTICAL, // 图表方向:水平、垂直 
true, // 是否显示图例(对于简单的柱状图必须是false) 
false, // 是否生成工具 
false // 是否生成URL链接 
); 
try { 
/*------得到chart的保存路径----*/ 
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session); 
/*------使用printWriter将文件写出----*/ 
ChartUtilities.writeImageMap(pw, filename, info, true); 
pw.flush(); 
} catch (IOException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 

return filename; 
} /** 
* 折线图 
*/ 
public static String generateLineChart(HttpSession session, PrintWriter pw,int w, int h){ 
String filename = null; 
CategoryDataset dataset = getDataSet3(); 
JFreeChart chart = ChartFactory.createLineChart( 
"水果产量图", // 图表标题 
"水果", // 目录轴的显示标签 
"产量", // 数值轴的显示标签 
dataset, // 数据集 
PlotOrientation.VERTICAL, // 图表方向:水平、垂直 
true, // 是否显示图例(对于简单的柱状图必须是false) 
false, // 是否生成工具 
false // 是否生成URL链接 
); /*----------设置消除字体的锯齿渲染(解决中文问题)--------------*/ 
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, 
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); /*------------配置图表属性--------------*/ 
// 1,设置整个图表背景颜色 
chart.setBackgroundPaint(Color.pink); /*------------设定Plot参数-------------*/ 
CategoryPlot plot = chart.getCategoryPlot(); 
// 2,设置详细图表的显示细节部分的背景颜色 
//plot.setBackgroundPaint(Color.PINK); 
// 3,设置垂直网格线颜色 
plot.setDomainGridlinePaint(Color.black); 
//4,设置是否显示垂直网格线 
plot.setDomainGridlinesVisible(true); 
//5,设置水平网格线颜色 
plot.setRangeGridlinePaint(Color.blue); 
//6,设置是否显示水平网格线 
plot.setRangeGridlinesVisible(true); try { 
/*------得到chart的保存路径----*/ 
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection()); 
filename = ServletUtilities.saveChartAsPNG(chart, w, h, info,session); 
/*------使用printWriter将文件写出----*/ 
ChartUtilities.writeImageMap(pw, filename, info, true); 
pw.flush(); 
} catch (IOException e) { 
// TODO 自动生成 catch 块 
e.printStackTrace(); 

return filename; 
} /** 
* 在本地生成图片文件 
*/ 
public static void ganarateFruitChart(){ 
CategoryDataset dataset = getDataSet2(); 
JFreeChart chart = ChartFactory.createBarChart3D( 
"水果产量图", // 图表标题 
"月份", // 目录轴的显示标签 
"产量(公斤)", // 数值轴的显示标签 
dataset, // 数据集 
PlotOrientation.VERTICAL, // 图表方向:水平、垂直 
true, // 是否显示图例(对于简单的柱状图必须是false) 
false, // 是否生成工具 
false // 是否生成URL链接 
); FileOutputStream fos_jpg = null; 
try { 
fos_jpg = new FileOutputStream("D:\\fruit.jpg"); 
ChartUtilities.writeChartAsJPEG(fos_jpg,1.0f,chart,400,300,null); 
} catch (FileNotFoundException e) { 
e.printStackTrace(); 
} catch (IOException e) { 
e.printStackTrace(); 
} finally { 
try { 
fos_jpg.close(); 
} catch (Exception e) {} 


/** 
* 获取一个饼状图的简单数据集对象 
* @return 
*/ 
private static PieDataset getDataSet() { 
DefaultPieDataset dataset = new DefaultPieDataset(); 
dataset.setValue("苹果", 100); 
dataset.setValue("梨子", 200); 
dataset.setValue("葡萄", 300); 
dataset.setValue("香蕉", 400); 
dataset.setValue("荔枝", 500); 
return dataset; 

/** 
* 获取一个柱状图数据集对象 
* @return 
*/ 
private static CategoryDataset getDataSet2() { 
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
dataset.addValue(100, "北京", "苹果"); 
dataset.addValue(500, "北京", "荔枝"); 
dataset.addValue(400, "北京", "香蕉"); 
dataset.addValue(200, "北京", "梨子"); 
dataset.addValue(300, "北京", "葡萄"); 
dataset.addValue(500, "上海", "葡萄"); 
dataset.addValue(600, "上海", "梨子"); 
dataset.addValue(400, "上海", "香蕉"); 
dataset.addValue(700, "上海", "苹果"); 
dataset.addValue(300, "上海", "荔枝"); 
dataset.addValue(300, "广州", "苹果"); 
dataset.addValue(200, "广州", "梨子"); 
dataset.addValue(500, "广州", "香蕉"); 
dataset.addValue(400, "广州", "葡萄"); 
dataset.addValue(700, "广州", "荔枝"); 
return dataset; 
} /** 
* 获取一个折线图数据集对象 
* @return 
*/ 
private static CategoryDataset getDataSet3() { 
DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
dataset.addValue(100, "北京", "一月"); 
dataset.addValue(200, "北京", "二月"); 
dataset.addValue(100, "北京", "三月"); 
dataset.addValue(400, "北京", "四月"); 
dataset.addValue(300, "北京", "五月"); 
dataset.addValue(500, "北京", "六月"); 
dataset.addValue(90, "北京", "七月"); 
dataset.addValue(700, "北京", "八月"); 
dataset.addValue(800, "北京", "九月"); 
dataset.addValue(1000, "北京", "十月"); 
dataset.addValue(300, "北京", "十一月"); 
dataset.addValue(700, "北京", "十二月"); 
dataset.addValue(1200, "上海", "一月"); 
dataset.addValue(1100, "上海", "二月"); 
dataset.addValue(1000, "上海", "三月"); 
dataset.addValue(900, "上海", "四月"); 
dataset.addValue(800, "上海", "五月"); 
dataset.addValue(700, "上海", "六月"); 
dataset.addValue(600, "上海", "七月"); 
dataset.addValue(500, "上海", "八月"); 
dataset.addValue(400, "上海", "九月"); 
dataset.addValue(300, "上海", "十月"); 
dataset.addValue(200, "上海", "十一月"); 
dataset.addValue(100, "上海", "十二月"); 
dataset.addValue(600, "武汉", "一月"); 
dataset.addValue(500, "武汉", "二月"); 
dataset.addValue(400, "武汉", "三月"); 
dataset.addValue(300, "武汉", "四月"); 
dataset.addValue(200, "武汉", "五月"); 
dataset.addValue(100, "武汉", "六月"); 
dataset.addValue(200, "武汉", "七月"); 
dataset.addValue(300, "武汉", "八月"); 
dataset.addValue(400, "武汉", "九月"); 
dataset.addValue(500, "武汉", "十月"); 
dataset.addValue(600, "武汉", "十一月"); 
dataset.addValue(700, "武汉", "十二月"); 
return dataset; 
} /** 
* @param args 
*/ 
public static void main(String[] args) { 
ganarateFruitChart(); 

}/*jsp文件*/ 
<%@ page contentType="text/html;charset=GBK"%> 
<%@ page import="java.io.PrintWriter"%> 
<jsp:directive.page import="com.cn.BarChartDemo"/> 
<html> 
<head> 
<title> 
</title> 
<% 
//饼状图 
String fileNamePie=BarChartDemo.generatePieChart(session,new PrintWriter(out),580,250); 
String graphURLPie = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileNamePie; 
//饼状图 
String fileNameBar=BarChartDemo.generateBarChart(session,new PrintWriter(out),580,250); 
String graphURLBar = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileNameBar; 
//折线图 
String fileNameLine=BarChartDemo.generateLineChart(session,new PrintWriter(out),580,250); 
String graphURLLine = request.getContextPath() + "/servlet/DisplayChart?filename=" + fileNameLine; 
%> 
</head> 
<body bgcolor="#ffffff"> 
<table align="center" width="580" border="0" cellspacing="0" cellpadding="0"> 
<tr> 
<td> 
<img src=" <%= graphURLPie %>"width=580 height=250 border=0 > 
</td> 
</tr> 
<tr> 
<td> 
<img src=" <%= graphURLBar %>"width=580 height=250 border=0 > 
</td> 
</tr> 
<tr> 
<td> 
<img src=" <%= graphURLLine %>"width=580 height=250 border=0 > 
</td> 
</tr> 
</table> 
</body> 
</html> /*web.xml文件*/ 
<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> 
<servlet> 
<servlet-name>DisplayChart </servlet-name> 
<servlet-class> 
org.jfree.chart.servlet.DisplayChart 
</servlet-class> 
</servlet> 
<servlet-mapping> 
<servlet-name>DisplayChart </servlet-name> 
<url-pattern>/servlet/DisplayChart </url-pattern> 
</servlet-mapping> <welcome-file-list> 
<welcome-file>index.jsp </welcome-file> 
</welcome-file-list> 
</web-app> 

解决方案 »

  1.   

    但是不知道jsp文件应该放在什么地方,怎么命名?也不知道那个web.xml应该放在什么地方?是在项目工程文件夹下吗?看来你java 刚开始学,后面的路还很长,加油!jsp是页面,web.XML  是 web项目的配置文件。你还没开始学J2ee 所以不清楚。不过可以用jfreechart  生成本地图片的。直接保存到本地的 统计图。
    你看到的例子 是web 项目,后台生成图片 返回到前台页面显示。
      

  2.   

    每个项目都有web.xml  还能放到什么地方,你写的真的够详细了,页面就是你现实的页面啊,喜欢放到哪就放到哪,和你新建的页面没什么区别,这个收藏了,省的以后还得找
      

  3.   

    给你一个本地玩的类
    【导包 什么的自己来】
    package com.cartogram.dao.impl;import java.awt.Color;
    import java.awt.Font;
    import java.io.File;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartFrame;
    import org.jfree.chart.ChartUtilities;
    import org.jfree.chart.JFreeChart;
    import org.jfree.chart.axis.CategoryAxis;
    import org.jfree.chart.axis.NumberAxis;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PiePlot;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.renderer.category.BarRenderer;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.DefaultCategoryDataset;
    import org.jfree.data.general.DefaultPieDataset;import com.cartogram.dao.GetPicDAO;
    import com.cartogram.pojo.DataDTO;
    import com.cartogram.pojo.ParamPicDTO;
    import com.cartogram.pojo.ReturnPicDTO;public class GetPicImpl implements GetPicDAO { /**
     * 生成柱形图
     * @param list 数据传输集合
     * @param paraDTO 统计图些属性的封装
     * @return 返回属性的封装
     */
    public ReturnPicDTO getBarChart(List<DataDTO> list, ParamPicDTO paraDTO) { ReturnPicDTO rtDTO = new ReturnPicDTO();
    DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String picTitle = ""; //  图片上显示的标题
    String picName = ""; //  图片名称
    String dirName = ""; //  保存到哪个目录
    int width = 640; // 统计图默认宽
    int height = 480; //  统计图默认高 if (null != paraDTO) {
    picTitle = paraDTO.getPicTitle();
    picName = paraDTO.getPicName();
    dirName = paraDTO.getPicDir();
    width = paraDTO.getPicWidth();
    height = paraDTO.getPicHeight();
    }
    if (null == list || list.size()==0){
    list.add(new DataDTO("所查项无数据","所查项无数据",0.0));
    }
    // 遍历list集合,给dataset赋值
    for (int i = 0; i < list.size(); i++) {
    DataDTO dataDTO = list.get(i);
    dataset.addValue(dataDTO.getValueD(), dataDTO.getGroupName(),
    dataDTO.getColName());
    } JFreeChart chart = ChartFactory.createBarChart(picTitle, // 
    paraDTO.getNameX(),  // X轴名称
    paraDTO.getNameY(),  // Y轴名称
    dataset,  // 数据对象
    PlotOrientation.VERTICAL, // 水平或垂直显示
    true, // 是否显示图例    
    true, // 是否显示工具提示   
    true); // 是否生成URL        CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    CategoryAxis domainAxis = categoryplot.getDomainAxis();
    domainAxis.setMaximumCategoryLabelLines(20); //柱子名称的显示长度
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); //45度显示柱子名称
    TextTitle textTitle = chart.getTitle(); //统计图效果的设置***勿删***
    // domainAxis.setLowerMargin(0.05);   //与左边的空区
    // domainAxis.setUpperMargin(0.05);   //右边的空区
    // domainAxis.setCategoryMargin(0.04); //不同类别间的间距 // 柱图的呈现器
    BarRenderer renderer = new BarRenderer();
    // renderer.setIncludeBaseInRange(true);     // 显示每个柱的数值,并修改该数值的字体属性
    renderer
    .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseItemLabelsVisible(true);
    renderer.setBaseOutlinePaint(Color.BLACK); // 
    renderer.setDrawBarOutline(true); // 
    renderer.setMaximumBarWidth(0.05);//

    //柱子最大宽度设置,会使下面设置柱子间距离方法失效
    renderer.setItemMargin(0); // 设置同组柱子间距离
    categoryplot.setRenderer(renderer); textTitle.setFont(new Font("", Font.PLAIN, 20));
    domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
    domainAxis.setLabelFont(new Font("", Font.PLAIN, 12));
    numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
    numberaxis.setLabelFont(new Font("", Font.PLAIN, 12));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 12)); ChartFrame frame = new ChartFrame(picTitle, chart);
    frame.pack(); String path = this.getClass().getResource("").getPath();
    //路径中有空格会替换成%20,替换回来
    path = path.replace("%20", " ");
    int index = path.indexOf("WEB-INF");
    String imgPath = path.substring(1, index) + dirName;
    File ftmp = new File(imgPath);
    if (null != ftmp && !ftmp.exists()) {
    ftmp.mkdirs();
    }
    try {
    ChartUtilities.saveChartAsPNG(new File(imgPath + File.separator
    + picName), chart, width, height);
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println("图片路径" + imgPath + File.separator + picName
    + "");
    rtDTO.setPicName(picName);
    return rtDTO;
    } /**
     * 生成折线图
     * @param list 数据传输集合
     * @param paraDTO 统计图些属性的封装
     * @return 返回属性的封装
     */
    public ReturnPicDTO getLineChart(List<DataDTO> list, ParamPicDTO paraDTO) {
    ReturnPicDTO rtDTO = new ReturnPicDTO();
    DefaultCategoryDataset dataset = new DefaultCategoryDataset(); String picTitle = ""; //  图片上显示的标题
    String picName = ""; //  图片名称
    String dirName = ""; //  保存到哪个目录
    int width = 640; // 统计图默认宽
    int height = 480; //  统计图默认高

    if (null != paraDTO) {
    picTitle = paraDTO.getPicTitle();
    picName = paraDTO.getPicName();
    dirName = paraDTO.getPicDir();
    width = paraDTO.getPicWidth();
    height = paraDTO.getPicHeight();
    }

    if (null == list || list.size()==0){
    list.add(new DataDTO("所查项无数据","所查项无数据",0.0));
    }
    for (int i = 0; i < list.size(); i++) {
    DataDTO dataDTO = list.get(i);
    dataset.addValue(dataDTO.getValueD(), dataDTO.getGroupName(),
    dataDTO.getColName());
    } JFreeChart chart = ChartFactory.createLineChart(picTitle, paraDTO
    .getNameX(), paraDTO.getNameY(), dataset,
    PlotOrientation.VERTICAL, true, true, true);

    TextTitle textTitle = chart.getTitle();
    CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    CategoryAxis domainAxis = categoryplot.getDomainAxis();
    domainAxis.setMaximumCategoryLabelLines(20);

    textTitle.setFont(new Font("", Font.PLAIN, 20));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 12));
    textTitle.setFont(new Font("", Font.PLAIN, 20));
    domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
    domainAxis.setLabelFont(new Font("", Font.PLAIN, 12));
    numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
    numberaxis.setLabelFont(new Font("", Font.PLAIN, 12));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 12)); LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer)categoryplot 
    .getRenderer();  lineandshaperenderer.setBaseShapesVisible(true); 
    lineandshaperenderer.setBaseLinesVisible(true); lineandshaperenderer.setBaseItemLabelGenerator(new 
    StandardCategoryItemLabelGenerator()); 
    lineandshaperenderer.setBaseItemLabelsVisible(true); 

    ChartFrame frame = new ChartFrame(picTitle, chart);
    frame.pack(); String path = this.getClass().getResource("").getPath();
    path = path.replace("%20", " ");
    int index = path.indexOf("WEB-INF");
    String imgPath = path.substring(1, index) + dirName;
    File ftmp = new File(imgPath);
    if (null != ftmp && !ftmp.exists()) {
    ftmp.mkdirs();
    }
    try {
    ChartUtilities.saveChartAsPNG(new File(imgPath + File.separator
    + picName), chart, width, height);
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println("" + imgPath + File.separator + picName
    + "");
    rtDTO.setPicName(picName);
    return rtDTO;
    }
      

  4.   

    /**
     * 生成饼图
     * @param list 数据传输集合
     * @param paraDTO 统计图些属性的封装
     * @return 返回属性的封装
     */
    public ReturnPicDTO getPieChart(List<DataDTO> list, ParamPicDTO paraDTO) {
    ReturnPicDTO rtDTO = new ReturnPicDTO();
    DefaultPieDataset dataset = new DefaultPieDataset(); String picTitle = ""; //  图片上显示的标题
    String picName = ""; //  图片名称
    String dirName = ""; //  保存到哪个目录
    int width = 640; // 统计图默认宽
    int height = 480; //  统计图默认高 if (null != paraDTO) {
    picTitle = paraDTO.getPicTitle();
    picName = paraDTO.getPicName();
    dirName = paraDTO.getPicDir();
    width = paraDTO.getPicWidth();
    height = paraDTO.getPicHeight();
    } if (null == list || list.size()==0){
    list.add(new DataDTO("所查项无数据","所查项无数据",0.0));
    }
    for (int i = 0; i < list.size(); i++) {
    DataDTO dataDTO = list.get(i);
    dataset.setValue(dataDTO.getColName(), dataDTO.getValueD());
    }

    JFreeChart chart = ChartFactory.createPieChart(picTitle, dataset, true,
    true, false);

    TextTitle textTitle = chart.getTitle();
    textTitle.setFont(new Font("", Font.PLAIN, 20));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 12));
    textTitle.setFont(new Font("", Font.PLAIN, 20));
    chart.getLegend().setItemFont(new Font("", Font.PLAIN, 12));

    // 取得饼图plot对象
    PiePlot plot = (PiePlot)chart.getPlot();
    //是否显示线 fasle就不显示了
    plot.setLabelLinksVisible(false);
    //设置图表标签字体
    plot.setLabelFont(new Font("SansSerif",Font.ITALIC,12));
    plot.setNoDataMessage("No data available");
    plot.setCircular(true);
    plot.setLabelGap(0.01D);//间距
    //显示数据
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{1}"));

    ChartFrame frame = new ChartFrame(picTitle, chart);
    frame.pack(); String path = this.getClass().getResource("").getPath();
    path = path.replace("%20", " ");
    int index = path.indexOf("WEB-INF");
    String imgPath = path.substring(1, index) + dirName;
    File ftmp = new File(imgPath);
    if (null != ftmp && !ftmp.exists()) {
    ftmp.mkdirs();
    }
    try {
    ChartUtilities.saveChartAsPNG(new File(imgPath + File.separator
    + picName), chart, width, height);
    } catch (IOException e) {
    e.printStackTrace();
    }
    System.out.println("" + imgPath + File.separator + picName
    + "");
    rtDTO.setPicName(picName);
    return rtDTO;
    } public static void main(String[] args) {
    GetPicImpl getPic = new GetPicImpl(); List<DataDTO> listBar = new ArrayList<DataDTO>();
    listBar.add(new DataDTO("第一医院", "肺结核", 2.2));
    listBar.add(new DataDTO("第一医院", "HIV", 2.2));
    listBar.add(new DataDTO("第一医院", "流感", 2.5));
    listBar.add(new DataDTO("第二医院", "肺结核", 2.7));
    listBar.add(new DataDTO("第二医院", "HIV", 4.7));
    listBar.add(new DataDTO("第二医院", "流感", 2.7));
    listBar.add(new DataDTO("第三医院", "肺结核", 3.2));
    listBar.add(new DataDTO("第三医院", "HIV", 3.1));
    listBar.add(new DataDTO("第三医院", "流感", 6.2));
    ParamPicDTO paraDTO1 = new ParamPicDTO();
    paraDTO1.setPicWidth(640);
    paraDTO1.setPicHeight(480);
    paraDTO1.setNameX("各项数据");
    paraDTO1.setNameY("数值");
    paraDTO1.setPicTitle("数据分析");
    paraDTO1.setPicDir("cartogram"); //设置保存到哪个文件夹下
    paraDTO1.setPicName("aaa.png"); //设置图片名称
    getPic.getBarChart(listBar, paraDTO1); List<DataDTO> listLine = new ArrayList<DataDTO>();
    listLine.add(new DataDTO("1月", "第一医院", 33.5));
    listLine.add(new DataDTO("1月", "第二医院", 41.7));
    listLine.add(new DataDTO("1月", "第三医院", 45.7));
    listLine.add(new DataDTO("2月", "第一医院", 57.7));
    listLine.add(new DataDTO("2月", "第二医院", 47.7));
    listLine.add(new DataDTO("2月", "第三医院", 30.7));
    listLine.add(new DataDTO("3月", "第一医院", 22.7));
    listLine.add(new DataDTO("3月", "第二医院", 11.7));
    listLine.add(new DataDTO("3月", "第三医院", 39.7));
    listLine.add(new DataDTO("4月", "第一医院", 22.7));
    listLine.add(new DataDTO("4月", "第二医院", 19.7));
    listLine.add(new DataDTO("4月", "第三医院", 43.7));
    listLine.add(new DataDTO("5月", "第一医院", 30.7));
    listLine.add(new DataDTO("5月", "第二医院", 20.7));
    listLine.add(new DataDTO("5月", "第三医院", 15.7)); ParamPicDTO paraDTO2 = new ParamPicDTO();
    paraDTO2.setPicWidth(640);
    paraDTO2.setPicHeight(480);
    paraDTO2.setNameX("各项数据");
    paraDTO2.setNameY("数值");
    paraDTO2.setPicTitle("数据分析");
    paraDTO2.setPicDir("cartogram"); //设置保存到哪个文件夹下
    paraDTO2.setPicName("bbb.png"); //设置图片名称
    getPic.getLineChart(listLine, paraDTO2); List<DataDTO> listPie = new ArrayList<DataDTO>();
    listPie.add(new DataDTO("第一医院", "", 7.7));
    listPie.add(new DataDTO("第二医院", "", 3.7));
    listPie.add(new DataDTO("第三医院", "", 4.7));
    listPie.add(new DataDTO("第五医院", "", 1.7));
    listPie.add(new DataDTO("第六医院", "", 11.7));
    ParamPicDTO paraDTO3 = new ParamPicDTO();
    paraDTO3.setPicWidth(640);
    paraDTO3.setPicHeight(480);
    paraDTO3.setNameX("各项数据");
    paraDTO3.setNameY("数值");
    paraDTO3.setPicTitle("数据分析");
    paraDTO3.setPicDir("cartogram"); //设置保存到哪个文件夹下
    paraDTO3.setPicName("ccc.png"); //设置图片名称
    ReturnPicDTO rpd = getPic.getPieChart(listPie, paraDTO3);
    }}
      

  5.   

    给你个以前整理过的代码,可以直接调用
    import java.awt.Color;
    import java.awt.Font;
    import java.awt.RenderingHints;
    import java.io.FileOutputStream;
    import java.text.DecimalFormat;
    import java.text.NumberFormat;import org.jfree.chart.ChartFactory;
    import org.jfree.chart.ChartUtilities;
    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.axis.ValueAxis;
    import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
    import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
    import org.jfree.chart.plot.CategoryPlot;
    import org.jfree.chart.plot.PieLabelLinkStyle;
    import org.jfree.chart.plot.PiePlot3D;
    import org.jfree.chart.plot.PlotOrientation;
    import org.jfree.chart.plot.SpiderWebPlot;
    import org.jfree.chart.renderer.category.BarRenderer;
    import org.jfree.chart.renderer.category.LineAndShapeRenderer;
    import org.jfree.chart.renderer.category.StackedBarRenderer;
    import org.jfree.chart.title.LegendTitle;
    import org.jfree.chart.title.TextTitle;
    import org.jfree.data.category.CategoryDataset;
    import org.jfree.data.general.DatasetUtilities;
    import org.jfree.data.general.DefaultPieDataset;
    import org.jfree.data.general.PieDataset;
    import org.jfree.ui.RectangleEdge;/**
     * 实际取色的时候一定要16位的,这样比较准确
     * 
     * @author new
     */
    public class CreateChartServiceImpl {

    private static final String CHART_PATH = "E:/"; public static void main(String[] args) {
    CreateChartServiceImpl pm = new CreateChartServiceImpl();
    // 生成饼状图
    pm.makePieChart();
    // 生成单组柱状图
    pm.makeBarChart();
    // 生成多组柱状图
    pm.makeBarGroupChart();
    // 生成堆积柱状图
    pm.makeStackedBarChart();
    // 生成折线图
    pm.makeLineAndShapeChart();
    } /**
     * 生成折线图
     */
    public void makeLineAndShapeChart() {
    double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
    { 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
    String[] rowKeys = { "苹果", "梨子", "葡萄" };
    String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
    CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
    createTimeXYChar("折线图", "x轴", "y轴", dataset, CHART_PATH);
    } /**
     * 生成分组的柱状图
     */
    public void makeBarGroupChart() {
    double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
    { 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
    String[] rowKeys = { "苹果", "梨子", "葡萄" };
    String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
    CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
    createBarChart(dataset, "x坐标", "y坐标", "柱状图", CHART_PATH);
    } /**
     * 生成柱状图
     */
    public void makeBarChart() {
    double[][] data = new double[][] { { 672, 766, 223, 540, 126 } };
    String[] rowKeys = { "苹果" };
    String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
    CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
    createBarChart(dataset, "x坐标", "y坐标", "柱状图", CHART_PATH);
    } /**
     * 生成堆栈柱状图
     */
    public void makeStackedBarChart() {
    double[][] data = new double[][] { { 672, 766, 223, 540, 126 },
    { 325, 521, 210, 340, 106 }, { 332, 256, 523, 240, 526 } };
    String[] rowKeys = { "苹果", "梨子", "Per" };
    String[] columnKeys = { "北京", "上海", "广州", "成都", "深圳" };
    CategoryDataset dataset = getBarData(data, rowKeys, columnKeys);
    createStackedBarChart(dataset, "x坐标:地区", "y坐标:水果种类", "柱状图",
    CHART_PATH);
    } /**
     * 生成饼状图
     */
    public void makePieChart() {
    double[] data = { 9, 51, 16, 24 };
    String[] keys = { "ID", "主机名", "软件数量", "用户数" }; createValidityComparePimChar(getDataPieSetByUtil(data, keys), "饼状图",
    CHART_PATH);
    } // 柱状图,折线图 数据集
    public CategoryDataset getBarData(double[][] data, String[] rowKeys,
    String[] columnKeys) {
    return DatasetUtilities
    .createCategoryDataset(rowKeys, columnKeys, data); } // 饼状图 数据集
    public PieDataset getDataPieSetByUtil(double[] data,
    String[] datadescription) { if (data != null && datadescription != null) {
    if (data.length == datadescription.length) {
    DefaultPieDataset dataset = new DefaultPieDataset();
    for (int i = 0; i < data.length; i++) {
    dataset.setValue(datadescription[i], data[i]);
    }
    return dataset;
    } } return null;
    } /**
     * 柱状图
     * 
     *@param dataset
     *            数据集
     * @param xName
     *            x轴的说明(如种类,时间等)
     * @param yName
     *            y轴的说明(如速度,时间等)
     * @param chartTitle
     *            图标题
     * @param charName
     *            生成图片的名字
     * @return
     */
    public String createBarChart(CategoryDataset dataset, String xName,
    String yName, String chartTitle, String path) {
    JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题
    xName, // 目录轴的显示标签
    yName, // 数值轴的显示标签
    dataset, // 数据集
    PlotOrientation.VERTICAL, // 图表方向:水平、垂直
    true, // 是否显示图例(对于简单的柱状图必须是false)
    false, // 是否生成工具
    false // 是否生成URL链接
    );
    /*
     * VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭,
     * 使用的关闭抗锯齿后,字体尽量选择12到14号的宋体字,这样文字最清晰好看
     */
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    chart.setTextAntiAlias(false);
    chart.setBackgroundPaint(Color.white);
    Font titleFont = new Font("黑体", Font.PLAIN, 20); // 图片标题
    chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
    Font font = new Font("宋体", 10, 15);
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
    // create plot
    CategoryPlot plot = (CategoryPlot) chart.getPlot();
    plot.getDomainAxis().setLabelFont(font);
    plot.getDomainAxis().setTickLabelFont(font);
    plot.getRangeAxis().setLabelFont(font);
    plot.getRangeAxis().setTickLabelFont(font);
    // 设置横虚线可见
    plot.setRangeGridlinesVisible(true);
    // 虚线色彩
    plot.setRangeGridlinePaint(Color.gray); // 数据轴精度
    NumberAxis vn = (NumberAxis) plot.getRangeAxis();
    // vn.setAutoRangeIncludesZero(true);
    DecimalFormat df = new DecimalFormat("#0.00");
    vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式
    // x轴设置
    CategoryAxis domainAxis = plot.getDomainAxis();
    domainAxis.setLabelFont(labelFont);// 轴标题
    domainAxis.setTickLabelFont(labelFont);// 轴数值 // Lable(Math.PI/3.0)度倾斜
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions
    // .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(1.5f);// 横轴上的 Lable 是否完整显示 // 设置距离图片左端距离
    domainAxis.setLowerMargin(0.1);
    // 设置距离图片右端距离
    domainAxis.setUpperMargin(0.1);
    // 设置 columnKey 是否间隔显示
    // domainAxis.setSkipCategoryLabelsToFit(true); plot.setDomainAxis(domainAxis);
    // 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
    plot.setBackgroundPaint(new Color(255, 255, 204)); // y轴设置
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabelFont(labelFont);
    rangeAxis.setTickLabelFont(labelFont);
    // 设置最高的一个 Item 与图片顶端的距离
    rangeAxis.setUpperMargin(0.15);
    // 设置最低的一个 Item 与图片底端的距离
    rangeAxis.setLowerMargin(0.15);
    plot.setRangeAxis(rangeAxis); BarRenderer renderer = new BarRenderer();
    // 设置柱子宽度
    renderer.setMaximumBarWidth(0.04);
    // 设置柱子高度
    renderer.setMinimumBarLength(0.2);
    // 设置柱子边框颜色
    renderer.setBaseOutlinePaint(Color.BLACK);
    // 设置柱子边框可见
    renderer.setDrawBarOutline(true); // 设置柱的颜色
    renderer.setSeriesPaint(0, new Color(204, 255, 255));
    renderer.setSeriesPaint(1, new Color(153, 204, 255));
    renderer.setSeriesPaint(2, new Color(51, 204, 204)); // 设置每个地区所包含的平行柱的之间距离
    renderer.setItemMargin(0.0); // 显示每个柱的数值,并修改该数值的字体属性
    renderer.setIncludeBaseInRange(true);
    renderer
    .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer);
    // 设置柱的透明度
    plot.setForegroundAlpha(1.0f); // 设置图片中item部分中文乱码
    chart.getLegend().setItemFont(font); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "Bar.png";
    fos_jpg = new FileOutputStream(chartName);
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 800, 600, true, 10);
    return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  6.   

    /**
     * 横向图
     * 
     * @param dataset
     *            数据集
     * @param xName
     *            x轴的说明(如种类,时间等)
     * @param yName
     *            y轴的说明(如速度,时间等)
     * @param chartTitle
     *            图标题
     * @param charName
     *            生成图片的名字
     * @return
     */
    public String createHorizontalBarChart(CategoryDataset dataset,
    String xName, String yName, String chartTitle, String path) {
    JFreeChart chart = ChartFactory.createBarChart(chartTitle, // 图表标题
    xName, // 目录轴的显示标签
    yName, // 数值轴的显示标签
    dataset, // 数据集
    PlotOrientation.VERTICAL, // 图表方向:水平、垂直
    true, // 是否显示图例(对于简单的柱状图必须是false)
    false, // 是否生成工具
    false // 是否生成URL链接
    ); CategoryPlot plot = chart.getCategoryPlot();
    // 数据轴精度
    NumberAxis vn = (NumberAxis) plot.getRangeAxis();
    // 设置刻度必须从0开始
    // vn.setAutoRangeIncludesZero(true);
    DecimalFormat df = new DecimalFormat("#0.00");
    vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式 CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
    // Lable
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); domainAxis.setLabelFont(labelFont);// 轴标题
    domainAxis.setTickLabelFont(labelFont);// 轴数值 domainAxis.setMaximumCategoryLabelWidthRatio(0.8f);// 横轴上的 Lable 是否完整显示
    // domainAxis.setVerticalCategoryLabels(false);
    plot.setDomainAxis(domainAxis); ValueAxis rangeAxis = plot.getRangeAxis();
    // 设置最高的一个 Item 与图片顶端的距离
    rangeAxis.setUpperMargin(0.15);
    // 设置最低的一个 Item 与图片底端的距离
    rangeAxis.setLowerMargin(0.15);
    plot.setRangeAxis(rangeAxis);
    BarRenderer renderer = new BarRenderer();
    // 设置柱子宽度
    renderer.setMaximumBarWidth(0.03);
    // 设置柱子高度
    renderer.setMinimumBarLength(30); renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱的颜色
    renderer.setSeriesPaint(0, Color.GREEN);
    renderer.setSeriesPaint(1, new Color(0, 0, 255));
    // 设置每个地区所包含的平行柱的之间距离
    renderer.setItemMargin(0.5);
    // 显示每个柱的数值,并修改该数值的字体属性
    renderer
    .setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
    // 设置柱的数值可见
    renderer.setBaseItemLabelsVisible(true); plot.setRenderer(renderer);
    // 设置柱的透明度
    plot.setForegroundAlpha(0.6f); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "HorizontalBar.png";
    fos_jpg = new FileOutputStream(chartName);
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
    return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 饼状图
     * 
     * @param dataset
     *            数据集
     * @param chartTitle
     *            图标题
     * @param path
     *            生成图的路径
     * @return
     */
    public String createValidityComparePimChar(PieDataset dataset,
    String chartTitle, String path) {
    JFreeChart chart = ChartFactory.createPieChart3D(chartTitle, // chart
    // title
    dataset,// data
    true,// include legend
    true, false); // 使下说明标签字体清晰,去锯齿类似于
    chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    chart.setTextAntiAlias(false);
    // 图片背景色
    chart.setBackgroundPaint(Color.white);
    // 设置图标题的字体重新设置title
    Font font = new Font("宋体", 10, 15);
    Font titleFont = new Font("黑体", Font.PLAIN, 20); // 图片标题
    chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont)); PiePlot3D plot = (PiePlot3D) chart.getPlot();
    // 图片中显示百分比:默认方式 plot.setBackgroundPaint(Color.white);
    plot.setLabelFont(font);
    plot.setCircular(true); // 指定饼图轮廓线的颜色
    plot.setBaseSectionOutlinePaint(Color.BLACK);
    plot.setBaseSectionPaint(Color.BLACK); // 设置无数据时的信息
    plot.setNoDataMessage("无对应的数据,请重新查询。"); // 设置无数据时的信息显示颜色
    plot.setNoDataMessagePaint(Color.red); // 图片中显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
    plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0} {2})",
    NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
    // 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
    plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}={1}")); // 设置指示线风格
    plot.setLabelLinkStyle(PieLabelLinkStyle.STANDARD); // 指定图片的透明度(0.0-1.0)
    plot.setForegroundAlpha(0.65f);
    // 指定显示的饼图上圆形(false)还椭圆形(true)
    plot.setCircular(false, true); // 设置第一个 饼块section 的开始位置,默认是12点钟方向
    plot.setStartAngle(90); // 转换成2D图
    // PiePlot3D plot3D = (PiePlot3D)plot;
    // plot3D.setDepthFactor(0); // 设置图片中item部分中文乱码
    chart.getLegend().setItemFont(font); // 设置分饼颜色
    // plot.setSectionPaint(pieKeys[0], new Color(244, 194, 144));
    // plot.setSectionPaint(pieKeys[1], new Color(144, 233, 144)); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "Pie.png";
    fos_jpg = new FileOutputStream(chartName);
    // 高宽的设置影响椭圆饼图的形状
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 800, 368); return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 折线图
     * 
     * @param chartTitle
     * @param x
     * @param y
     * @param xyDataset
     * @param charName
     * @return
     */
    public String createTimeXYChar(String chartTitle, String x, String y,
    CategoryDataset xyDataset, String path) { JFreeChart chart = ChartFactory.createLineChart(chartTitle, x, y,
    xyDataset, PlotOrientation.VERTICAL, true, true, false); chart.setTextAntiAlias(false);
    chart.setBackgroundPaint(Color.WHITE);
    // 设置图标题的字体重新设置title
    Font font = new Font("宋体", 10, 15);
    Font titleFont = new Font("黑体", Font.PLAIN, 20); // 图片标题
    chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont));
    // 设置面板字体
    Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12); chart.setBackgroundPaint(Color.WHITE); CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
    // x轴 // 分类轴网格是否可见
    categoryplot.setDomainGridlinesVisible(true);
    // y轴 //数据轴网格是否可见
    categoryplot.setRangeGridlinesVisible(true); categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩 categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩 categoryplot.setBackgroundPaint(Color.lightGray); // 设置轴和面板之间的距离
    // categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D)); CategoryAxis domainAxis = categoryplot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题
    domainAxis.setTickLabelFont(labelFont);// 轴数值 ValueAxis rangeAxis = categoryplot.getRangeAxis();
    rangeAxis.setLabelFont(font);
    // rangeAxis.setLabelPaint(Color.BLUE); // 字体颜色
    rangeAxis.setTickLabelFont(font); domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
    // Lable
    // 45度倾斜
    // 设置距离图片左端距离
    domainAxis.setLowerMargin(0.0);
    // 设置距离图片右端距离
    domainAxis.setUpperMargin(0.0); NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
    numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
    numberaxis.setAutoRangeIncludesZero(true); // 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
    LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
    .getRenderer(); lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
    lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见 // 显示折点数据
    // lineandshaperenderer.setBaseItemLabelGenerator(new
    // StandardCategoryItemLabelGenerator());
    // lineandshaperenderer.setBaseItemLabelsVisible(true); // 设置图片中item部分中文乱码
    chart.getLegend().setItemFont(font); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "LineAndShape.png";
    fos_jpg = new FileOutputStream(chartName); // 将报表保存为png文件
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 800, 600); return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    } /**
     * 雷达图
     * 
     * @param chartTitle
     * @param x
     * @param y
     * @param xyDataset
     * @param charName
     * @return
     */
    public String createPolarChart(String chartTitle,
    CategoryDataset categorydataset, String path) {
    SpiderWebPlot dataset = new TickMarkSpiderWebPlot(categorydataset); JFreeChart chart = new JFreeChart(chartTitle, new Font("宋体",
    Font.PLAIN, 15), dataset, false); chart.setTextAntiAlias(false);
    chart.setBackgroundPaint(Color.WHITE);
    // 设置图标题的字体重新设置title
    Font font = new Font("宋体", 10, 15);
    Font titleFont = new Font("黑体", Font.PLAIN, 20); // 图片标题
    chart.setTitle(new TextTitle(chart.getTitle().getText(), titleFont)); chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
    RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    chart.setBackgroundPaint(Color.white);
    TickMarkSpiderWebPlot pieplot = (TickMarkSpiderWebPlot) chart.getPlot();
    pieplot.setBackgroundPaint(Color.white);

    // 设定背景透明度(0-1.0之间)
    pieplot.setBackgroundAlpha(0.6f);

    // 设定前景透明度(0-1.0之间)
    pieplot.setForegroundAlpha(0.8f);
    LegendTitle legendtitle = new LegendTitle(dataset);
    legendtitle.setPosition(RectangleEdge.BOTTOM);
    chart.addSubtitle(legendtitle); // 设置图片中item部分中文乱码
    chart.getLegend().setItemFont(font); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "Polar.png";
    fos_jpg = new FileOutputStream(chartName); // 将报表保存为png文件
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 800, 600); return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }
      

  7.   

    /**
     * 堆栈柱状图
     * 
     * @param dataset
     * @param xName
     * @param yName
     * @param chartTitle
     * @param charName
     * @return
     */
    public String createStackedBarChart(CategoryDataset dataset, String xName,
    String yName, String chartTitle, String path) {
    // 1:得到 CategoryDataset // 2:JFreeChart对象
    JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // 图表标题
    xName, // 目录轴的显示标签
    yName, // 数值轴的显示标签
    dataset, // 数据集
    PlotOrientation.VERTICAL, // 图表方向:水平、垂直
    true, // 是否显示图例(对于简单的柱状图必须是false)
    false, // 是否生成工具
    false // 是否生成URL链接
    );
    // 图例字体清晰
    chart.setTextAntiAlias(false); chart.setBackgroundPaint(Color.WHITE); // 2 .2 主标题对象 主标题对象是 TextTitle 类型
    chart.setTitle(new TextTitle(chartTitle, new Font("黑体", Font.PLAIN,
    20)));
    // 2 .2.1:设置中文
    // x,y轴坐标字体
    Font labelFont = new Font("宋体", 10, 15); // 2 .3 Plot 对象 Plot 对象是图形的绘制结构对象
    CategoryPlot plot = chart.getCategoryPlot(); // 设置横虚线可见
    plot.setRangeGridlinesVisible(true);
    // 虚线色彩
    plot.setRangeGridlinePaint(Color.gray); // 数据轴精度
    NumberAxis vn = (NumberAxis) plot.getRangeAxis();
    // // 设置最大值是1
    // vn.setUpperBound(2000);
    // 设置数据轴坐标从0开始
    // vn.setAutoRangeIncludesZero(true);
    // 数据显示格式是百分比
    DecimalFormat df = new DecimalFormat("#0.00");
    vn.setNumberFormatOverride(df); // 数据轴数据标签的显示格式
    // DomainAxis (区域轴,相当于 x 轴), RangeAxis (范围轴,相当于 y 轴)
    CategoryAxis domainAxis = plot.getDomainAxis(); domainAxis.setLabelFont(labelFont);// 轴标题
    domainAxis.setTickLabelFont(labelFont);// 轴数值 // x轴坐标太长,建议设置倾斜,如下两种方式选其一,两种效果相同
    // 倾斜(1)横轴上的 Lable 45度倾斜
    domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
    // 倾斜(2)Lable(Math.PI 3.0)度倾斜
    // domainAxis.setCategoryLabelPositions(CategoryLabelPositions
    //  .createUpRotationLabelPositions(Math.PI / 3.0)); domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);// 横轴上的 Lable 是否完整显示 plot.setDomainAxis(domainAxis); // y轴设置
    ValueAxis rangeAxis = plot.getRangeAxis();
    rangeAxis.setLabelFont(labelFont);
    rangeAxis.setTickLabelFont(labelFont);
    // 设置最高的一个 Item 与图片顶端的距离
    rangeAxis.setUpperMargin(0.15);
    // 设置最低的一个 Item 与图片底端的距离
    rangeAxis.setLowerMargin(0.15);
    plot.setRangeAxis(rangeAxis); // Renderer 对象是图形的绘制单元
    StackedBarRenderer renderer = new StackedBarRenderer();
    // 设置柱子宽度
    renderer.setMaximumBarWidth(0.05);
    // 设置柱子高度
    renderer.setMinimumBarLength(0.1);
    // 设置柱的边框颜色
    renderer.setBaseOutlinePaint(Color.BLACK);
    // 设置柱的边框可见
    renderer.setDrawBarOutline(true); // 设置柱的颜色(可设定也可默认)
    renderer.setSeriesPaint(0, new Color(204, 255, 204));
    renderer.setSeriesPaint(1, new Color(255, 204, 153)); // 设置每个地区所包含的平行柱的之间距离
    renderer.setItemMargin(0.4); plot.setRenderer(renderer);
    // 设置柱的透明度(如果是3D的必须设置才能达到立体效果,如果是2D的设置则使颜色变淡)
    // plot.setForegroundAlpha(0.65f);

    // 设置图片中item部分中文乱码
    chart.getLegend().setItemFont(labelFont); FileOutputStream fos_jpg = null;
    try {
    String chartName = path + "StackedBar.png";
    fos_jpg = new FileOutputStream(chartName);
    ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 500, true, 10);
    return chartName;
    } catch (Exception e) {
    e.printStackTrace();
    return null;
    } finally {
    try {
    fos_jpg.close();
    } catch (Exception e) {
    e.printStackTrace();
    }
    }
    }}import java.awt.AlphaComposite;
    import java.awt.Composite;
    import java.awt.Graphics2D;
    import java.awt.font.FontRenderContext;
    import java.awt.font.LineMetrics;
    import java.awt.geom.AffineTransform;
    import java.awt.geom.Arc2D;
    import java.awt.geom.Line2D;
    import java.awt.geom.Point2D;
    import java.awt.geom.Rectangle2D;
    import java.text.NumberFormat;import org.jfree.chart.plot.SpiderWebPlot;
    import org.jfree.data.category.CategoryDataset;/**
     * 重写piderPlot的drawLabel方法,以便在数轴上显示刻度,
     * 用于雷达图显示,(该项目目前未用)
     * 
     */
    public class TickMarkSpiderWebPlot extends SpiderWebPlot { private static final long serialVersionUID = 1L;
    private int ticks = DEFAULT_TICKS;
    private static final int DEFAULT_TICKS = 5;
    private NumberFormat format = NumberFormat.getInstance();
    private static final double PERPENDICULAR = 90;
    private static final double TICK_SCALE = 0.015;
    private int valueLabelGap = DEFAULT_GAP;
    private static final int DEFAULT_GAP = 10;
    private static final double THRESHOLD = 15; TickMarkSpiderWebPlot(CategoryDataset createCategoryDataset) {
    super(createCategoryDataset);
    } /**
     * 用于雷达图显示,(该项目目前未用)
     */
    @Override
    protected void drawLabel(final Graphics2D g2, final Rectangle2D plotArea,
    final double value, final int cat, final double startAngle,
    final double extent) {
    super.drawLabel(g2, plotArea, value, cat, startAngle, extent);
    final FontRenderContext frc = g2.getFontRenderContext();
    final double[] transformed = new double[2];
    final double[] transformer = new double[2];
    final Arc2D arc1 = new Arc2D.Double(plotArea, startAngle, 0, Arc2D.OPEN); for (int i = 1; i <= ticks; i++) { final Point2D point1 = arc1.getEndPoint(); final double deltaX = plotArea.getCenterX();
    final double deltaY = plotArea.getCenterY();
    double labelX = point1.getX() - deltaX;
    double labelY = point1.getY() - deltaY; final double scale = ((double) i / (double) ticks);
    final AffineTransform tx = AffineTransform.getScaleInstance(scale,
    scale);
    final AffineTransform pointTrans = AffineTransform
    .getScaleInstance(scale + TICK_SCALE, scale + TICK_SCALE);
    transformer[0] = labelX;
    transformer[1] = labelY;
    pointTrans.transform(transformer, 0, transformed, 0, 1);
    final double pointX = transformed[0] + deltaX;
    final double pointY = transformed[1] + deltaY;
    tx.transform(transformer, 0, transformed, 0, 1);
    labelX = transformed[0] + deltaX;
    labelY = transformed[1] + deltaY; double rotated = (PERPENDICULAR); AffineTransform rotateTrans = AffineTransform.getRotateInstance(
    Math.toRadians(rotated), labelX, labelY);
    transformer[0] = pointX;
    transformer[1] = pointY;
    rotateTrans.transform(transformer, 0, transformed, 0, 1);
    final double x1 = transformed[0];
    final double y1 = transformed[1]; rotated = (-PERPENDICULAR);
    rotateTrans = AffineTransform.getRotateInstance(Math
    .toRadians(rotated), labelX, labelY); rotateTrans.transform(transformer, 0, transformed, 0, 1); final Composite saveComposite = g2.getComposite();
    g2.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER,
    1.0f)); g2.draw(new Line2D.Double(transformed[0], transformed[1], x1, y1)); if (startAngle == this.getStartAngle()) {
    final String label = format
    .format(((double) i / (double) ticks)
    * this.getMaxValue());
    final LineMetrics lm = getLabelFont()
    .getLineMetrics(label, frc);
    final double ascent = lm.getAscent();
    if (Math.abs(labelX - plotArea.getCenterX()) < THRESHOLD) {
    labelX += valueLabelGap;
    labelY += ascent / (float) 2;
    } else if (Math.abs(labelY - plotArea.getCenterY()) < THRESHOLD) {
    labelY += valueLabelGap;
    } else if (labelX >= plotArea.getCenterX()) {
    if (labelY < plotArea.getCenterY()) {
    labelX += valueLabelGap;
    labelY += valueLabelGap;
    } else {
    labelX -= valueLabelGap;
    labelY += valueLabelGap;
    }
    } else {
    if (labelY > plotArea.getCenterY()) {
    labelX -= valueLabelGap;
    labelY -= valueLabelGap;
    } else {
    labelX += valueLabelGap;
    labelY -= valueLabelGap;
    }
    }
    g2.setPaint(getLabelPaint());
    g2.setFont(getLabelFont());
    g2.drawString(label, (float) labelX, (float) labelY);
    }
    g2.setComposite(saveComposite);
    }
    }
    }能支持多种二维图形的输出,饼状图、单组柱状图、多组柱状图、堆积柱状图、折线图、雷达图