jfreechart生成折线图 | 饼图 | 柱状图 | 堆栈柱状图 (六)

2014-11-24 10:11:45 · 作者: · 浏览: 11
ndPaint(Color.WHITE);
// 设置图标题的字体重新设置title
Font font = new Font("隶书", Font.BOLD, 25);
TextTitle title = new TextTitle(chartTitle);
title.setFont(font);
chart.setTitle(title);
// 设置面板字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

chart.setBackgroundPaint(Color.WHITE);

CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();
// x轴 // 分类轴网格是否可见
categoryplot.setDomainGridlinesVisible(true);
// y轴 //数据轴网格是否可见
categoryplot.setRangeGridlinesVisible(true);

categoryplot.setRangeGridlinePaint(Color.WHITE);// 虚线色彩

categoryplot.setDomainGridlinePaint(Color.WHITE);// 虚线色彩

categoryplot.setBackgroundPaint(Color.lightGray);

// 设置轴和面板之间的距离
// categoryplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));

CategoryAxis domainAxis = categoryplot.getDomainAxis();

domainAxis.setLabelFont(labelFont);// 轴标题
domainAxis.setTickLabelFont(labelFont);// 轴数值

domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45); // 横轴上的
// Lable
// 45度倾斜
// 设置距离图片左端距离
domainAxis.setLowerMargin(0.0);
// 设置距离图片右端距离
domainAxis.setUpperMargin(0.0);

NumberAxis numberaxis = (NumberAxis) categoryplot.getRangeAxis();
numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
numberaxis.setAutoRangeIncludesZero(true);

// 获得renderer 注意这里是下嗍造型到lineandshaperenderer!!
LineAndShapeRenderer lineandshaperenderer = (LineAndShapeRenderer) categoryplot
.getRenderer();

lineandshaperenderer.setBaseShapesVisible(true); // series 点(即数据点)可见
lineandshaperenderer.setBaseLinesVisible(true); // series 点(即数据点)间有连线可见

// 显示折点数据
// lineandshaperenderer.setBaseItemLabelGenerator(new
// StandardCategoryItemLabelGenerator());
// lineandshaperenderer.setBaseItemLabelsVisible(true);

FileOutputStream fos_jpg = null;
try {
isChartPathExist(CHART_PATH);
String chartName = CHART_PATH + charName;
fos_jpg = new FileOutputStream(chartName);

// 将报表保存为png文件
ChartUtilities.writeChartAsPNG(fos_jpg, chart, 500, 510);

return chartName;
} catch (Exception e) {
e.printStackTrace();
return null;
} finally {
try {
fos_jpg.close();
System.out.println("create time-createTimeXYChar.");
} catch (Exception e) {
e.printStackTrace();
}
}
}

/**
* 堆栈柱状图
*
* @param dataset
* @param xName
* @param yName
* @param chartTitle
* @param charName
* @return
*/
public String createStackedBarChart(CategoryDataset dataset, String xName,
String yName, String chartTitle, String charName) {
// 1:得到 CategoryDataset

// 2:JFreeChart对象
JFreeChart chart = ChartFactory.createStackedBarChart(chartTitle, // 图表标题
xName, // 目录轴的显示标签
yName, // 数值轴的显示标签
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示图例(对于简单的柱状图必须是false)
false, // 是否生成工具
false // 是否生成URL链接
);
// 图例字体清晰
chart.setTextAntiAlias(false);

chart.setBackgroundPaint(Color.WHITE);

// 2 .2 主标题对象 主标题对象是 TextTitle 类型
chart
.setTitle(new TextTitle(chartTitle, new Font("隶书", Font.BOLD,
25)));
// 2 .2.1:设置中文
// x,y轴坐标字体
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);

// 2 .3 Plot 对象 Plot 对象是图形的绘制结构对象
CategoryPlot plot = chart.getCategoryPlot();

// 设置横虚线可见
plot.setRan