文章是从几个博客内容综合起来的,记录一下。用的时候需要修改哪些属性,可以直接拿来测试!
/**
* 类库
* 1、jfreechart-1.0.5.jar、jcommon-1.0.9.jar、gnujaxp.jar
* 2、jbcl.jar、dx.jar、beandt.jar 由JBuilder2006的lib下提供
*/
package jfreechart;
import java.io.*;
import java.text.DecimalFormat;
import org.jfree.data.*;
import org.jfree.chart.*;
import org.jfree.chart.plot.*;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.*;
import org.jfree.ui.TextAnchor;
import java.awt.Color;
import java.awt.RenderingHints;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import java.awt.Font;
import org.jfree.chart.axis.AxisLocation;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.Toolkit;
import java.awt.Dimension;
/**
*
* <p>
* Title: Java开发手册
* </p>
*
*
*
* <p>
* Description: 适合中高级使用者
* </p>
*
*
*
* <p>
* Copyright: Copyright (c) 2007
* </p>
*
*
*
* <p>
* Company: lingbrother
* </p>
*
*
*
* @author lingbrother
*
* @version 1.0
*/
public class BarChartDemo {
public BarChartDemo() {
super();
}
public static void main(String[] args) throws IOException {
DefaultCategoryDataset dataset = new DefaultCategoryDataset();
// 添加数据
dataset.addValue(440, "数据", "类型1");
/*dataset.addValue(360, "数据", "类型2");
dataset.addValue(510, "数据", "类型3");
dataset.addValue(390, "数据", "类型4");*/
/**
* 参数分别为:图表标题 ,目录轴的显示标签 ,数值轴的显示标签 ,数据集 ,是否生成URL链接 图表方向:水平、垂直,
* 是否显示图例(对于简单的柱状图必须是false) ,是否生成工具
*/
JFreeChart chart = ChartFactory.createBarChart3D("XXX统计图", "类型", "数据额",
dataset, PlotOrientation.VERTICAL, true, false, true);
/*----------设置消除字体的锯齿渲染(解决中文问题)--------------*/
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING,
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
// 底部汉字乱码的问题
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
// 设置标题字体
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
textTitle.setBackgroundPaint(Color.LIGHT_GRAY);// 标题背景色
textTitle.setPaint(Color.cyan);// 标题字体颜色
textTitle.setText("类型统计图");// 标题内容
CategoryPlot plot = chart.getCategoryPlot();// 设置图的高级属性
BarRenderer3D renderer = new BarRenderer3D();// 3D属性修改
CategoryAxis domainAxis = plot.getDomainAxis();// 对X轴做操作
ValueAxis rAxis = plot.getRangeAxis();// 对Y轴做操作
/***
* domainAxis设置(x轴一些设置)
**/
// 设置X轴坐标上的文字
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
// 设置X轴的标题文字
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
domainAxis.setLabel("");// X轴的标题内容
domainAxis.setTickLabelPaint(Color.red);// X轴的标题文字颜色
domainAxis.setTickLabelsVisible(true);// X轴的标题文字是否显示
domainAxis.setAxisLinePaint(Color.red);// X轴横线颜色
domainAxis.setTickMarksVisible(true);// 标记线是否显示
domainAxis.setTickMarkOutsideLength(3);// 标记线向外长度
domainAxis.setTickMarkInsideLength(3);// 标记线向内长度
domainAxis.setTickMarkPaint(Color.red);// 标记线颜色
domainAxis.setUpperMargin(0.2);// 设置距离图片左端距离
domainAxis.setLowerMargin(0.2); // 设置距离图片右端距离
// 横轴上的 Lable 是否完整显示
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f);
// 横轴上的 Lable 45度倾斜
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
/**
* rAxis设置 Y轴设置
*
**/
// 设置Y轴坐标上的文字
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
// 设置Y轴的标题文字
rAxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
// Y轴取值范围(下面不能出现 rAxis.setAutoRange(true) 否则不起作用)
rAxis.setRange(100, 600);
// rAxis.setLowerBound(100); //Y轴以开始的最小值
// rAxis.setUpperBound(600);//Y轴的最大值
rAxis.setLabel("content");// Y轴内容
//rAxis.setLabelAngle(1.6);// 标题内容显示角度(1.6时候水平)
//rAxis.setLabelPaint(Color.red);// 标题内容颜色
//rAxis.setMinorTickMarksVisible(true);// 标记线是否显示
//rAxis.setMinorTickCount(7);// 节段中的刻度数
//rAxis.setMinorTickMarkInsideLength(3);// 内刻度线向内长度
//rAxis.setMinorTickMarkOutsideLength(3);// 内刻度记线向外长度
//rAxis.setTickMarkInsideLength(3);// 外刻度线向内长度
//rAxis.setTickMarkPaint(Color.red);// 刻度线颜色
rAxis.setTickLabelsVisible(true);// 刻度数值是否显示
// 所有Y标记线是否显示(如果前面设置rAxis.setMinorTickMarksVisible(true); 则其照样显示)
rAxis.setTickMarksVisible(true);
rAxis.setAxisLinePaint(Color.red);// Y轴竖线颜色
rAxis.setAxisLineVisible(true);// Y轴竖线是否显示
// 设置最高的一个 Item 与图片顶端的距离 (在设置rAxis.setRange(100, 600);情况下不起作用)
rAxis.setUpperMargin(0.15);
// 设置最低的一个 Item 与图片底端的距离
rAxis.setLowerMargin(0.15);
rAxis.setAutoRange(true);// 是否自动适应范围
rAxis.setVisible(true);// Y轴内容是否显示
// 数据轴精度
NumberAxis na = (NumberAxis) plot.getRangeAxis();
na.setAutoRangeIncludesZero(true);
DecimalFormat df = new DecimalFormat("#0.000");
// 数据轴数据标签的显示格式
na.setNumberFormatOverride(df);
/**
* renderer设置 柱子相关属性设置
*/
renderer.setBaseOutlinePaint(Color.ORANGE); // 边框颜色
renderer.setDrawBarOutline(true);
renderer.setWallPaint(Color.gray);// 设置墙体颜色
renderer.setMaximumBarWidth(0.08); // 设置柱子宽度
renderer.setMinimumBarLength(0.1); // 设置柱子高度
renderer.setSeriesPaint(0, Color.ORANGE); // 设置柱的颜色
renderer.setItemMargin(0); // 设置每个地区所包含的平行柱的之间距离
// 在柱子上显示相应信息
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
renderer.setBaseItemLabelsVisible(true);
renderer.setBaseItemLabelPaint(Color.BLACK);// 设置数值颜色,默认黑色
// 搭配ItemLabelAnchor TextAnchor
// 这两项能达到不同的效果,但是ItemLabelAnchor最好选OUTSIDE,因为INSIDE显示不出来
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT));
// 下面可以进一步调整数值的位置,但是得根据ItemLabelAnchor选择情况.
renderer.setItemLabelAnchorOffset(10);
/**
* plot 设置
***/
// 设置网格竖线颜色
plot.setDomainGridlinePaint(Color.blue);
plot.setDomainGridlinesVisible(true);
// 设置网格横线颜色
plot.setRangeGridlinePaint(Color.blue);
plot.setRangeGridlinesVisible(true);
// 图片背景色
plot.setBackgroundPaint(Color.LIGHT_GRAY);
plot.setOutlineVisible(true);
// 图边框颜色
plot.setOutlinePaint(Color.magenta);
// 设置柱的透明度
plot.setForegroundAlpha(1.0f);
// 将类型放到上面
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT);
// 将默认放到左边的数值放到右边
//plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT);
plot.setRenderer(renderer);// 将修改后的属性值保存到图中
try {
File file = new File("d:/student.png");
ChartUtilities.saveChartAsPNG(file, chart, 400, 300);// 把报表保存为文件
// 将生成的报表放到预览窗口中
final ChartFrame preview = new ChartFrame("招生信息", chart);
preview.addWindowListener(new WindowAdapter() {
public void windowClosing(final WindowEvent event) {
preview.dispose();
}
});
preview.pack();
// 调整预览窗口的大小和位置,适合屏幕,并且居中
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
preview.setSize(screenSize.width, screenSize.height - 50);// 适合屏幕,50表示把工具栏要考虑在内
Dimension frameSize = preview.getSize();
if (frameSize.height > screenSize.height) {
frameSize.height = screenSize.height;
}
if (frameSize.width > screenSize.width) {
frameSize.width = screenSize.width;
}
preview.setLocation((screenSize.width - frameSize.width) / 2,
(screenSize.height - frameSize.height - 50) / 2);
// 显示报表预览窗口
preview.setVisible(true);
} catch (Exception e) {
String s = e.getLocalizedMessage();
s = e.getMessage();
s = e.toString();
}
}
}