jFreeChart也许对大家来说很陌生,她是一款免费的、功能强大的统计图生成工具,可以直接生成PNG,JPG等各式的文件
为我们java项目的开发增加了一部利器。她的英文简介
JFreeChart is a free Java class library for generating charts, including:
pie charts;
line charts and horizontal/vertical bar charts (regular and stacked, with optional 3D-effect);
XY plots and scatter plots;
time series, high/low/open/close charts and candle stick charts;
combination charts;
Pareto charts;
Gantt charts;
bubble charts;
wind plots, meter charts and symbol charts;
jFreeChart也许对大家来说很陌生,她是一款免费的、功能强大的统计图生成工具,可以直接生成PNG,JPG等各式的文件
为我们java项目的开发增加了一部利器。她的英文简介
JFreeChart is a free Java class library for generating charts, including:
pie charts;
line charts and horizontal/vertical bar charts (regular and stacked, with optional 3D-effect);
XY plots and scatter plots;
time series, high/low/open/close charts and candle stick charts;
combination charts;
Pareto charts;
Gantt charts;
bubble charts;
wind plots, meter charts and symbol charts;
四:创建曲线图
// create a default chart based on some sample data...
//曲线图标题
String title = "趋势分析";
//曲线图X轴提示
String domain = "月份走势";
//曲线图Y轴提示
String range = "应收余额";
//曲线图自标题
String subtitleStr = "2003财年分析";
//创建时间数据源
//每一个TimeSeries在图上是一条曲线
TimeSeries ca = new TimeSeries("用友");
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
//ca.add(new Month(mon + 1, i), new Double(500 + Math.random() * 100));
//TimeSeriesDataPair是一个时间点的数值体现
ca.add(
new TimeSeriesDataPair(
new Day(1, mon + 1, i),
new Double(500 + Math.random() * 100)));
}
}
TimeSeries ibm = new TimeSeries("金碟");
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));
ibm.add(
new TimeSeriesDataPair(
new Day(1, mon + 1, i),
new Double(400 - Math.random() * 100)));
}
}
TimeSeries king = new TimeSeries("东软");
for (int i = 1999; i < 2005; i++) {
for (int mon = 0; mon < 12; mon++) {
//ibm.add(new Month(mon+1,i),new Double(400-Math.random()*100));
king.add(
new TimeSeriesDataPair(
new Day(1, mon + 1, i),
new Double(300 - Math.random() * 100)));
}
}
//时间曲线数据集合
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(ca);
dataset.addSeries(ibm);
dataset.addSeries(king);
//dataset.addSeries(jpy);
//dataset.addSeries(mav);
//时间曲线元素
JFreeChart chart =
ChartFactory.createTimeSeriesChart(
title,
domain,
range,
dataset,
true,
true,
false);
// then customise it a little...
TextTitle subtitle =
new TextTitle(subtitleStr, new Font("黑体", Font.BOLD, 12));
chart.addSubtitle(subtitle);
chart.setTitle(new TextTitle(title, new Font("隶书", Font.ITALIC, 15)));
//pie.setSeriesLabelFont(new Font("黑体", Font.BOLD, 15));
chart.setBackgroundPaint(
new GradientPaint(0, 0, Color.white, 0, 1000, Color.blue));
//sysout
//输出文件到指定目录
String rfname = MathUtil.getRoundCode(22) + ".jpeg";
String fileName = "d:/test/" + rfname;
try {
//for
//System.out.println();
ChartUtilities.saveChartAsJPEG(new File(fileName), 100, chart, 600, 600);
// log.info("....Create image File:" + fileName);
} catch (IOException exz) {
System.out.print("....Cant’t Create image File");
}
五:创建柱状图
String title = "柱状图测试";
String domain = "单位比较";
String range = "数值";
//CategoryDataset data = DemoDatasetFactory.createCategoryDataset();
DefaultCategoryDataset data = new DefaultCategoryDataset();
for (int r = 0; r < 5; r++) {
String rowKey = "单位 [" + (r + 1)+"]" ;
//第一层循环:分析对象
for (int c = 0; c < 6; c++) {
//第二层循环:分析对象在时间点上的数据
String columnKey = "2001年" + (c + 1) + "月";
data.addValue(new Double(r * c + 5), rowKey, columnKey);
}
}