饼状图
DefaultPieDataset dpd=new DefaultPieDataset();
dpd.setValue(" 程序员 ", 40);
dpd.setValue(" 美工 ", 13);
dpd.setValue(" 项目经理 ", 3);
dpd.setValue(" 架构师 ", 5);
JFreeChart chart=ChartFactory.createPieChart3D("公司组织架构", dpd, true,true,false);
chart.getTitle().setFont(new Font("宋体", Font.BOLD,12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
PiePlot plot=(PiePlot)chart.getPlot();
plot.setLabelFont(new Font("宋体", Font.PLAIN, 12));
ChartUtilities.saveChartAsJPEG(new File("f:/test.png"), chart,300,300);
柱状图
categoryAxis.setTickLabelFont(new Font("宋体",Font.PLAIN,12)); //设置坐标轴标尺值字体
categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//设置坐标轴标题旋转角度
renderer.setItemMargin(0.32); //设置柱子间的间距
plot.setRenderer(renderer); //设置图片渲染对象
设置主题样式
//创建主题样式
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");
//设置标题字体
standardChartTheme.setExtraLargeFont(new Font("隶书", Font.BOLD, 20));
//设置图例的字体
standardChartTheme.setRegularFont(new Font("宋体", Font.PLAIN, 15));
//设置轴向的字体
standardChartTheme.setLargeFont(new Font("宋体", Font.PLAIN, 15));
//应用主题样式
ChartFactory.setChartTheme(standardChartTheme);
public static void main(String[] args) throws Exception {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// dataset.addValue(value, rowKey, columnKey)
// value 表示填充柱状图所要的实际数据 rowKeys 表示 X 轴数据 columnKeys 表示 Y 轴数据
dataset.addValue(2000, " 计划 ", " 清华大学 ");// value, rowKey, columnKey
dataset.addValue(4000, " 实报 ", " 清华大学 ");
dataset.addValue(1000, " 计划 ", " 北京大学 ");
dataset.addValue(2050, " 实报 ", " 北京大学 ");
dataset.addValue(2000, " 计划 ", " 武汉大学 ");
dataset.addValue(2850, " 实报 ", " 武汉大学 ");
JFreeChart chart = ChartFactory.createBarChart3D("2010 三大高校招生信息总览 ", // 图表标题
" 应报与实报对照 ", // X 轴标题
" 人数 ", // Y 轴标题
dataset, // 数据集
PlotOrientation.VERTICAL, // 图表方向:水平、垂直
true, // 是否显示标题 ( 对于简单的柱状图必须是 false) ,最底下
false, // 是否生成工具
false// 是否生成 URL 链接
);
// 设置 Title 和 Legend 的字体
chart.getTitle().setFont((new Font(" 宋体 ", Font.CENTER_BASELINE, 20)));
chart.getLegend().setItemFont(new Font(" 宋体 ", Font.CENTER_BASELINE, 15));
CategoryPlot plot = chart.getCategoryPlot();// 获得图表区域对象
// 设置纵横坐标的显示位置
plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
// 学校显示在下端 ( 柱子竖直 ) 或左侧 ( 柱子水平 )
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);
// 人数显示在左侧 ( 柱子竖直 ) 或下端 ( 柱子水平 )
CategoryAxis domainAxis = plot.getDomainAxis();
setXDomainAxis(domainAxis);
// 设置图表的纵轴( Y 轴)
ValueAxis valueAxis = plot.getRangeAxis();
setYDomainAxis(valueAxis);
// 设置图表的颜色
setRender(plot);
File file = new File("F:/student.png");
ChartUtilities.saveChartAsPNG(file, chart, 400, 300);// 把报表保存为文件
}
/**
* 对 X 轴的设置
* @param categoryAxis
*/
public static void setXDomainAxis(CategoryAxis domainAxis) {
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
domainAxis.setLowerMargin(0.1);// 设置距离图片左端距离此时为 10%
domainAxis.setUpperMargin(0.1);// 设置距离图片右端距离此时为百分之 10
domainAxis.setLabelFont(labelFont);// X 轴标题字体
domainAxis.setTickLabelFont(labelFont);// X 轴数据字体
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);//数据字体旋转角度
domainAxis.setCategoryLabelPositionOffset(10);// 图表横轴与标签的距离 (10 像素 )
domainAxis.setCategoryMargin(0.2);// 横轴标签之间的距离 20%
// domainAxis.setMaximumCategoryLabelLines(1);
// domainAxis.setMaximumCategoryLabelWidthRatio(0);
}
/**
* 对 Y 轴的设置
* @param categoryAxis
*/
public static void setYDomainAxis(ValueAxis valueAxis) {
Font labelFont = new Font("SansSerif", Font.TRUETYPE_FONT, 12);
valueAxis.setUpperMargin(0.1);// 设置最高的一个柱与图片顶端的距离 ( 最高柱的 10%)
valueAxis.setLabelFont(labelFont);// Y 轴标题
valueAxis.setTickLabelFont(labelFont);// Y 轴数值
}
/**
* 设置图表的颜色 ( 渲染图表 )
* @param categoryPlot
*/
public static void setRender(CategoryPlot plot) {
BarRenderer3D renderer = new BarRenderer3D();
renderer.setBaseOutlinePaint(Color.red);
renderer.setSeriesPaint(0, new Color(0, 255, 255));// 计划柱子的颜色为青色
renderer.setSeriesOutlinePaint(0, Color.BLACK);// 边框为黑色
renderer.setSeriesPaint(1, new Color(0, 255, 0));// 实报柱子的颜色为绿色
renderer.setSeriesOutlinePaint(1, Color.red);// 边框为红色
renderer.setItemMargin(0.1);// 组内柱子间隔为组宽的 10%
// 显示每个柱的数值,并修改该数值的字体属性
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelFont((new Font(" 黑体 ", Font.BOLD, 12)));
renderer.setBaseItemLabelPaint(Color.black);// 字体为黑色
renderer.setBaseItemLabelsVisible(true);
// 默认的数字显示在柱子中,通过如下两句可调整数字的显示
// 注意:此句很关键,若无此句,那数字的显示会被覆盖,给人数字没有显示出来的问题
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE1, TextAnchor.CENTER_LEFT));
renderer.setItemLabelAnchorOffset(10D);// 位置调整
renderer.setBaseItemLabelFont(new Font(" 黑体 ", Font.BOLD, 12));
renderer.setBaseItemLabelsVisible(true);
plot.setRenderer(renderer);
plot.setRenderer(renderer);// 使用我们设计的效果
}
jfreechart实例
最新推荐文章于 2021-03-13 12:32:32 发布