jfreechart timeserieschart

阅读更多
	Calendar calendar = Calendar.getInstance();
		calendar.setTime(new java.util.Date());
		calendar.add(java.util.Calendar.DAY_OF_YEAR,-1);
		String c1 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(calendar.getTime());
        String c2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new java.util.Date());
		
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        long date1 = 0L;
        long date2 =0L;
        try {
			date1 = sdf.parse(c1).getTime();
	        date2 = sdf.parse(c2).getTime();

		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		//放置数据集
        Map map = getEndpointStatus(date1,date2);
        
        TimeSeriesCollection dataset = new TimeSeriesCollection();
		TimeSeries ts = new TimeSeries(InternationalUtil.getI18nUtil().getValue("vms.endpoint.usage"),Second.class);
		Iterator iter = map.keySet().iterator();
		while(iter.hasNext()){
			long t = iter.next();
			Date dt = new Date(t);
			ts.addOrUpdate(new Second(dt), (long)map.get(t));
		}
		dataset.addSeries(ts);
		
		ServletOutputStream out = response.getOutputStream();
		JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
		JFreeChart jc = null;
		jc = ChartFactory.createTimeSeriesChart(InternationalUtil.getI18nUtil().getValue("vms.endpoint.usage"),null,null,dataset, true, true, false);
	
		Plot plot = jc.getPlot();
		//设置没有数据时显示的信息
		((XYPlot) plot).setNoDataMessage("no data");
		NumberAxis numberaxis = (NumberAxis)((XYPlot) plot).getRangeAxis();
		//设置y轴为整数
		numberaxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits());
		//设置背景颜色
		jc.setBackgroundPaint(Color.WHITE);
		BufferedImage bimg = jc.createBufferedImage(700, 300);
		encoder.encode(bimg);

		out.flush();
		out.close();

你可能感兴趣的:(jfreechart)