jFreeChart初级封装

package com.dream.commen;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;
import java.util.Map.Entry;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
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.PiePlot3D;
import org.jfree.chart.plot.Plot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.AbstractRenderer;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.category.CategoryItemRenderer;
import org.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

/**
* 使用JFreeChart生成web图表
* @author 忧里修斯
*
*/
public class WebChart {

public JFreeChart chart = null;

private String title;//标题
private int width;
private int height;
private HashMap datas;//数据集

public WebChart(String title, int width, int height) {
super();
this.title = title;
this.width = width;
this.height = height;
}


/**
* 生成饼状图
* @param request
* @param session
* @param isPercent 数值是否为百分数
* @param is3D 是否为3D饼状图
* @return 图表图片的地址URL
*/
public String createPieChart(HashMap datas,boolean isPercent,boolean is3D,HttpServletRequest request,HttpSession session){

String graphUrl = "";
//创建数据集
DefaultPieDataset dataset = new DefaultPieDataset();
//重组数据
if(null != datas && datas.size() > 0){
Set set = datas.entrySet();
for (Iterator iterator = set.iterator(); iterator.hasNext();) {
Entry entry = (Entry) iterator.next();
String key = entry.getKey().toString();
String value = entry.getValue().toString();
Number number = null;
try {
if(value.indexOf(".") != -1){//float or double
number = Float.parseFloat(value);
}else{
number = Integer.parseInt(value);
}
} catch (NumberFormatException e) {
System.out.println("/****************"+value+"格式化成数字错误******************/");
e.printStackTrace();
}
dataset.setValue(key, number);
}
//图表域
String percent = "";
if(isPercent){
percent = "%";
}
if(is3D){
chart = ChartFactory.createPieChart3D(title, dataset, true, true, true);
PiePlot3D plot3D = (PiePlot3D)chart.getPlot();
plot3D.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"+percent+""));
plot3D.setLabelFont(new Font("宋体", Font.BOLD,12));

}else{
chart = ChartFactory.createPieChart(title, dataset, true, true, true);
PiePlot plot = (PiePlot)chart.getPlot();
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}"+percent+""));
plot.setLabelFont(new Font("宋体", Font.BOLD,12));
}
/*------这两句代码解决了底部和标题汉字乱码的问题-----------*/
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));

//绘制对象
ChartRenderingInfo rinfo = new ChartRenderingInfo(new StandardEntityCollection());
//生成的图表图片
String filename = "";
try {
//图表图片的URL
filename = ServletUtilities.saveChartAsPNG(chart,width,height,rinfo,session);
graphUrl = request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
} catch (IOException e) {
e.printStackTrace();
}
}else{
System.err.println("/************请设置数据集*****************/");
}
return graphUrl;
}

/**
* 创建线图或准状图
* @param request
* @param session
* @param is3D
* @param xname 横轴名称
* @param yname 纵轴名称
* @param is3D 是否为3D图
* @param isVertical 是否垂直显示
* @param isBar 是否柱状图,若为false则为现状图
* @return
*/
public String createBarOrLineChart(DefaultCategoryDataset dataSet,String xname,String yname,boolean isBar,boolean is3D,boolean isVertical,HttpServletRequest request,HttpSession session){

String graphUrl = "";
PlotOrientation orientation = null;
if(isVertical){//垂直显示
orientation = PlotOrientation.VERTICAL;
}else{//水平
orientation = PlotOrientation.HORIZONTAL;
}
//绘图对象
CategoryItemRenderer render = null;
//获取图表域对象
CategoryPlot plot = null;
if(isBar){//柱状图
if(is3D){//3D图
chart = ChartFactory.createBarChart3D(title, xname, yname, dataSet, orientation, true, true, true);
render = new BarRenderer3D();
}else{
chart = ChartFactory.createBarChart(title, xname, yname, dataSet, orientation, true, true, true);
render = new BarRenderer();
}
}else{//线状图
if(is3D){//3D图
chart = ChartFactory.createLineChart3D(title, xname, yname, dataSet, orientation, true, true, true);
}else{
chart = ChartFactory.createLineChart(title, xname, yname, dataSet, orientation, true, true, true);
}
render = new LineAndShapeRenderer();
}
plot = chart.getCategoryPlot();

//设置图表的横轴和纵轴
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setLowerMargin(0.01);//设置距离图片左端的距离为1%
domainAxis.setUpperMargin(0.01);// 设置距离图片右端的距离为1%
domainAxis.setCategoryLabelPositionOffset(10);//图表横轴与标签的距离为10像
domainAxis.setCategoryMargin(0.1);//横轴标签之间的距离为10%

//设置纵轴的属性
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setUpperMargin(0.1);//设置最高点与图片顶端的距离为10%

//获取相应的绘图对象

render.setBaseOutlinePaint(Color.red);
//注意第一个参数0、1
render.setSeriesPaint(0,new Color(0,255,0));//设置柱子的颜色(产量)
render.setSeriesOutlinePaint(0,Color.black);//设置柱子边框的颜色(产量)
render.setSeriesPaint(1,new Color(0,0,255));//设置柱子的颜色(销量)
render.setSeriesOutlinePaint(1,Color.red);//设置柱子边框的颜色(销量)

//显示每个柱的数值,并修改该数值的字体属性
render.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
render.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
//render.setItemLabelAnchorOffset(10D);// 设置柱形图上的文字离柱子顶端的距离
render.setItemLabelFont(new Font("黑体",Font.BOLD,12));//12号黑体加粗
render.setItemLabelPaint(new Color(0,0,0));
render.setItemLabelsVisible(true);

/*------设置X轴坐标上的文字-----------*/
domainAxis.setTickLabelFont(new Font("sans-serif", Font.BOLD, 11));

/*------设置X轴的标题文字------------*/
domainAxis.setLabelFont(new Font("宋体", Font.BOLD, 12));

/*------设置Y轴坐标上的文字-----------*/
rangeAxis.setTickLabelFont(new Font("sans-serif", Font.BOLD, 12));

/*------设置Y轴的标题文字------------*/
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 12));

/*------这句代码解决了底部汉字乱码的问题-----------*/
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
plot.setRenderer(render);

//设置横纵坐标的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
//plot.setBackgroundPaint(new Color(255,255,128));

//生成的图表图片
String filename;
try {
filename = ServletUtilities.saveChartAsPNG(chart,600,400,session);
//图表图片的URL
graphUrl = request.getContextPath()+"/servlet/DisplayChart?filename="+filename;
} catch (IOException e) {
e.printStackTrace();
}
return graphUrl;
}

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值