版本JFreeChart 1.0.12
代码如下:
JFreeChart chart = ChartFactory.createBarChart(title, // char title
catAxisLabel, // categoryAxisLabel
valAxisLabel, // valueAxisLabel
dataset, // the dataset for the chart
PlotOrientation.VERTICAL, //
false, // legend if is needed
false, // tooltips if is needed
false // urls
);
//
customizeBarChart(chart);
//
return chart;
}
public JFreeChart createPieChart(PieDataset dataset, String title) {
// if no dataset, don't generate chart
if(dataset == null) return null;
JFreeChart chart = ChartFactory.createPieChart(title, // chart title
dataset, // data
true, // include legend
true, // include tooltip
false);
// customize the chart
customizePieChart(chart);
// customize for pie
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(true);
plot.setNoDataMessage("No data available");
return chart;
}
public JFreeChart create3DBarChart(CategoryDataset dataset, String title,
String catAxisLabel, String valAxisLabel) {
// if no dataset, don't generate chart
if(dataset == null) return null;
JFreeChart chart = ChartFactory.createBarChart3D(title, // 图表标题
catAxisLabel, // 目录轴的显示标签
valAxisLabel, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
false, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
// basic customization
customizeBarChart(chart);
// additional customization
return chart;
}
public JFreeChart create3DPieChart(PieDataset dataset, String title) {
// if no dataset, don't generate chart
if(dataset == null) return null;
JFreeChart chart = ChartFactory.createPieChart3D(title, // chart title
dataset, // data
true, // include legend
true, // incluse tooltip
false);
// customize the font of the chart
customizePieChart(chart);
// additional customize for 3D effect
PiePlot3D plot = (PiePlot3D) chart.getPlot();
plot.setStartAngle(290);
plot.setDirection(Rotation.CLOCKWISE);
plot.setForegroundAlpha(0.5f);
return chart;
}
/**
* Customize the pie chart, such as adjust font, legend.
* @param chart
*/
private void customizePieChart(JFreeChart chart) {
// 定义整张图片的背景颜色: GradientPaint渐变色
Paint bgPaint = new GradientPaint(50, 0, new Color(0xD1DEEF), 50, 700, new Color(0xFFFFFF));
chart.setBackgroundPaint(bgPaint); // set chart bg
chart.setBorderPaint(Color.GREEN);
chart.setBorderVisible(false); // set chart border, true in debug mode
PiePlot plot = (PiePlot) chart.getPlot(); // 获取饼图图表区域
plot.setBackgroundPaint(bgPaint); // 设置图表区域背景颜色
plot.setBackgroundAlpha(0.0f); // 设置此区域背景色透明度
plot.setOutlineVisible(false); // 设置是否显示边框,true in debug mode
plot.setNoDataMessage("无对应的数据,请重新查询。");
// 设置中文字体
Font titleFont = new Font("宋体", Font.PLAIN, 14); // 图表标题字体
Font contentFont = new Font("宋体", Font.PLAIN, 12); // 图表内容字体
Font legendFont = new Font("宋体", Font.PLAIN, 12); // 图例字体
// 设置图表标题属性
TextTitle title = chart.getTitle();
title.setFont(titleFont);
title.setMargin(10, 0, 0, 0);
// 设置图表内容字体
plot.setLabelFont(contentFont);
/*
* 是标签格式(默认是“{0} = {1}”这样是显示数据),
* {0}是类别名,{1}是实际数据,后面的{2}是百分比。
*/
String unitSytle = "{0}: {1}({2})"; // {2}
// 设置图表图例字体和位置
LegendTitle legend = chart.getLegend();
if (legend != null) {
legend.setItemFont(legendFont);
legend.setPosition(RectangleEdge.RIGHT);
legend.setVerticalAlignment(VerticalAlignment.TOP);
legend.setBackgroundPaint(bgPaint);
legend.setBorder(0.0, 0, 0.0, 0); // (0.5, 0, 0.5, 0) in test mode
// 为图例添加描述信息
LabelBlock labelblock = new LabelBlock("图例:", legendFont);
BlockContainer blockcontainer = new BlockContainer(
new BorderArrangement());
blockcontainer.add(labelblock, RectangleEdge.TOP);
// 添加图例本身
BlockContainer itemContainer = legend.getItemContainer();
blockcontainer.add(itemContainer);
legend.setWrapper(blockcontainer);
//
}
//引出标签显示样式,在图表上显示比例
plot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})"/*unitSytle*/,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
//图例显示样式
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(unitSytle,
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
}
/**
* Customize bar chart for font, background
* @param chart
*/
private void customizeBarChart(JFreeChart chart) {
// 定义整张图片的背景颜色: GradientPaint渐变色
Paint bgPaint = new GradientPaint(50, 0, new Color(0xD1DEEF), 50, 700, new Color(0xFFFFFF));
chart.setBackgroundPaint(bgPaint); // set chart bg
chart.setBorderPaint(Color.GREEN);
chart.setBorderVisible(false); // set chart border, true in debug mode
// 设置图表区域
CategoryPlot cplot = chart.getCategoryPlot(); // 获取饼图图表区域
cplot.setBackgroundAlpha(0.0f); // 设置此区域背景色透明度
cplot.setOutlineVisible(false); // 设置是否显示边框,true in debug mode
// solve Chinese character messy code of chart title.
Font titleFont = new Font("宋体", Font.PLAIN, 14);
Font xyTitleFont = new Font("宋体", Font.PLAIN, 12);
Font xyCoordFont = new Font("宋体", Font.PLAIN, 12);
Font legendFont = new Font("宋体", Font.PLAIN, 12);
Font contentFont = new Font("宋体", Font.PLAIN, 12);
// 设置图表标题属性
TextTitle title = chart.getTitle();
title.setFont(titleFont);
title.setMargin(15, 0, 10, 0);
//
CategoryAxis axis = cplot.getDomainAxis();
// Set font on X-axis title 设置X坐标轴上标题字体
axis.setLabelFont(xyTitleFont);
// Set font on X-axis coordinate, 设置X坐标轴上刻度字体
axis.setTickLabelFont(xyCoordFont);
//
ValueAxis vAxis = cplot.getRangeAxis();
// set the Y-axis title font
vAxis.setLabelFont(xyTitleFont);
// set the Y-axis coordinate title font
vAxis.setTickLabelFont(xyCoordFont);
vAxis.setUpperMargin(0.1); // 设置最高柱子离顶端的间距
// 设置Y轴坐标间距
NumberAxis numAxis = (NumberAxis)cplot.getRangeAxis();
numAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
// set legend font 如果有图例,设置其字体和位置
LegendTitle legend = chart.getLegend();
if (legend != null) {
legend.setItemFont(legendFont);
legend.setPosition(RectangleEdge.RIGHT);
}
// 设置图标属性
// change the color of the chart
cplot.setBackgroundPaint(new Color(212, 225, 241));
cplot.setDomainGridlinePaint(Color.GRAY); // 设置表中竖参考线的颜色
cplot.setDomainGridlinesVisible(true); // 设置竖参考线是否显示
// 设置横参考线属性
cplot.setRangeGridlinePaint(Color.BLUE);
cplot.setRangeGridlinesVisible(true);
cplot.setForegroundAlpha(1.0f); // 设置前景透明度(1.0f不透明)
BarRenderer crender = (BarRenderer) cplot.getRenderer(); //new BarRenderer();
// 设置柱子的形状属性
crender.setSeriesPaint(0, Color.BLUE); // 柱子颜色: 绿色new Color(0x3882EF),new Color(0x7170F0)
crender.setMaximumBarWidth(0.15); // 设置柱子宽度
// 设置显示柱子显示数据
crender.setSeriesItemLabelGenerator(0, new StandardCategoryItemLabelGenerator());
crender.setSeriesItemLabelFont(0, contentFont);
crender.setSeriesItemLabelsVisible(0, true);
}
/**
* 产生柱子的数据显示
* @author wuyao
*
*/
static class LabelGenerator extends StandardCategoryItemLabelGenerator {
/**
*
*/
private static final long serialVersionUID = 7335548557126787550L;
public String generateItemLabel(CategoryDataset categorydataset, int i,
int j) {
return categorydataset.getRowKey(i).toString();
}
LabelGenerator() {
}
}
/**
* Just for testing
* @param chart
* @param out
*/
public void writeChart(JFreeChart chart, OutputStream out) {
try {
ChartUtilities.writeChartAsJPEG(out, 1.0f, chart, 800, 600, null);
} catch (IOException e) {
// just throw it, or throw to high level Exception
throw new RuntimeException(e);
} finally {
try {
out.close();
} catch (IOException e) {
// just throw it, or throw to high level Exception
throw new RuntimeException(e);
}
}
}
}
一个简单的测试: