核心代码就是要将一个JFreeChart 对象,写到BufferedImage中。JFreeChart 中没有提供类似getImage(), getBufferedImage()方法,而是提供了一个createBufferedImage方法。
在这个例子中,您还可以找到如何解决JFreeChart的时间乱码问题。
JFreeChart chart = createChart(data);
BufferedImage image = chart.createBufferedImage(800, 300,
BufferedImage.TYPE_INT_RGB, null);
所有源码可以在附件中找到, 需要依赖JFreeChart的Jar包。 其余就是自己改改包路径,相信这个没啥难度。就两个类,一个用于处理JFrame和JFreeChart的逻辑,一个用于画图。
TimeCharttest
package com.abczww.test.t1;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Font;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.ValueMarker;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.time.Month;
import org.jfree.data.time.Quarter;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
public class TimeCharttest extends JFrame {
private static final long serialVersionUID = 2319942903676246265L;
private ImagePanel imagePanel = new ImagePanel();
private TimeCharttest() throws IOException {
XYDataset data = createDataset();
JFreeChart chart = createChart(data);
BufferedImage image = chart.createBufferedImage(800, 300,
BufferedImage.TYPE_INT_RGB, null);
imagePanel.setImage(image);
Container con = this.getContentPane();
con.add(imagePanel, BorderLayout.CENTER);
this.setSize(900, 400);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) throws IOException {
new TimeCharttest();
}
private static XYDataset createDataset() {
TimeSeries timeseries = new TimeSeries("L&G 欧洲调查");
timeseries.add(new Month(2, 2001), 181.8D);// 这里用的是Month.class,同样还有Day.class
// Year.class 等等
timeseries.add(new Month(3, 2001), 167.3D);
timeseries.add(new Month(4, 2001), 153.8D);
timeseries.add(new Month(5, 2001), 167.6D);
timeseries.add(new Month(6, 2001), 158.8D);
timeseries.add(new Month(7, 2001), 148.3D);
timeseries.add(new Month(8, 2001), 153.9D);
timeseries.add(new Month(9, 2001), 142.7D);
timeseries.add(new Month(10, 2001), 123.2D);
timeseries.add(new Month(11, 2001), 131.8D);
timeseries.add(new Month(12, 2001), 139.6D);
timeseries.add(new Month(1, 2002), 142.9D);
timeseries.add(new Month(2, 2002), 138.7D);
timeseries.add(new Month(3, 2002), 137.3D);
timeseries.add(new Month(4, 2002), 143.9D);
timeseries.add(new Month(5, 2002), 139.8D);
timeseries.add(new Month(6, 2002), 137D);
timeseries.add(new Month(7, 2002), 132.8D);
TimeSeries timeseries1 = new TimeSeries("L&G 韩国调查");
timeseries1.add(new Month(2, 2001), 129.6D);
timeseries1.add(new Month(3, 2001), 123.2D);
timeseries1.add(new Month(4, 2001), 117.2D);
timeseries1.add(new Month(5, 2001), 124.1D);
timeseries1.add(new Month(6, 2001), 122.6D);
timeseries1.add(new Month(7, 2001), 119.2D);
timeseries1.add(new Month(8, 2001), 116.5D);
timeseries1.add(new Month(9, 2001), 112.7D);
timeseries1.add(new Month(10, 2001), 101.5D);
timeseries1.add(new Month(11, 2001), 106.1D);
timeseries1.add(new Month(12, 2001), 110.3D);
timeseries1.add(new Month(1, 2002), 111.7D);
timeseries1.add(new Month(2, 2002), 111D);
timeseries1.add(new Month(3, 2002), 109.6D);
timeseries1.add(new Month(4, 2002), 113.2D);
timeseries1.add(new Month(5, 2002), 111.6D);
timeseries1.add(new Month(6, 2002), 108.8D);
timeseries1.add(new Month(7, 2002), 101.6D);
TimeSeriesCollection timeseriescollection = new TimeSeriesCollection();
timeseriescollection.addSeries(timeseries);
timeseriescollection.addSeries(timeseries1);
// timeseriescollection.setDomainIsPointsInTime(true); //
// domain轴上的刻度点代表的是时间点而不是时间段
return timeseriescollection;
}
// 2、由ChartFactory 产生 JFreeChart 对象
private static JFreeChart createChart(XYDataset xydataset) {
JFreeChart jfreechart = ChartFactory.createTimeSeriesChart("L&G单位价格调查",
"日期", "价格/单位", xydataset, true, true, false);
jfreechart.setBackgroundPaint(Color.white);
XYPlot xyplot = (XYPlot) jfreechart.getPlot(); // 获得 plot : XYPlot!!
xyplot.setBackgroundPaint(Color.lightGray);
xyplot.setDomainGridlinePaint(Color.white);// 网格的颜色
xyplot.setRangeGridlinePaint(Color.white);// 网格的颜色
xyplot.setAxisOffset(new RectangleInsets(5D, 5D, 5D, 5D));
xyplot.setDomainCrosshairVisible(true);
xyplot.setRangeCrosshairVisible(true);
ValueAxis categoryaxis = xyplot.getDomainAxis(); // 横轴上的
categoryaxis.setPositiveArrowVisible(true);// 增加横轴的箭头
xyplot.getRangeAxis().setPositiveArrowVisible(true);// 增加纵轴的箭头
categoryaxis.setTickLabelFont(new Font("SansSerif", 10, 12));// 设定字体、类型、字号
// 一些重要的方法:
// A、增加标记线:
xyplot.addRangeMarker(new ValueMarker(550D)); // 数值轴
Quarter quarter = new Quarter(2, 2002);
xyplot.addDomainMarker(new ValueMarker(quarter.getMiddleMillisecond())); // 时间轴
// xyplot.setRangeGridlinePaint(Color.blue);
// -----------------------
org.jfree.chart.renderer.xy.XYItemRenderer xyitemrenderer = xyplot
.getRenderer();
if (xyitemrenderer instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyitemrenderer;
xylineandshaperenderer.setBaseShapesVisible(true); // 数据点可见
xylineandshaperenderer.setBaseShapesFilled(true); // 数据点是实心点
}
// B、数据点的调整
XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer) xyplot
.getRenderer();
xylineandshaperenderer.setBaseShapesVisible(true); // 数据点可见
xylineandshaperenderer.setSeriesFillPaint(0, Color.red); // 数据点填充为红色
xylineandshaperenderer.setSeriesFillPaint(1, Color.white); // 数据点填充为白色
xylineandshaperenderer.setUseFillPaint(true); // 应用
DateAxis dateaxis = (DateAxis) xyplot.getDomainAxis(); // 对domain
// 轴上日期显示格式定义
dateaxis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
// 解决中文乱码问题,共要处理这三部分
// 1、对标题
Font font1 = new Font("SimSun", 10, 20); // 设定字体、类型、字号
jfreechart.getTitle().setFont(font1); // 标题
// 2、对图里面的汉字设定,也就是Plot的设定
Font font2 = new Font("SimSun", 10, 16); // 设定字体、类型、字号
xyplot.getDomainAxis().setLabelFont(font2);// 相当于横轴或理解为X轴
xyplot.getRangeAxis().setLabelFont(font2);// 相当于竖轴理解为Y轴
// 3、下面的方块区域是 LegendTitle 对象
Font font3 = new Font("SimSun", 10, 12); // 设定字体、类型、字号
jfreechart.getLegend().setItemFont(font3);// 最下方
return jfreechart;
}
}
ImagePanel
package com.abczww.test.t1;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import javax.swing.JPanel;
public class ImagePanel extends JPanel {
private static final long serialVersionUID = 4644786195524096243L;
private BufferedImage image;
public ImagePanel() {
super();
}
public void setImage(BufferedImage image) {
this.image = image;
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.white);
// img = Toolkit.getDefaultToolkit().getImage("C:\\test.jpg");
if (null != image) {
this.setSize(image.getWidth(this), image.getHeight(this));
g.fill3DRect(0, 0, image.getWidth(this), image.getHeight(this),
true);
g.drawImage(image, 0, 0, null, this);
setPreferredSize(new Dimension(image.getWidth(this),
image.getHeight(this)));
}
}
}