http://dev.firnow.com/course/3_program/java/javajs/20090327/163636.html
写道
package com.ijo.footballdelta;
import junit.framework.TestCase;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.annotations.XYBoxAnnotation;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
//JFreeChart LineXY Chart(折线图)
public class CreateJFreeChartXYLineTest extends TestCase {
/**
* 创建JFreeChart LineXY Chart(折线图)
*/
public void jFreeChar() {
//步骤1:创建XYDataset对象(准备数据)
XYDataset dataset = createXYDataset();
//步骤2:根据Dataset 生成 JFreeChart对象,以及做相应的设置
JFreeChart freeChart = createChart(dataset);
//步骤3:将JFreeChart对象输出到文件,Servlet输出流等
saveAsFile(freeChart, "/Users/17ijotechnologies/Desktop/1.png", 800, 600);
}
// 保存为文件
public static void saveAsFile(JFreeChart chart, String outputPath,
int weight, int height) {
FileOutputStream out = null;
try {
File outFile = new File(outputPath);
if (!outFile.getParentFile().exists()) {
outFile.getParentFile().mkdirs();
}
out = new FileOutputStream(outputPath);
// 保存为PNG
ChartUtilities.writeChartAsPNG(out, chart, 300, 200);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}
// 根据XYDataset创建JFreeChart对象
public static JFreeChart createChart(XYDataset dataset) {
// 创建JFreeChart对象:ChartFactory.createXYLineChart
JFreeChart jfreechart = ChartFactory.createXYLineChart("XYLine Chart Demo", // 标题
"X", // categoryAxisLabel (category 轴,横轴,X轴标签)
"Y", // valueAxisLabel(value 轴,纵轴,Y轴的标签)
dataset, // dataset
PlotOrientation.VERTICAL, true, // legend
false, // tooltips
false); // URLs
// 使用CategoryPlot设置各种参数。以下设置可以省略。
XYPlot plot = jfreechart.getXYPlot();
// LineAndShapeRenderer renderer = (LineAndShapeRenderer)plot.getRenderer();
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();
//设置第一条线有数据点
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesLinesVisible(2, true);
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, true);
renderer.setSeriesShapesVisible(2, true);
//renderer.setDrawOutlines(true);
//可以重新设置数据点颜色默认为白色方形
renderer.setUseFillPaint(true);
// renderer.isSeriesVisible()
//数据点填充颜色
renderer.setFillPaint(Color.yellow);
//设置数据点为圆形(x为上下偏离,y为左右偏离,w为圆形左右距离,h为圆形上下距离)
renderer.setSeriesShape(0, new Ellipse2D.Double(-1.0, -1.0, 5.0, 5.0));
// renderer.setSeriesShape(0, ShapeUtilities.createUpTriangle(3.0f));
// 背景色 透明度
plot.setBackgroundAlpha(0.5f);
// 前景色 透明度
plot.setForegroundAlpha(0.5f);
// 其它设置可以参考XYPlot类
// AbstractRenderer
XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 2002, 60);
plot.addAnnotation(box);
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setAxisLineStroke(new BasicStroke(1.6f)); // 设置轴线粗细
rangeAxis.setAxisLinePaint(Color.BLACK); // 设置轴线颜色
rangeAxis.setUpperBound(50); // 设置坐标最大值
rangeAxis.setTickMarkStroke(new BasicStroke(1.6f)); // 设置坐标标记大小
rangeAxis.setTickMarkPaint(Color.BLACK); // 设置坐标标记颜色
rangeAxis.setLabelFont(new Font("黑体", Font.BOLD, 16)); // 设置坐标轴标题字体
rangeAxis.setLabelPaint(Color.BLACK); // 设置坐标轴标题颜色
rangeAxis.setLabelAngle(Math.PI / 2); // 设置坐标轴标题旋转角度
return jfreechart;
}
/**
* 创建XYDataset对象
*/
private static XYDataset createXYDataset() {
XYSeries xyseries1 = new XYSeries("One");
xyseries1.add(1980, 50);
xyseries1.add(1981, 49);
xyseries1.add(1982, 48);
xyseries1.add(1983, 47);
xyseries1.add(1984, 46);
xyseries1.add(1985, 45);
xyseries1.add(1986, 44);
xyseries1.add(1987, 43);
xyseries1.add(1988, 42);
xyseries1.add(1989, 41);
xyseries1.add(1990, 40);
xyseries1.add(1991, 39);
xyseries1.add(1992, 38);
xyseries1.add(1993, 37);
xyseries1.add(1994, 36);
xyseries1.add(1995, 35);
xyseries1.add(1996, 34);
xyseries1.add(1997, 33);
xyseries1.add(1998, 32);
xyseries1.add(2001, 33);
xyseries1.add(2002, 34);
xyseries1.add(2003, 35);
xyseries1.add(2004, 36);
XYSeries xyseries2 = new XYSeries("Three");
xyseries2.add(1987, 40);
xyseries2.add(1997, 30.0008);
xyseries2.add(2007, 38.24);
XYSeries xyseries3 = new XYSeries("Two");
for (int i = 1980; i < 2007; i++) {
Random rand = new Random();
xyseries3.add(i, rand.nextInt(50));
}
XYSeriesCollection xySeriesCollection = new XYSeriesCollection();
xySeriesCollection.addSeries(xyseries1);
xySeriesCollection.addSeries(xyseries2);
xySeriesCollection.addSeries(xyseries3);
return xySeriesCollection;
}
}