package charts;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import javax.servlet.http.HttpSession;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.DateAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.servlet.ServletUtilities;
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.data.xy.XYDataset;
import org.jfree.ui.RectangleInsets;
public class LineChart
{
	  /**
     * 构造报表的结果集
     * @return
     */
    private static XYDataset createDataset() {
        TimeSeries s1 = new TimeSeries("非金融", Month.class);
        s1.add(new Month(12, 2005), 0.09);
        s1.add(new Month(3, 2006), 0.10);
        s1.add(new Month(6, 2006), 0.14);
        s1.add(new Month(9, 2006), 0.13);
        s1.add(new Month(12, 2006), 0.125);
        s1.add(new Month(3, 2007), 0.124);
        s1.add(new Month(6, 2007), 0.123);
        s1.add(new Month(9, 2007), 0.122);

        TimeSeries s2 = new TimeSeries("居民", Month.class);
        s2.add(new Month(12, 2005), 0.13);
        s2.add(new Month(3, 2006), 0.14);
        s2.add(new Month(6, 2006), 0.15);
        s2.add(new Month(9, 2006), 0.175);
        s2.add(new Month(12, 2006), 0.19);
        s2.add(new Month(3, 2007), 0.22);
        s2.add(new Month(6, 2007), 0.24);
        s2.add(new Month(9, 2007), 0.31);

        TimeSeriesCollection dataset = new TimeSeriesCollection();
        dataset.addSeries(s1);
        dataset.addSeries(s2);

        return dataset;
    }
    /**
     * 根据结果集构造JFreechart报表对象
     * @param xyDataSet
     * @return
     */
    private static JFreeChart createChart(XYDataset dataset)
    {
        JFreeChart jfreechart = ChartFactory.createTimeSeriesChart(
                "居民和非金融类贷款年度同比增长率",          //报表标题
                "月份",                                 //x轴标签 
                "增长率",                               //y轴标签 
                dataset,                               //数据集 
                true,                                  //是否显示图例
                true,                                  //是否有工具条提示
                false                                  //是否有链接
                );

        jfreechart.setBackgroundPaint(Color.white);
        jfreechart.addSubtitle(new TextTitle("------《2008年银行业行业报告》",
                new Font("Dialog", Font.ITALIC, 10)));
        
        XYPlot plot = (XYPlot) jfreechart.getPlot();
        plot.setAxisOffset(new RectangleInsets(5.0, 5.0, 5.0, 5.0));

        XYItemRenderer r = plot.getRenderer();
        XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) r;
        renderer.setBaseShapesVisible(true);

        DateAxis axis = (DateAxis) plot.getDomainAxis();
        axis.setDateFormatOverride(new SimpleDateFormat("yyyy-MMM"));
        axis.setVerticalTickLabels(true);
        
        NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis();
        rangeAxis.setNumberFormatOverride(new DecimalFormat("0.0%"));
        
        


        return jfreechart;
    }
    /**
     * 生成折线图的图片,返回图片文件的名称
     * 
     * @param session
     * @param pw
     * @return
     */
    public static String generateLineChart(HttpSession session, PrintWriter pw)
    {
        String filename = null;
        XYDataset xyDataset = createDataset();
        JFreeChart chart = createChart(xyDataset);
        ChartRenderingInfo info = new ChartRenderingInfo(
                new StandardEntityCollection());
        try
        {
            filename = ServletUtilities.saveChartAsPNG(chart, 600, 350, info,
                    session);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        try
        {
            ChartUtilities.writeImageMap(pw, filename, info, false);
        }
        catch (IOException e)
        {
            e.printStackTrace();
        }
        pw.flush();
        return filename;
    }
}
最近下载更多
wusiyin  LV14 2022年9月15日
微信网友_5957378031800320  LV3 2022年5月18日
xcj456  LV8 2020年9月12日
15838634741  LV18 2020年7月3日
peterLi  LV3 2019年12月30日
zjjhzjb  LV14 2019年12月7日
wl2301821  LV7 2019年11月19日
906396135  LV7 2019年10月31日
wanglong_wang  LV13 2019年8月1日
丷夏空丷  LV4 2019年5月19日
最近浏览更多
fesfefe  LV13 2023年11月1日
pnews88  LV8 2023年6月30日
abc562311934  LV4 2022年10月8日
wusiyin  LV14 2022年9月15日
杠上炮  LV6 2022年6月28日
微信网友_5957378031800320  LV3 2022年5月18日
huzxiong 2022年2月24日
暂无贡献等级
wxh1234567  LV4 2022年1月17日
nickshen111  LV8 2021年12月13日
James_hong  LV1 2021年10月14日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友