JFreeChat学习----曲线图的创建

 备注:导包

 1 import java.awt.Color;
 2 import java.awt.Font;
 3 import java.io.IOException;
 4 
 5 import javax.servlet.ServletException;
 6 import javax.servlet.ServletRequest;
 7 import javax.servlet.ServletResponse;
 8 import javax.servlet.http.HttpServlet;
 9 
10 import org.jfree.chart.ChartFactory;
11 import org.jfree.chart.ChartUtilities;
12 import org.jfree.chart.JFreeChart;
13 import org.jfree.chart.labels.ItemLabelAnchor;
14 import org.jfree.chart.labels.ItemLabelPosition;
15 import org.jfree.chart.labels.StandardXYItemLabelGenerator;
16 import org.jfree.chart.plot.XYPlot;
17 import org.jfree.chart.renderer.xy.XYItemRenderer;
18 import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
19 import org.jfree.chart.title.LegendTitle;
20 import org.jfree.chart.title.TextTitle;
21 import org.jfree.data.time.Month;
22 import org.jfree.data.time.TimeSeries;
23 import org.jfree.data.time.TimeSeriesCollection;
24 import org.jfree.ui.RectangleInsets;
25 import org.jfree.ui.TextAnchor;

 

第一步:一样准备数据:

public static TimeSeries getTimeSeriesBy2013(){
  //访问量统计时间线:
    TimeSeries timeSeries = new TimeSeries("2013年度",Month.class);
    //构造数据集合
    timeSeries.add(new Month(1,2013),11200);
    timeSeries.add(new Month(2, 2013), 9000);
    timeSeries.add(new Month(3, 2013), 6200);
    timeSeries.add(new Month(4, 2013), 8200);
    timeSeries.add(new Month(5, 2013), 8200);
    timeSeries.add(new Month(6, 2013), 12200);
    timeSeries.add(new Month(7, 2013), 13200);
    timeSeries.add(new Month(8, 2013), 8300);
    timeSeries.add(new Month(9, 2013), 12400);
    timeSeries.add(new Month(10, 2013), 12500);
    timeSeries.add(new Month(11, 2013), 13600);
    timeSeries.add(new Month(12, 2013), 12500);
    return timeSeries;
   }
   
   public static TimeSeries getTimeSeriesBy2012(){
    TimeSeries timeSeries = new TimeSeries("2012年度",Month.class);
    timeSeries.add(new Month(1, 2013), 7200);
    timeSeries.add(new Month(2, 2013), 7000);
    timeSeries.add(new Month(3, 2013), 4200);
    timeSeries.add(new Month(4, 2013), 8200);
    timeSeries.add(new Month(5, 2013), 7300);
    timeSeries.add(new Month(6, 2013), 8200);
    timeSeries.add(new Month(7, 2013), 9200);
    timeSeries.add(new Month(8, 2013), 7300);
    timeSeries.add(new Month(9, 2013), 9400);
    timeSeries.add(new Month(10, 2013), 7500);
    timeSeries.add(new Month(11, 2013), 6600);
    timeSeries.add(new Month(12, 2013), 3500);
    return timeSeries;
   }

 
第二步:构造曲线数据集合

1 //访问量统计时间线:(两个季度)
2     TimeSeries timeSeries2013 = this.getTimeSeriesBy2013();
3     TimeSeries timeSeries2012 = this.getTimeSeriesBy2012();
4     
5     //时间曲线数据集合
6     TimeSeriesCollection lineDataset = new TimeSeriesCollection();
7     //把上面的时间线添加到数据集合之中
8     lineDataset.addSeries(timeSeries2012);
9     lineDataset.addSeries(timeSeries2013);

第三步 开始画线

1 //开始进行画线
2     JFreeChart chart = ChartFactory.createTimeSeriesChart(
3       "访问量统计时间线", 
4       "月份", 
5       "访问量", 
6       lineDataset, 
7       true, true, true)

第四步 设置样式,同时解决 口口 问题

4.1 X,Y轴标题 口口问题

1  XYPlot plot = chart.getXYPlot();
2     Font fn = new Font("黑体", Font.BOLD, 12);
3     plot.getDomainAxis().setLabelFont(fn);
4     plot.getRangeAxis().setLabelFont(fn);

4.2 样式设计

 1 //设置网格背景颜色
 2     plot.setBackgroundPaint(Color.white);
 3 
 4 
 5     //设置网格竖线颜色
 6     plot.setDomainGridlinePaint(Color.pink);
 7 
 8 
 9     //设置网格横线颜色
10     plot.setRangeGridlinePaint(Color.pink);
11 
12 
13     //设置曲线图与 xy 轴的距离
14     plot.setAxisOffset(new RectangleInsets(0D, 0D, 0D, 10D));
15 
16 
17     //设置曲线是否显示数据点
18     XYLineAndShapeRenderer xylineandshaperenderer = (XYLineAndShapeRenderer)plot.getRenderer();
19     xylineandshaperenderer.setBaseShapesVisible(true);
20 
21 
22     //设置曲线显示各数据点的值
23     XYItemRenderer xyitem = plot.getRenderer();
24     xyitem.setBaseItemLabelsVisible(true);
25     xyitem.setBasePositiveItemLabelPosition(new ItemLabelPosition
26       (ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
27     xyitem.setBaseItemLabelGenerator(new StandardXYItemLabelGenerator());
28     xyitem.setBaseItemLabelFont(new Font("黑体", 1, 14));
29     plot.setRenderer(xyitem);
30 
31 
32 
33  //设置子标题
34     TextTitle subtitle = new TextTitle("2013 / 2012年度", new Font("黑体", Font.BOLD, 12));
35     chart.addSubtitle(subtitle);
36 
37 
38     //设置主标题
39     chart.setTitle(new TextTitle("blog 访问量统计", new Font("隶书", Font.ITALIC, 15)));
40     chart.setAntiAlias(true);
41     
42   // 为图例说明设置字体
43     Font LegendFont = new Font("楷体", Font.PLAIN, 18); 
44       LegendTitle legend = chart.getLegend(0); 
45        legend.setItemFont(LegendFont);

 

最后:发送到前台JSP:

1 res.setContentType("image/jpeg");// 将输出设置为image/jepg格式
2 ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart, 
3                  800, 450, null);// 输出图表 

上图:

 

posted @ 2013-06-02 04:30  流花飘原  阅读(368)  评论(4编辑  收藏  举报