写道
package com.ijo.demo;
import java.awt.BasicStroke;
import java.awt.Color;
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;
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.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
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 org.jfree.ui.RectangleInsets;
public class CreateJFreeChartXYLineTest {
public static void main(String[] args) {
CreateJFreeChartXYLineTest test = new CreateJFreeChartXYLineTest();
test.jFreeChar();
}
/**
* 创建JFreeChart LineXY Chart(折线图)
*/
public void jFreeChar() {
// 步骤1:创建XYDataset对象(准备数据)
XYDataset dataset = createXYDataset();
// 步骤2:根据Dataset 生成 JFreeChart对象,以及做相应的设置
JFreeChart freeChart = createChart(dataset);
// 步骤3:将JFreeChart对象输出到文件,Servlet输出流等
saveAsFile(freeChart, "C:\\Documents and Settings\\zhao\\桌面\\1.png",
400, 400);
}
// 保存为文件
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, weight, height);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}
public static JFreeChart createChart(XYDataset dataset) {
JFreeChart jfreechart = ChartFactory.createXYLineChart("test", // title
"", // categoryAxisLabel (category 轴,横轴,X轴标签)
"", // valueAxisLabel(value 轴,纵轴,Y轴的标签)
dataset, // dataset
PlotOrientation.VERTICAL, false, // legend
false, // tooltips
false); // URLs
// jfreechart.setTitle(new TextTitle("vote", new
// java.awt.Font("宋体",Font.BOLD, 18)));
// 使用CategoryPlot设置各种参数。以下设置可以省略。
XYPlot plot = jfreechart.getXYPlot();
plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
.getRenderer();
// 设置第一条线有数据点
// line or node
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesLinesVisible(2, true);
// Is it show a shape of customer
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesShapesVisible(2, true);
// set a line color
renderer.setSeriesPaint(0, new Color(204, 255, 255));
renderer.setSeriesPaint(1, new Color(153, 204, 255));
renderer.setSeriesPaint(2, new Color(51, 204, 204));
// set a line fill color
renderer.setSeriesFillPaint(0, new Color(204, 255, 255));
renderer.setSeriesFillPaint(1, new Color(153, 204, 255));
renderer.setSeriesFillPaint(2, new Color(51, 204, 204));
// set a line width
renderer.setSeriesStroke(0, new BasicStroke(5f));
renderer.setSeriesStroke(1, new BasicStroke(10f));
renderer.setSeriesStroke(2, new BasicStroke(1.5f));
// draw a crycle
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, Math.abs(25),
Math.abs(25)));
// set a middle line
XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 1990, 60);
plot.addAnnotation(box);
// Is it show gridline
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
// x line
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
domainAxis.setAutoRange(true);
// domainAxis.setAutoRangeMinimumSize(1980);
NumberAxis numberAxis = (NumberAxis) domainAxis;
numberAxis.setAutoTickUnitSelection(false);
numberAxis.setTickUnit(new NumberTickUnit(5));
// 纵坐标值只能是1980-2008之间的值
numberAxis.setRangeWithMargins(1980, 2010);
// begin point value
// numberAxis.setLowerBound(0);
// numberAxis.setUpperBound(2010);
numberAxis.setAutoRangeIncludesZero(true);
// domainAxis.setTickLabelsVisible(false); //label value
domainAxis.setTickMarksVisible(false); // show mark
// y
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setVisible(false);
// rangeAxis.setRangeWithMargins(0, 50);
rangeAxis.setLowerBound(0);
rangeAxis.setUpperBound(50);
return jfreechart;
}
/**
* 创建XYDataset对象
*/
private static XYDataset createXYDataset() {
XYSeries xyseries1 = new XYSeries("");
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("");
xyseries2.add(1987, 40);
xyseries2.add(1997, 30.0008);
xyseries2.add(2007, 38.24);
XYSeries xyseries3 = new XYSeries("");
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;
}
}
import java.awt.BasicStroke;
import java.awt.Color;
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;
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.NumberAxis;
import org.jfree.chart.axis.NumberTickUnit;
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 org.jfree.ui.RectangleInsets;
public class CreateJFreeChartXYLineTest {
public static void main(String[] args) {
CreateJFreeChartXYLineTest test = new CreateJFreeChartXYLineTest();
test.jFreeChar();
}
/**
* 创建JFreeChart LineXY Chart(折线图)
*/
public void jFreeChar() {
// 步骤1:创建XYDataset对象(准备数据)
XYDataset dataset = createXYDataset();
// 步骤2:根据Dataset 生成 JFreeChart对象,以及做相应的设置
JFreeChart freeChart = createChart(dataset);
// 步骤3:将JFreeChart对象输出到文件,Servlet输出流等
saveAsFile(freeChart, "C:\\Documents and Settings\\zhao\\桌面\\1.png",
400, 400);
}
// 保存为文件
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, weight, height);
out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null) {
try {
out.close();
} catch (IOException e) {
// do nothing
}
}
}
}
public static JFreeChart createChart(XYDataset dataset) {
JFreeChart jfreechart = ChartFactory.createXYLineChart("test", // title
"", // categoryAxisLabel (category 轴,横轴,X轴标签)
"", // valueAxisLabel(value 轴,纵轴,Y轴的标签)
dataset, // dataset
PlotOrientation.VERTICAL, false, // legend
false, // tooltips
false); // URLs
// jfreechart.setTitle(new TextTitle("vote", new
// java.awt.Font("宋体",Font.BOLD, 18)));
// 使用CategoryPlot设置各种参数。以下设置可以省略。
XYPlot plot = jfreechart.getXYPlot();
plot.setAxisOffset(new RectangleInsets(0, 0, 0, 0));
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot
.getRenderer();
// 设置第一条线有数据点
// line or node
renderer.setSeriesLinesVisible(0, false);
renderer.setSeriesLinesVisible(1, true);
renderer.setSeriesLinesVisible(2, true);
// Is it show a shape of customer
renderer.setSeriesShapesVisible(0, true);
renderer.setSeriesShapesVisible(1, false);
renderer.setSeriesShapesVisible(2, true);
// set a line color
renderer.setSeriesPaint(0, new Color(204, 255, 255));
renderer.setSeriesPaint(1, new Color(153, 204, 255));
renderer.setSeriesPaint(2, new Color(51, 204, 204));
// set a line fill color
renderer.setSeriesFillPaint(0, new Color(204, 255, 255));
renderer.setSeriesFillPaint(1, new Color(153, 204, 255));
renderer.setSeriesFillPaint(2, new Color(51, 204, 204));
// set a line width
renderer.setSeriesStroke(0, new BasicStroke(5f));
renderer.setSeriesStroke(1, new BasicStroke(10f));
renderer.setSeriesStroke(2, new BasicStroke(1.5f));
// draw a crycle
renderer.setSeriesShape(0, new Ellipse2D.Double(0, 0, Math.abs(25),
Math.abs(25)));
// set a middle line
XYBoxAnnotation box = new XYBoxAnnotation(0, 0, 1990, 60);
plot.addAnnotation(box);
// Is it show gridline
plot.setRangeGridlinesVisible(false);
plot.setDomainGridlinesVisible(false);
// x line
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setVisible(false);
domainAxis.setAutoRange(true);
// domainAxis.setAutoRangeMinimumSize(1980);
NumberAxis numberAxis = (NumberAxis) domainAxis;
numberAxis.setAutoTickUnitSelection(false);
numberAxis.setTickUnit(new NumberTickUnit(5));
// 纵坐标值只能是1980-2008之间的值
numberAxis.setRangeWithMargins(1980, 2010);
// begin point value
// numberAxis.setLowerBound(0);
// numberAxis.setUpperBound(2010);
numberAxis.setAutoRangeIncludesZero(true);
// domainAxis.setTickLabelsVisible(false); //label value
domainAxis.setTickMarksVisible(false); // show mark
// y
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setVisible(false);
// rangeAxis.setRangeWithMargins(0, 50);
rangeAxis.setLowerBound(0);
rangeAxis.setUpperBound(50);
return jfreechart;
}
/**
* 创建XYDataset对象
*/
private static XYDataset createXYDataset() {
XYSeries xyseries1 = new XYSeries("");
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("");
xyseries2.add(1987, 40);
xyseries2.add(1997, 30.0008);
xyseries2.add(2007, 38.24);
XYSeries xyseries3 = new XYSeries("");
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;
}
}