package com.talkweb.jobseeker.worker.action;
import java.awt.Color;
import java.awt.Font;
import java.awt.RenderingHints;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
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.axis.ValueAxis;
import org.jfree.chart.entity.StandardEntityCollection;
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.servlet.ChartDeleter;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.general.DefaultPieDataset;
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.createPieChart(title,
dataset, // data
true, // include legend
true, false);
// 图片背景色
chart.setBackgroundPaint(Color.white);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setNoDataMessage("暂时无相关的数据。");
// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);
plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})", NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
// 图例显示百分比:自定义方式, {0} 表示选项, {1} 表示数值, {2} 表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
"{0}={1}({2})"));
// 指定图片的透明度(0.0-1.0)
//plot.setForegroundAlpha(0.65f);
// 指定显示的饼图上圆形(false)还椭圆形(true)
plot.setCircular(true, false);
// 设置第一个 饼块section 的开始位置,默认是12点钟方向
plot.setStartAngle(90);
// 图表图像写入到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
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;
}
/**
* 单柱
* @param dataset
* @param xName
* @param yName
* @param title
* @param width
* @param height
* @param session
* @param pw
* @return
*/
public static String generatePoleChart(CategoryDataset dataset, String xName,
String yName,String title, int width, int height, HttpSession session,
PrintWriter pw) {
String filename="";
try{
JFreeChart chart = ChartFactory.createBarChart(title, // 图表标题
xName, // 目录轴的显示标签
yName, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
/*
* 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);
// create plot
CategoryPlot plot = chart.getCategoryPlot();
// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);
plot.setNoDataMessage("暂时无相关的数据。");
// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);
// 数据轴精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0");
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(1f);// 横轴上的 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.05);
// 设置柱子高度
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);
// 图表图像写入到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
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;
}
/**
* 单柱 水平
*/
public static String generatePoleChartForHor(CategoryDataset dataset, String xName,
String yName,String title, int width, int height, HttpSession session,
PrintWriter pw) {
String filename="";
try{
JFreeChart chart = ChartFactory.createBarChart(title, // 图表标题
xName, // 目录轴的显示标签
yName, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.HORIZONTAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
true, // 是否生成工具
false // 是否生成URL链接
);
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
/*
* 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);
// create plot
CategoryPlot plot = chart.getCategoryPlot();
// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);
plot.setNoDataMessage("暂时无相关的数据。");
// 设置无数据时的信息显示颜色
plot.setNoDataMessagePaint(Color.red);
// 数据轴精度
NumberAxis vn = (NumberAxis) plot.getRangeAxis();
// vn.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0");
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.0f);// 横轴上的 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);
rangeAxis.setLabelAngle(1.6);//标签字体的方向
plot.setRangeAxis(rangeAxis);
BarRenderer renderer = new BarRenderer();
// 设置柱子宽度
renderer.setMaximumBarWidth(0.05);
// 设置柱子高度
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);
// 图表图像写入到临时目录
ChartRenderingInfo info = new ChartRenderingInfo(
new StandardEntityCollection());
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;
}
// 柱状图 数据集
public static CategoryDataset getBarData(double[][] data, String[] rowKeys,
String[] columnKeys) {
return DatasetUtilities
.createCategoryDataset(rowKeys, columnKeys, data);
}
}
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
DefaultPieDataset piedataset=(DefaultPieDataset)request.getAttribute("piedataset");
String p = ChartUtil.generatePieChart(piedataset," 职位分布情况统计",400,400,null, new PrintWriter(out));
String p1 = request.getContextPath() + "/servlet/DisplayChart?filename=" + p;
%>
<img src="<%= p1 %>" width=400 height=400 border=0 usemap="#<%= p %>"/>
分享到:
相关推荐
这个小案例包括了基本的JFreeChar的报表制作,其中有饼图,折线图,然后还包括了连接数据库sqlserver获取数据的方式
JFreeChar生成饼图图片详细JAVA代码,直接导入eclipse即可使用。
jfreechar 实现生成柱状图,饼图,曲线,没有调用正在的数据库数据,用的到的可以看看。
JFreeChar图表生成柱状图和饼状图实例源代码。
struts2上集成jfeechar生成统计图表,配置很详细,大家可以下载看看
用户java web开发的时候的用于图标的生成,特别是对有数据统计,统计图生成的项目
jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图 直接导入eclipse 或myeclipse 即可运行,里面含有lib ,及运行详细说 如:<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts ...
生成饼状图 生成单组柱状图 生成单组柱状图 生成堆积柱状图 生成折线图 柱状图
JFreeChar 动态横向柱状图,带纵向滚动条,可根据数据自动计算滚动条长度,可自定义系统柱子颜色,定时刷新
Jfreechar所需的jar包,JFreeChart是JAVA平台上的一个开放的图表绘制类库,JFreeChart可生成饼图,柱状图,散点图等等。。
JfreeChar测试程序测试程序,生成图表,并且可以导出图片,学习参考
java jfreechar 饼图 柱图 折线 本文中有详细的实例供参考 可以说当前所需要的统计图形都能生成
图形应用包,java生成图象
jfreechart文件 是一个java应用程序 直接运行就ok jfreechar-Demo 是web程序 部署运行 访问action jfreechar是个很好的 生成分布图的工具
客户端报表:用来完成绘图,兼容性存在问题(Jqueyr中的插件交互性根好),服务器报表Jfreechart将数据生成一张图片然后发送给客户端,缺点:交互性差,影响服务器的性能,服务器绘制图消耗cpu资源。 使用:报表步骤:...
cewolf-1.0-bundle-2 第三方生成图形 好东东
NULL 博文链接:https://lixc.iteye.com/blog/278929
关于图表的jar包,例如:饼图,直方图。都可以用这个jar包进行生成。
JFreeChart是一组功能强大、灵活易用的Java绘图API,使用它可以生成多种通用性的报表,包括柱状图、饼图、曲线图、甘特图等。它能够用在Swing和Web等中制作自定义的图表或报表,并且得到广泛的应用。本文将通过引领...
很好用的图形报表工具,有详细的帮助文档及实例,能生成曲线图、饼状图、柱状图、时序图,杜绝(鄙视)劣质代码,只提供优质代码!