利用TimeSeriesCollection生成的x轴为时间的折线图

package com.potevio.rnd.tobacco.mine;

import java.util.Map;

/**
 * @description 数据BEAN
 * @author Zhou-Jingxian
 */
public class Bean {

	private String goods_name ;
	private Map<String,Double> priceindexMap;
	
	public String getGoods_name() {
		return goods_name;
	}
	public void setGoods_name(String goods_name) {
		this.goods_name = goods_name;
	}

	public Map<String, Double> getPriceindexMap() {
		return priceindexMap;
	}
	public void setPriceindexMap(Map<String, Double> priceindexMap) {
		this.priceindexMap = priceindexMap;
	}
	
	
}

 

package com.potevio.rnd.tobacco.mine;

import java.awt.Color;
import java.awt.Font;
import java.awt.GradientPaint;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardXYItemLabelGenerator;
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;
import org.jfree.ui.TextAnchor;

/**
 * @description 使用JFreeChart组建,生成一个价格随日期的走势图表
 * @author Zhou-Jingxian
 */
public class TimeSeriesChartUtil { 
	
	private String type;//month,year
	private int width ;//后台计算
	private int height;//后台计算
	private String title;//图表的主标题
	private String subTitle;//图表的子标题
	private String xName;//可默认值:月份
	private String yName;//可默认值:价格指数
	
	/***
	 * constructor function
	 * @param type
	 * @param title
	 * @param subTitle
	 * @param xName
	 * @param yName
	 */
	public TimeSeriesChartUtil(String type,String title,String subTitle,String xName,String yName){
		this.type = type;
		this.title = title;
		this.subTitle = subTitle;
		this.xName = xName;
		this.yName = yName;
		if("month".equals(type)){
			this.width = 800;
			this.height = 600;
		}else if("year".equals(type)){
			this.width = 600;
			this.height = 400;
		}
	}
	
	/** 根据TimeSeriesCollection创建JFreeChart对象*/   
    public JFreeChart createChart(TimeSeriesCollection dataset) {   
    	
    	JFreeChart chart = ChartFactory.createTimeSeriesChart(this.title, this.xName,this.yName, dataset, true, true, true);
    	
		XYPlot plot = (XYPlot) chart.getPlot();
		XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
		//设置网格背景颜色
		plot.setBackgroundPaint(Color.white);
		//设置网格竖线颜色
		plot.setDomainGridlinePaint(Color.pink);
		//设置网格横线颜色
		plot.setRangeGridlinePaint(Color.pink);
		//设置曲线图与xy轴的距离
		plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
		//设置曲线是否显示数据点
		xylineandshaperenderer.setBaseShapesVisible(true);
		//设置曲线显示各数据点的值
		XYItemRenderer xyitem = plot.getRenderer();   
		xyitem.setBaseItemLabelsVisible(true);   
		xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
		xyitem.setBaseItemLabelFont(new Font("Dialog", 1, 14));
		plot.setRenderer(xyitem);
		
		//设置子标题
		TextTitle subtitle = new TextTitle(this.subTitle, new Font("黑体", Font.BOLD, 12));
		chart.addSubtitle(subtitle);
		//设置主标题
		chart.setTitle(new TextTitle(this.title, new Font("隶书", Font.ITALIC, 15)));
		//设置背景颜色
		chart.setBackgroundPaint(new GradientPaint(0, 0, Color.white, 0, 1000,Color.blue));
		chart.setAntiAlias(true);
  
        return chart;   
    }   
  
    /**创建TimeSeriesCollection对象  */
    public TimeSeriesCollection createDataset(List<Bean> datalist){
    	
    	//时间曲线数据集合
		TimeSeriesCollection lineDataset = new TimeSeriesCollection();
		for(int i=0;i<datalist.size();i++){
        	Bean bean = datalist.get(i);
        	TimeSeries series = new TimeSeries(bean.getGoods_name(),Month.class);
    		Map<String,Double> pimap = bean.getPriceindexMap(); 
    		Set piset = pimap.entrySet();   
    		Iterator piIterator = piset.iterator();   
    		while(piIterator.hasNext()){   
    		    Map.Entry<String,Double> hiddenMapEntry = (Map.Entry<String,Double>)piIterator.next();  
    		    String key = hiddenMapEntry.getKey();   
    		    int year = Integer.parseInt(key.substring(0,4));
    		    int month = Integer.parseInt(key.substring(4, 6));
    		    series.add(new Month(month,year),hiddenMapEntry.getValue());
    		}  
        	lineDataset.addSeries(series);
        }
		return lineDataset;
    }
   	
	/**保存为文件*/   
    public void saveAsFile(JFreeChart chart, String outputPath) {   
        FileOutputStream out = null;   
        try {   
            File outFile = new File(outputPath);   
            if (!outFile.getParentFile().exists()) {   
                outFile.getParentFile().mkdirs();   
            }   
            out = new FileOutputStream(outputPath);   
            // 保存为PNG   
            ChartUtilities.writeChartAsPNG(out, chart, width, height);   
            // 保存为JPEG   
            // ChartUtilities.writeChartAsJPEG(out, chart, width, height);   
            out.flush();   
        } catch (FileNotFoundException e) {   
            e.printStackTrace();   
        } catch (IOException e) {   
            e.printStackTrace();   
        } finally {   
            if (out != null) {   
                try {   
                    out.close();   
                } catch (IOException e) {   
                    // do nothing   
                }   
            }   
        }   
    }   
  
	public int getWidth() {
		return width;
	}

	public void setWidth(int width) {
		this.width = width;
	}

	public int getHeight() {
		return height;
	}

	public void setHeight(int height) {
		this.height = height;
	}   
	
	public String getXName() {
		return xName;
	}

	public void setXName(String name) {
		xName = name;
	}

	public String getYName() {
		return yName;
	}

	public void setYName(String name) {
		yName = name;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}


	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getSubTitle() {
		return subTitle;
	}

	public void setSubTitle(String subTitle) {
		this.subTitle = subTitle;
	}

}

 

package com.potevio.rnd.tobacco.mine;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jfree.chart.JFreeChart;
import org.jfree.data.time.TimeSeriesCollection;


/**
 * @description 构造数据,测试图片生成
 * @author Zhou-Jingxian
 */
public class Main {
	  
    public static void main(String[] args) {
    	
    	TimeSeriesChartUtil util = new TimeSeriesChartUtil("year", "重点品牌价格走势图", "2009年8-10月走势图", "时间", "价格指数");
    	List<Bean> datalist = new ArrayList<Bean>();
    	
    	Bean bean1 = new Bean();
    	bean1.setGoods_name("中华");
    	Map<String,Double> priceindexMap1 = new HashMap<String,Double>();
    	priceindexMap1.put("200903", 99.86);
    	priceindexMap1.put("200904", 99.8);
    	priceindexMap1.put("200905", 99.97);
    	priceindexMap1.put("200906", 99.96);
    	priceindexMap1.put("200907", 99.86);
    	priceindexMap1.put("200908", 99.8);
    	priceindexMap1.put("200909", 99.97);
    	priceindexMap1.put("200910", 99.96);
    	bean1.setPriceindexMap(priceindexMap1);
    	datalist.add(bean1);
    	
    	Bean bean2 = new Bean();
    	bean2.setGoods_name("芙蓉王");
    	Map<String,Double> priceindexMap2 = new HashMap<String,Double>();
    	priceindexMap2.put("200903", 100.12);
    	priceindexMap2.put("200904", 100.2);
    	priceindexMap2.put("200905", 100.0);
    	priceindexMap2.put("200906", 100.08);
    	priceindexMap2.put("200907", 100.12);
    	priceindexMap2.put("200908", 100.2);
    	priceindexMap2.put("200909", 100.0);
    	priceindexMap2.put("200910", 100.08);
    	bean2.setPriceindexMap(priceindexMap2);
    	datalist.add(bean2);
    	
    	Bean bean3 = new Bean();
    	bean3.setGoods_name("云烟");
    	Map<String,Double> priceindexMap3 = new HashMap<String,Double>();
    	priceindexMap3.put("200903", 99.77);
    	priceindexMap3.put("200904", 99.7);
    	priceindexMap3.put("200905", 99.83);
    	priceindexMap3.put("200906", 99.93);
    	priceindexMap3.put("200907", 99.77);
    	priceindexMap3.put("200908", 99.7);
    	priceindexMap3.put("200909", 99.83);
    	priceindexMap3.put("200910", 99.93);
    	bean3.setPriceindexMap(priceindexMap3);
    	datalist.add(bean3);
    	
        //步骤1:创建XYDataset对象(准备数据) 
        TimeSeriesCollection dataset = util.createDataset(datalist);
        //步骤2:根据Dataset 生成JFreeChart对象,以及做相应的设置 
        JFreeChart freeChart = util.createChart(dataset);
        
        //步骤3:将JFreeChart对象输出到文件,Servlet输出流等   
        util.saveAsFile(freeChart, "F:\\jfreechart\\lineXY_"+Math.random()*20+".png");   
    }   
  
}

 生成的效果图:



 

这是由美联储经济数据库(FRED)托管的美联储数据集。有关每个文件的更多详细信息,请参见各个文件的说明。 industrial-production-business-equipment_metadata.json industrial-production-consumer-goods_metadata.json industrial-production-durable-consumer-goods_metadata.json industrial-production-durable-goods-raw-steel_metadata.json industrial-production-durable-manufacturing-motor-vehicles-and-parts_metadata.json industrial-production-durable-materials_metadata.json industrial-production-electric-and-gas-utilities_metadata.json industrial-production-electric-and-gas-utilities_metadata_1.json industrial-production-final-products-and-nonindustrial-supplies_metadata.json industrial-production-final-products-market-group_metadata.json industrial-production-fuels_metadata.json industrial-production-manufacturing-naics_metadata.json industrial-production-manufacturing-naics_metadata_1.json industrial-production-manufacturing-sic_metadata.json industrial-production-materials_metadata.json industrial-production-mining_metadata.json industrial-production-mining-crude-oil_metadata.json industrial-production-nondurable-consumer-goods_metadata.json industrial-production-nondurable-materials_metadata.json industrial-production-residential-utilities_metadata.json industrial-production-total-index_metadata.json IPB50001N.csv IPB51222S.csv IPBUSEQ.csv IPCONGD.csv IPDCONGD.csv IPDMAT.csv IPFINAL.csv IPFPNSS.csv IPFUELS.csv IPG2211A2N.csv IPG3361T3S.csv IPG211111CN.csv IPGMFN.csv IPMAN.csv IPMANSICS.csv IPMAT.csv IPMINE.csv IPN3311A2RN.csv IPNCONGD.csv IPNMAT.csv IPUTIL.csv
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值