package com.langhua.time;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.time.Month;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;
import org.jfree.ui.RectangleInsets;
/**
* 坐标是时间例子
* @author Administrator
*
*/
public class TimeSeriesTest {
public static void main(String[] args) {
//初始化数据
TimeSeries s1 = new TimeSeries("L&G European Index Trust中文", Month.class);
s1.add(new Month(2, 2001), 181.8);
s1.add(new Month(3, 2001), 167.3);
s1.add(new Month(4, 2001), 153.8);
s1.add(new Month(5, 2001), 167.6);
s1.add(new Month(6, 2001), 158.8);
s1.add(new Month(7, 2001), 148.3);
s1.add(new Month(8, 2001), 153.9);
s1.add(new Month(9, 2001), 142.7);
s1.add(new Month(10, 2001), 123.2);
s1.add(new Month(11, 2001), 131.8);
s1.add(new Month(12, 2001), 139.6);
s1.add(new Month(1, 2002), 142.9);
s1.add(new Month(2, 2002), 138.7);
s1.add(new Month(3, 2002), 137.3);
s1.add(new Month(4, 2002), 143.9);
s1.add(new Month(5, 2002), 139.8);
s1.add(new Month(6, 2002), 137.0);
s1.add(new Month(7, 2002), 132.8);
TimeSeries s2 = new TimeSeries("L&G UK Index Trust中文", Month.class);
s2.add(new Month(2, 2001), 129.6);
s2.add(new Month(3, 2001), 123.2);
s2.add(new Month(4, 2001), 117.2);
s2.add(new Month(5, 2001), 124.1);
s2.add(new Month(6, 2001), 122.6);
s2.add(new Month(7, 2001), 119.2);
s2.add(new Month(8, 2001), 116.5);
s2.add(new Month(9, 2001), 112.7);
s2.add(new Month(10, 2001), 101.5);
s2.add(new Month(11, 2001), 106.1);
s2.add(new Month(12, 2001), 110.3);
s2.add(new Month(1, 2002), 111.7);
s2.add(new Month(2, 2002), 111.0);
s2.add(new Month(3, 2002), 109.6);
s2.add(new Month(4, 2002), 113.2);
s2.add(new Month(5, 2002), 111.6);
s2.add(new Month(6, 2002), 108.8);
s2.add(new Month(7, 2002), 101.6);
TimeSeriesCollection dataset = new TimeSeriesCollection();
dataset.addSeries(s1);
dataset.addSeries(s2);
//创建 相应的对象
JFreeChart chart = ChartFactory.createTimeSeriesChart("日期","Date时间","Price价格",dataset,true, true, false );
//配置字体等格式
TimeSeriesTest.configFont(chart);
//设置背影色,由于这个例是COPY过来的,并没有放到TimeSeriesTest.configFont(chart);这个方法里面
chart.setBackgroundPaint(Color.white);
//获得相当对象
XYPlot plot = (XYPlot) chart.getPlot();
//背影色
plot.setBackgroundPaint(Color.lightGray);
//X轴网格线和Y轴网格线颜色
plot.setDomainGridlinePaint(Color.white);
plot.setRangeGridlinePaint(Color.white);
//以下还不清楚
plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));
plot.setDomainCrosshairVisible(true);
plot.setRangeCrosshairVisible(true);
//以下也不清楚
XYItemRenderer r = plot.getRenderer();
if (r instanceof XYLineAndShapeRenderer) {
XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
renderer.setBaseShapesVisible(true);
renderer.setBaseShapesFilled(true);
}
//设置X坐标上面的格式
DateAxis axis = (DateAxis) plot.getDomainAxis();
axis.setDateFormatOverride(new SimpleDateFormat("MMM-yyyy"));
FileOutputStream fos_jpg = null;
try {
fos_jpg = new FileOutputStream("c:\\TimeSeries.jpg");
ChartUtilities.writeChartAsJPEG(fos_jpg,0.99f,chart,800,600, null);
fos_jpg.close();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 配置字体等信息
* @param chart
*/
private static void configFont(JFreeChart chart) {
// 配置字体
Font xfont = new Font("宋体", Font.CENTER_BASELINE, 12);// X轴
Font yfont = new Font("宋体", Font.CENTER_BASELINE, 20);// Y轴
Font kfont = new Font("宋体", Font.CENTER_BASELINE, 18);// 底部
Font titleFont = new Font("微软雅黑", Font.CENTER_BASELINE, 25); // 图片标题
XYPlot plot = (XYPlot) chart.getPlot();// 图形的绘制结构对象
//数据轴网格线条颜色
plot.setRangeGridlinePaint(Color.BLUE);
//数据轴网格线条笔触
plot.setRangeGridlineStroke(new BasicStroke(1.0f));
// 图片标题
chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
chart.getLegend().setItemFont(kfont);
//X轴
ValueAxis domainAxis = plot.getDomainAxis();
//设置X轴标题字体
domainAxis.setLabelFont(xfont);
//设置X轴字体
domainAxis.setTickLabelFont(xfont);
//设置字体颜色
domainAxis.setTickLabelPaint(Color.BLUE);
//横轴上的label斜显示
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
//domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45);
//分类轴边距,同种类型之间的距离,比如说Row 1之间的
//domainAxis.setCategoryMargin(0.2f);
//分类轴下(左)边距,就是离左边的距离
domainAxis.setLowerMargin(0.1);
//分类轴下(右)边距,就是离最右边的距离
domainAxis.setUpperMargin(0.1);
//Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
//设置Y轴标题字体
rangeAxis.setLabelFont(yfont);
//设置Y轴字体
rangeAxis.setTickLabelFont(yfont);
// 字体颜色
rangeAxis.setLabelPaint(Color.RED);
}
}
Jfreechart学习笔记4-简单Time类型图形创建
最新推荐文章于 2024-11-10 14:34:06 发布