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;
	}
}

显示的结果为:


 

 

 

转载于:https://www.cnblogs.com/sean-zou/archive/2013/04/03/3710074.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值