package jfreechart;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
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.labels.StandardPieToolTipGenerator;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
import org.jfree.chart.labels.StandardXYToolTipGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.IntervalMarker;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.category.BarRenderer;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.Layer;
import org.jfree.ui.TextAnchor;
import org.jfree.util.Rotation;
/**
*
* 文 件 名:JFreeChartServlet.java
* 包 名:com.report.servlet
* 创 建 人:
* 创建日期: Mar 30, 2010 1:00:23 PM
* 描 述:生成jFreeChart
*/
public class JFreeChartCreater {
/**
*
* 方法名称:createPieChart
* 功能描述:创建PieChart(饼图)图表
* 返 回 值:JFreeChart
* 创 建 人:
* 创建日期:Mar 30, 2010 12:59:07 PM
*
* @param dataset
*/
public static JFreeChart createPieChart(DefaultPieDataset dataset, String title, boolean is3D) {
JFreeChart chart = null;
if (is3D) {
chart = ChartFactory.createPieChart3D(title, // 图表标题
dataset, // 数据集
true, // 是否显示图例
true, // 是否显示工具提示
true // 是否生成URL
);
} else {
chart = ChartFactory.createPieChart(title, // 图表标题
dataset, // 数据集
true, // 是否显示图例
true, // 是否显示工具提示
true // 是否生成URL
);
}
// 设置标题字体,为了防止中文乱码:必须设置字体
chart.setTitle(new TextTitle(title, new Font("黑体", Font.ITALIC, 22)));
// 设置图例的字体,为了防止中文乱码:必须设置字体
chart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 12));
// 获取饼图的Plot对象(实际图表)
PiePlot plot = (PiePlot) chart.getPlot();
// 图形边框颜色
plot.setBaseSectionOutlinePaint(Color.GRAY);
// 图形边框粗细
plot.setBaseSectionOutlineStroke(new BasicStroke(0.0f));
// 设置饼状图的绘制方向,可以按顺时针方向绘制,也可以按逆时针方向绘制
plot.setDirection(Rotation.ANTICLOCKWISE);
// 设置绘制角度(图形旋转角度)
plot.setStartAngle(70);
// 设置突出显示的数据块
// plot.setExplodePercent("One", 0.1D);
// 设置背景色透明度
plot.setBackgroundAlpha(0.7F);
// 设置前景色透明度
plot.setForegroundAlpha(0.65F);
// 设置区块标签的字体==为了防止中文乱码:必须设置字体
plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
// 扇区分离显示,对3D图不起效
if (is3D)
plot.setExplodePercent(dataset.getKey(3), 0.1D);
// 图例显示百分比:自定义方式,{0} 表示选项, {1} 表示数值, {2} 表示所占比例 ,小数点后两位
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}\r\n({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));
// 指定显示的饼图为:圆形(true) 还是椭圆形(false)
plot.setCircular(true);
// 没有数据的时候显示的内容
plot.setNoDataMessage("找不到可用数据");
// 设置鼠标悬停提示
plot.setToolTipGenerator(new StandardPieToolTipGenerator());
// 设置热点链接
// plot.setURLGenerator(new StandardPieURLGenerator("detail.jsp"));
return chart;
}
/**
*
* 方法名称:createPieChart
* 功能描述:创建BarChart(柱状图/条形图)图表
* 返 回 值:JFreeChart
* 创 建 人:
* 创建日期:Mar 30, 2010 12:59:07 PM
*
* @param dataset
*/
@SuppressWarnings("deprecation")
public static JFreeChart createBarChart(CategoryDataset dataset, String title, String x, String y, boolean is3D) {
JFreeChart chart = null;
BarRenderer renderer = null;
if (is3D) {
chart = ChartFactory.createBarChart3D( // 3D柱状图
// JFreeChart chart = ChartFactory.createLineChart3D(
// //3D折线图
title, // 图表的标题
x, // 目录轴的显示标签
y, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
true, // 是否显示图例
false, // 是否显示工具提示
false // 是否生成URL
);
// 柱图的呈现器
renderer = new BarRenderer3D();
renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setItemLabelFont(new Font("黑体", Font.PLAIN, 12));
renderer.setItemLabelsVisible(true);
// 3D柱子上不能正常显示数字
// 注意:如果数值太大切前面的柱子低于后面的柱子,那么前面的那个数值将被挡住,所以将下面方法中的0改为负值
ItemLabelPosition itemLabelPositionFallback = new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT, TextAnchor.HALF_ASCENT_LEFT, -1.3D);
// 设置不能正常显示的柱子label的position
renderer.setPositiveItemLabelPositionFallback(itemLabelPositionFallback);
renderer.setNegativeItemLabelPositionFallback(itemLabelPositionFallback);
} else {
chart = ChartFactory.createBarChart( // 柱状图
// JFreeChart chart = ChartFactory.createLineChart3D(
// //3D折线图
title, // 图表的标题
x, // 目录轴的显示标签
y, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方式:V垂直;H水平
true, // 是否显示图例
false, // 是否显示工具提示
false // 是否生成URL
);
// 柱图的呈现器
renderer = new BarRenderer();
renderer.setIncludeBaseInRange(true); // 显示每个柱的数值,并修改该数值的字体属性
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
}
// 设置图片背景
// chart.setBackgroundPaint(Color.PINK);
// 为了防止中文乱码:必须设置字体
chart.setTitle(new TextTitle(title, new Font("黑体", Font.PLAIN, 22)));
LegendTitle legend = chart.getLegend(); // 获取图例
legend.setItemFont(new Font("宋体", Font.BOLD, 12)); // 设置图例的字体,防止中文乱码
CategoryPlot plot = (CategoryPlot) chart.getPlot(); // 获取柱图的Plot对象(实际图表)
// 设置柱图背景色(注意,系统取色的时候要使用16位的模式来查看颜色编码,这样比较准确)
plot.setBackgroundPaint(new Color(255, 255, 204));
plot.setForegroundAlpha(0.65F); // 设置前景色透明度
// 设置横虚线可见
plot.setRangeGridlinesVisible(true);
// 虚线色彩
plot.setRangeGridlinePaint(Color.gray);
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个Item与图片顶端的距离
rangeAxis.setUpperMargin(0.2);
// 设置最低的一个Item与图片底端的距离
rangeAxis.setLowerMargin(0.3);
CategoryAxis domainAxis = plot.getDomainAxis(); // 获取x轴
domainAxis.setMaximumCategoryLabelWidthRatio(1.0f);// 横轴上的 Lable 是否完整显示
domainAxis.setLabelFont(new Font("宋体", Font.TRUETYPE_FONT, 14));// 设置字体,防止中文乱码
domainAxis.setTickLabelFont(new Font("宋体", Font.BOLD, 12));// 轴数值
// h.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//45度倾斜
plot.getRangeAxis().setLabelFont(new Font("宋体", Font.TRUETYPE_FONT, 14)); // Y轴设置字体,防止中文乱码
renderer.setBaseOutlinePaint(Color.BLACK); // 设置柱子边框颜色
renderer.setDrawBarOutline(true); // 设置柱子边框可见
renderer.setSeriesPaint(0, Color.YELLOW); // 设置每个柱的颜色
renderer.setSeriesPaint(1, Color.green);
renderer.setSeriesPaint(2, Color.RED);
renderer.setSeriesPaint(3, Color.CYAN);
renderer.setSeriesPaint(5, Color.ORANGE);
renderer.setSeriesPaint(4, Color.MAGENTA);
renderer.setSeriesPaint(6, Color.DARK_GRAY);
renderer.setSeriesPaint(7, Color.PINK);
renderer.setSeriesPaint(8, Color.black);
renderer.setItemMargin(0.1); // 设置每个地区所包含的平行柱的之间距离
plot.setRenderer(renderer); // 给柱图添加呈现器
plot.setForegroundAlpha(0.7f); // 设置柱的透明度
// renderer.setMaximumBarWidth(0.2); // 设置柱子宽度
// renderer.setMinimumBarLength(0.6); // 设置柱子高度
// 设置横坐标显示位置(默认是下方);
// plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// 设置纵坐标显示位置(默认是左方)
// plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
// 没有数据的时候显示的内容
plot.setNoDataMessage("找不到可用数据");
return chart;
}
/**
*
* 方法名称:JFreeChartSeriesChart
* 功能描述:创建曲线图(折线图)
* 注意事项:一般曲线图不用加域所以后4个参数一般为(false,null,0,0)
* 参 数:title-标题;subtitleStr-子标题;domain-x轴标志;range-y轴标志;dataset-设置数据;
* isAreaText-是否在图标中加域;
* areaText-域中文字,lowpress-域的最低刻度;uperpress-域的最高刻度
* 返 回 值:JFreeChart
* 创 建 人:
* 创建日期:Mar 30, 2010 1:11:24 PM
*/
@SuppressWarnings("deprecation")
public static JFreeChart JFreeChartSeriesChart(String title, String subtitleStr, String domain, String range, TimeSeriesCollection dataset, boolean isAreaText, String areaText, double lowpress, double uperpress) { // 时间曲线元素
// JFreeChart chart =
// ChartFactory.createTimeSeriesChart("标题","x轴标志","y轴标志","设置数据",是否显示图形,是否进行提示,是否配置报表存放地址);
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, domain, range, dataset, true, true, false);
if (subtitleStr != null) {
TextTitle subtitle = new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
}
// 设置日期显示格式
XYPlot plot = chart.getXYPlot();
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
// 设置标题的颜色
chart.setTitle(new TextTitle(title, new Font("黑体", Font.ITALIC, 22)));
chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
plot.setOutlineStroke(new BasicStroke(1.5f)); // 边框粗细
ValueAxis vaxis = plot.getDomainAxis();
vaxis.setAxisLineStroke(new BasicStroke(1.5f)); // 坐标轴粗细
vaxis.setAxisLinePaint(new Color(215, 215, 215)); // 坐标轴颜色
vaxis.setLabelPaint(new Color(10, 10, 10)); // 坐标轴标题颜色
vaxis.setTickLabelPaint(new Color(102, 102, 102)); // 坐标轴标尺值颜色
vaxis.setLowerMargin(0.06d);// 分类轴下(左)边距
vaxis.setUpperMargin(0.14d);// 分类轴下(右)边距,防止最后边的一个数据靠近了坐标轴。
plot.setNoDataMessage("找不到可用数据");// 没有数据时显示的文字说明。
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) plot.getRenderer();
// 第一条折线的颜色
xylineandshaperenderer.setBaseItemLabelsVisible(true);
xylineandshaperenderer.setSeriesFillPaint(0, new Color(127, 128, 0));
xylineandshaperenderer.setSeriesPaint(0, new Color(127, 128, 0));
xylineandshaperenderer.setSeriesShapesVisible(0, true);
xylineandshaperenderer.setSeriesShapesVisible(1, true);
xylineandshaperenderer.setSeriesShapesVisible(2, true);
xylineandshaperenderer.setSeriesShapesVisible(3, true);
xylineandshaperenderer.setSeriesShapesVisible(4, true);
// 折线的粗细调
StandardXYToolTipGenerator xytool = new StandardXYToolTipGenerator();
xylineandshaperenderer.setToolTipGenerator(xytool);
xylineandshaperenderer.setStroke(new BasicStroke(1.5f));
// 显示节点的值
xylineandshaperenderer.setBaseItemLabelsVisible(true);
xylineandshaperenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
xylineandshaperenderer.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
xylineandshaperenderer.setBaseItemLabelPaint(new Color(102, 102, 102));// 显示折点数值字体的颜色
ValueAxis rangeAxis = plot.getRangeAxis();
// 设置最高的一个Item与图片顶端的距离
rangeAxis.setUpperMargin(0.2);
// 设置最低的一个Item与图片底端的距离
rangeAxis.setLowerMargin(0.3);
// 在图表中加区域加区域
if (isAreaText) {
lowpress = 62;
uperpress = 400;
IntervalMarker intermarker = new IntervalMarker(lowpress, uperpress);
intermarker.setPaint(Color.decode("#66FFCC"));// 域顏色
intermarker.setLabelFont(new Font("SansSerif", 41, 14));
intermarker.setLabelPaint(Color.RED);
intermarker.setLabel(areaText);
if (dataset != null) {
plot.addRangeMarker(intermarker, Layer.BACKGROUND);
}
}
return chart;
}
}
jfreechart测试代码
最新推荐文章于 2021-01-14 16:17:27 发布