您的位置:首页 > 其它

jfreechart生成图表

2014-05-14 11:11 295 查看
JFreeChart是JAVA平台上的一个开放的图表绘制类库。它完全使用JAVA语言编写,是为applications, applets, servlets 以及JSP等使用所设计。JFreeChart可生成饼图(pie charts)、柱状图(barcharts)、散点图(scatterplots)、时序图(timeseries)、甘特图(Ganttcharts)等等多种图表,并且可以产生PNG和JPEG格式的输出,还可以与PDF和EXCEL关联。使用前要导入jar包下面是一点简单的例子:1,生成饼图
package cn.itcast.jfreechart;

import java.awt.Font;
import java.io.File;

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;

public class App {

public static void main(String[] args) throws Exception {
//
String title = "各大公司JEE AS市场占有率统计" ;
DefaultPieDataset ds = new DefaultPieDataset();
ds.setValue("IBM", 2000);
ds.setValue("ORACLE", 3500);
ds.setValue("JBOSS", 1570);
ds.setValue("用友", 4400);
JFreeChart chart = ChartFactory.createPieChart3D(title, ds, true, false, false);

//中文
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//标题字体
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 18));

//绘图区
PiePlot plot = (PiePlot) chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.PLAIN, 15));

//背景
//chart.setBackgroundImage(ImageIO.read(new File("f:/sunset.jpg")));//图表区背景
//plot.setBackgroundImage(ImageIO.read(new File("f:/water.jpg")));

//设置分裂效果
plot.setExplodePercent("IBM", 0.1f);
plot.setExplodePercent("JBOSS", 0.2f);

//设置前景色透明度
plot.setForegroundAlpha(0.7f);

//设置标签生成器
//{0}:公司名称
//{1}:销量
//{2}:百分比
//{3}:总量
//{4}:
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({1}/{3}-{2})"));
ChartUtilities.saveChartAsJPEG(new File("f:\\pie.jpg"), chart, 800, 500);
}
}
效果如下:2,生成柱状图
package cn.itcast.jfreechart;

import java.awt.Font;
import java.io.File;

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;

public class AppBar {

public static void main(String[] args) throws Exception {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(2000, "IBM", "一季度");
ds.addValue(2300, "ORACLE", "一季度");
ds.addValue(2800, "JBOSS", "一季度");
ds.addValue(3300, "用友", "一季度");

ds.addValue(4800, "IBM", "二季度");
ds.addValue(4300, "ORACLE", "二季度");
ds.addValue(3200, "JBOSS", "二季度");
ds.addValue(1800, "用友", "二季度");

ds.addValue(1500, "IBM", "三季度");
ds.addValue(2600, "ORACLE", "三季度");
ds.addValue(3900, "JBOSS", "三季度");
ds.addValue(2100, "用友", "三季度");

String title = "前三季度各大公司JEE AS销量统计" ;
JFreeChart chart = ChartFactory.createBarChart3D(title, "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);

//中文
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//大标题

//提示条
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));

CategoryPlot plot = (CategoryPlot) chart.getPlot();
//域轴字体
plot.getDomainAxis().setLabelFont(new Font("宋体", Font.BOLD, 18));
plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));//小标签字体

//range
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));

plot.setForegroundAlpha(0.6f);

ChartUtilities.saveChartAsJPEG(new File("f:\\bar.jpg"), chart, 800, 500);
}
}
效果如下:3,生成折线图:
package cn.itcast.jfreechart;

import java.awt.Font;
import java.io.File;

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;

public class AppLine {

public static void main(String[] args) throws Exception {
DefaultCategoryDataset ds = new DefaultCategoryDataset();
ds.addValue(2000, "IBM", "一季度");
ds.addValue(2300, "ORACLE", "一季度");
ds.addValue(2800, "JBOSS", "一季度");
ds.addValue(3300, "用友", "一季度");

ds.addValue(4800, "IBM", "二季度");
ds.addValue(4300, "ORACLE", "二季度");
ds.addValue(3200, "JBOSS", "二季度");
ds.addValue(1800, "用友", "二季度");

ds.addValue(1500, "IBM", "三季度");
ds.addValue(2600, "ORACLE", "三季度");
ds.addValue(3900, "JBOSS", "三季度");
ds.addValue(2100, "用友", "三季度");

String title = "前三季度各大公司JEE AS销量统计" ;
JFreeChart chart = ChartFactory.createLineChart(title, "季度", "销量(单位:万台)", ds, PlotOrientation.VERTICAL, true, false, false);

//中文
chart.getTitle().setFont(new Font("宋体", Font.BOLD, 25));//大标题

//提示条
chart.getLegend().setItemFont(new Font("宋体", Font.BOLD, 15));

CategoryPlot plot = (CategoryPlot) chart.getPlot();
//域轴字体
plot.getDomainAxis().setLabelFont(new Font("宋体", Font.BOLD, 18));
plot.getDomainAxis().setTickLabelFont(new Font("宋体", Font.PLAIN, 15));//小标签字体

//range
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.BOLD, 15));

plot.setForegroundAlpha(0.6f);

ChartUtilities.saveChartAsJPEG(new File("f:\\line.jpg"), chart, 800, 500);
}
}
效果如下:示例工程:http://download.csdn.net/detail/u013614451/7344601jfreeChart使用方法之一:(纯web环境,不采用框架,原理大致相当于将图片写到流中,然后页面直接在img 的src中引用)1 .添加工具类ChartUtil
public class ChartUtil {
public  static String generatePieChart(DefaultPieDataset dataset,String title,int width,int height,HttpSession session, PrintWriter pw) {    
  
String filename = null;    
 try {    
 if (session != null)    
 {    
ChartDeleter deleter = (ChartDeleter)session.getAttribute("JFreeChart_Deleter");    
 session.removeAttribute("JFreeChart_Deleter");    
 session.setAttribute("JFreeChart_Deleter", deleter);    
}    
JFreeChart chart = ChartFactory.createPieChart3D(    
title,  // chart title    
dataset, // data    
true,  // include legend    
true,    
false );    
//  Write the chart image to the temporary directory    
ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());    
//If the last parameter is null, the chart is a "one time"-chart and will be deleted after the first serving.    
//If the last parameter is a session object, the chart remains until session time out.    
filename = ServletUtilities.saveChartAsPNG(chart, width, height, info, session);    
//  Write the image map to the PrintWriter    
 ChartUtilities.writeImageMap(pw, filename, info,true);    
 pw.flush();    
} catch (Exception e) {    
 System.out.println("Exception - " + e.toString());    
 e.printStackTrace(System.out);    
 filename = "picture_error.png"; }    
return filename;    
}    
}   
}
2、在action里统计数据,设置好数据集dataset。传到页面3、 在页面jsp里取出
DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");String p = ChartUtil.generatePieChart(piedataset," 项目收支线图",500,300,null, new PrintWriter(out));String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;<td><img src="<%= p1 %>" width=500 height=300 border=0 usemap="#<%= p %>"></td>
4、在web.xml中添加
<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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: