JFreeChart时序图

使用的jfreecart版本为jfreechart-1.0.14.jar,如果要使用jfreechart,还必须要导入其依赖包jcommon,使用的版本为jcommon-1.0.17.jar

下图展示了所使用的jfreechart基本类之间的关系:

public class charttest {
	public static void main(String[] args) {
		JFreeChart chart = 
			ChartFactory.createTimeSeriesChart("示例", "x轴名称", "y轴名称", 
					getDataSet(), true, false, false);
		chart.getTitle().setFont(new Font("宋体", Font.PLAIN, 12));//标题
		chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));//图例
		XYPlot plot = chart.getXYPlot();//图表
		
		DateAxis x = (DateAxis)plot.getDomainAxis();//x轴
		x.setLabelFont(new Font("宋体", Font.PLAIN, 12));//x轴名称字体
		x.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//x轴节点字体
		x.setDateFormatOverride(new SimpleDateFormat("yyyy-MM-dd HH:mm"));//x轴节点格式
		
		NumberAxis y = (NumberAxis)plot.getRangeAxis();//y轴
		y.setLabelFont(new Font("宋体", Font.PLAIN, 12));//y轴名称字体
		y.setTickLabelFont(new Font("宋体", Font.PLAIN, 12));//y轴节点字体
		NumberFormat nf = NumberFormat.getInstance();
		nf.setMaximumFractionDigits(1);
		y.setNumberFormatOverride(nf);//y轴节点格式
		
		XYLineAndShapeRenderer render = 
			(XYLineAndShapeRenderer) plot.getRenderer();//图表式样
		plot.setBackgroundPaint(Color.LIGHT_GRAY);//背景颜色
	    plot.setDomainGridlinePaint(Color.green);//网格竖线颜色
	     plot.setRangeGridlinePaint(Color.green);//网格横线颜色
		render.setBaseShapesVisible(true);//显示节点

		
		FileOutputStream output;
		try {
			output = new FileOutputStream("D:\\test.jpg");
			ChartUtilities.writeChartAsPNG(output, chart, 450, 400);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private static XYDataset getDataSet(){
		TimeSeriesCollection tsSet = new TimeSeriesCollection();
		TimeSeries ts = new TimeSeries("test");
		ts.add(new Day(1, 1, 2013), 11.1);
		ts.add(new Day(2, 1, 2013), 12.2);
		//ts.add(new Month(2, 2013), 133);
		//ts.add(new Month(3, 2013), 111);
		tsSet.addSeries(ts);
		return tsSet;
	}
}

显示的结果为:


 

 

 

你可能感兴趣的:(JFreeChart时序图)