jfreechar 曲线图

网上再加工的,呵呵.

本例实现了,俩个曲线数据的对比情况. 相关的jar包,大家可以去官网进行下载后,

将lib中的相关包进行导入即可.  下面的只是随便的测试例子,代码没有进行整理



package pic;

import java.awt.Color;
import java.awt.Font;
import java.io.File;
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.title.TextTitle;
import org.jfree.data.time.Day;
import org.jfree.data.time.TimeSeries;
import org.jfree.data.time.TimeSeriesCollection;


public class FreeCharTest {


//
/**
* 曲线图

* Nov 12, 2011 10:37:58 PM
* @throws Exception
*/
public void curveMake() throws Exception {

//这是选用了Day.class ,我的x轴式到日的,当然可以是Month

TimeSeries timeSeries = new TimeSeries("曲线1", Day.class); 
// 时间曲线数据集合
TimeSeriesCollection lineDataset = new TimeSeriesCollection();
// 构造数据集合
timeSeries.add(new Day(1, 1, 2007), 11200);
timeSeries.add(new Day(2, 1, 2007), 9000);
timeSeries.add(new Day(3, 1, 2007), 6200);
timeSeries.add(new Day(4, 1, 2007), 8200);
timeSeries.add(new Day(5, 1, 2007), 8200);
timeSeries.add(new Day(6, 1, 2007), 11200);
timeSeries.add(new Day(7, 1, 2007), 9000);
timeSeries.add(new Day(8, 1, 2007), 6200);
timeSeries.add(new Day(9, 1, 2007), 8200);
timeSeries.add(new Day(10, 1, 2007), 8200);


TimeSeries timeSeries2 = new TimeSeries("曲线2", Day.class);
timeSeries2.add(new Day(1, 1, 2007), 11100);
timeSeries2.add(new Day(2, 1, 2007), 8000);
timeSeries2.add(new Day(3, 1, 2007), 6000);
timeSeries2.add(new Day(4, 1, 2007), 8000);
timeSeries2.add(new Day(5, 1, 2007), 9000);
timeSeries2.add(new Day(6, 1, 2007), 11200);
timeSeries2.add(new Day(7, 1, 2007), 9000);
timeSeries2.add(new Day(8, 1, 2007), 6200);
timeSeries2.add(new Day(9, 1, 2007), 8200);
timeSeries2.add(new Day(10, 1, 2007), 8200);
timeSeries2.add(new Day(11, 1, 2007), 9000);
timeSeries2.add(new Day(12, 1, 2007), 6200);
timeSeries2.add(new Day(13, 1, 2007), 8200);
timeSeries2.add(new Day(14, 1, 2007), 8200);

               //将俩个数据集合都加入
lineDataset.addSeries(timeSeries);
lineDataset.addSeries(timeSeries2);


JFreeChart chart = ChartFactory.createTimeSeriesChart("对比曲线", "日期", // x轴说明
"百分比",// y轴说明
lineDataset, true, true, true);
// 设置子标题
// TextTitle subtitle = new TextTitle("2007年度", new Font("黑体",
// Font.BOLD, 12));
// chart.addSubtitle(subtitle);
// //设置主标题
chart.setTitle(
new TextTitle("积极进取组合与上证指数收益对比",
   new Font("隶书",
Font.ITALIC, 15)));
chart.setAntiAlias(true);

chart.getXYPlot().setBackgroundPaint(Color.white);   //设置图片的背景



                //字体的设置
XYPlot plot = chart.getXYPlot();// 图形的绘制结构对象
Font xfont = new Font("宋体", Font.PLAIN, 12);// X轴
Font yfont = new Font("宋体", Font.PLAIN, 12);// Y轴
Font kfont = new Font("宋体", Font.PLAIN, 12);// 底部
chart.getLegend().setItemFont(kfont);


// X 轴
ValueAxis domainAxis = plot.getDomainAxis();
domainAxis.setLabelFont(xfont);// 轴标题
domainAxis.setTickLabelFont(xfont);// 轴数值
domainAxis.setTickLabelPaint(Color.BLACK); // 字体颜色
//domainAxis.setVisible(false);//设置X轴的,提示日期不可以见
((DateAxis)domainAxis).setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd"));
// domainAxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
// // 横轴上的label斜显示



// Y 轴
ValueAxis rangeAxis = plot.getRangeAxis();
rangeAxis.setLabelFont(yfont);
rangeAxis.setLabelPaint(Color.BLACK); // 字体颜色
rangeAxis.setTickLabelFont(yfont);


File file = new File("D:\\test.jpg");
ChartUtilities.saveChartAsJPEG(file, chart, 350, 300);//jfreechar 可以支持多中保存格式,还有png等
}


public static void main(String[] args) throws Exception {
FreeCharTest fct = new FreeCharTest();
fct.curveMake();
}
}

你可能感兴趣的:(exception,String,File,jfreechart,Class,plot)