/**
* 封装JFreeChart的一些方法
*
* @author 黄磊
*
*/
public class MyChartFactory {
/*
* 报表字体
*/
private static final Font CHART_FONT = new Font("黑体", 12, 12);
/**
* 获得一个柱状图报表
*
* @param title
* 标题
* @param xSubject
* X轴标题
* @param ySubject
* Y轴标题
* @param dataset
* 数据源
* @return 报表
*/
public static JFreeChart getBarChart(String title, String xSubject,
String ySubject, CategoryDataset dataset) {
JFreeChart chart = ChartFactory.createBarChart(title, xSubject,
ySubject, dataset, PlotOrientation.VERTICAL, true, true, false);
chart.setBackgroundPaint(new Color(230,239,248));
CategoryPlot plot = (CategoryPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
//plot.setBackgroundPaint(new Color(230,239,248));
plot.setDomainGridlinePaint(Color.white);
plot.setDomainGridlinesVisible(true);
plot.setRangeGridlinePaint(Color.white);
final NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
BarRenderer renderer = (BarRenderer) plot.getRenderer();
renderer.setDrawBarOutline(false);
CategoryAxis domainAxis = plot.getDomainAxis();
domainAxis.setCategoryLabelPositions(CategoryLabelPositions
.createUpRotationLabelPositions(Math.PI / 6.0));
plot.setNoDataMessage("没有数据显示");
// 设置字体,解决中文乱码问题
chart.getTitle().setFont(CHART_FONT);
chart.getLegend().setItemFont(CHART_FONT);
rangeAxis.setLabelFont(CHART_FONT);
domainAxis.setLabelFont(CHART_FONT);
return chart;
}
/**
* 获得一个时间轴报表图
*
* @param title
* 标题
* @param xSubject
* X轴标题
* @param ySubject
* Y轴标题
* @param dataset
* 数据源
* @return 报表
*/
public static JFreeChart getTimeSeriesChart(String title, String xSubject,
String ySubject, XYDataset dataset) {
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, xSubject,
ySubject, dataset, true, true, false);
chart.setBackgroundPaint(new Color(230,239,248));
XYPlot plot = (XYPlot) chart.getPlot();
plot.setBackgroundPaint(Color.lightGray);
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
renderer.setDrawSeriesLineAsPath(true);
}
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
plot.setNoDataMessage("没有数据显示");
// 设置字体,解决中文乱码问题
chart.getTitle().setFont(CHART_FONT);
chart.getLegend().setItemFont(CHART_FONT);
plot.getRangeAxis().setLabelFont(CHART_FONT);
axis.setLabelFont(CHART_FONT);
return chart;
}
/**
* 获得一个饼状图报表
*
* @param title
* 标题
* @param dataset
* 数据源
* @return 报表
*/
public static JFreeChart getPieChart(String title, PieDataset dataset) {
JFreeChart chart = ChartFactory.createPieChart(title, dataset, true,
true, false);
PiePlot plot = (PiePlot) chart.getPlot();
plot.setSectionOutlinesVisible(false);
plot.setNoDataMessage("没有数据显示");
// 设置字体,解决中文乱码问题
chart.getTitle().setFont(CHART_FONT);
chart.getLegend().setItemFont(CHART_FONT);
plot.setLabelFont(CHART_FONT);
return chart;
}
}