JFreeChart中的折线图

在Servlet中的代码:

package com.lyb.pic;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Font;
import java.io.IOException;
import java.util.Date;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
importorg.jfree.chart.renderer.category.LineAndShapeRenderer;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.HorizontalAlignment;
import org.jfree.ui.RectangleEdge;





public class Line extends HttpServlet {

public void doGet(HttpServletRequestrequest,HttpServletResponse response) throws IOException {
  doPost(request,response);
}
public void doPost(HttpServletRequestrequest,HttpServletResponse response) throws IOException {
  //返回类型为图片类型
  response.setContentType("image/png");
  //设置返回字体
  response.setCharacterEncoding("utf-8");
  //获取数据对象
  CategoryDataset cd =getData();
  //创建图形对象
  JFreeChart jfc =ChartFactory.createLineChart("书籍组成变化", null, "组成量", cd,PlotOrientation.VERTICAL, true, true, false);
  //设置图例的字体显示,防止中文乱码
  jfc.getLegend().setItemFont(newFont("黑体", 0, 10));
  //设置标题并且设置其字体,防止中文乱码
  TextTitle textTitle = newTextTitle("书籍组成变化");
  textTitle.setFont(newFont("宋体", Font.BOLD, 12));
  jfc.setTitle(textTitle);
  //设置图表子标题
  jfc.addSubtitle(newTextTitle("按月份"));
  //创建一个标题对象,用于放置产生图形日前
  TextTitle tt = newTextTitle("日期:"+new Date());
  //设置该标题的字体,防止中文乱码
  tt.setFont(new Font("黑体", 0,10));
  //设置该标题的位置为产生的图形下面
  tt.setPosition(RectangleEdge.BOTTOM);
  //设置图片为右对齐
  tt.setHorizontalAlignment(HorizontalAlignment.RIGHT);
  //将该标题添加到图表
  jfc.addSubtitle(tt);
  //设置整个图表的背景色为绿色
  jfc.setBackgroundPaint(Color.green);
  //获取图表区域对象
  CategoryPlot cp =jfc.getCategoryPlot();
  //设置图表区域背景色
  cp.setBackgroundPaint(Color.white);
  //设置边线不可见
  cp.setRangeGridlinesVisible(false);
       //设置横轴的字体,防止中文乱码
       cp.getDomainAxis().setTickLabelFont(new Font("黑体", 0, 10));
       //设置竖轴的字体,防止中文乱码
       cp.getRangeAxis().setLabelFont(new Font("黑体", 0, 10));
  //获取显示线条的对象
  LineAndShapeRenderer lasp =(LineAndShapeRenderer)cp.getRenderer();
  //设置拐点是否可见/是否显示拐点
  lasp.setBaseShapesVisible(true);
  //设置拐点不同用不同的形状
  lasp.setDrawOutlines(true);
  //设置线条是否被显示填充颜色
  lasp.setUseFillPaint(true);
  //设置拐点颜色
  lasp.setBaseFillPaint(Color.yellow);
  // 设置折线加粗
  lasp.setSeriesStroke(0, newBasicStroke(3F));
  lasp.setSeriesOutlineStroke(0,new BasicStroke(2.0F));
  // 设置折线拐点
  lasp.setSeriesShape(0,newjava.awt.geom.Ellipse2D.Double(-5D, -5D, 10D, 10D));
 
  // 将图表已数据流的方式返回给客户端
  ChartUtilities.writeChartAsPNG(response.getOutputStream(),jfc,500, 270);
}

private static CategoryDataset getData() {
  DefaultCategoryDataset dcd =new DefaultCategoryDataset();
  for(int i = 1; i<= 12; i++) {
   dcd.addValue(DataUtils.getNumber(),"c++", i+"月");
   dcd.addValue(DataUtils.getNumber(),"java", i+"月");
   dcd.addValue(DataUtils.getNumber(),"动画", i+"月");
  }
  return dcd;
}
}

产生的假数据:

package com.lyb.pic;

import java.util.Random;

public class DataUtils {

private static Random random = newRandom();
private static final int NUMBER = 100;
public static int getNumber() {
  returnrandom.nextInt(NUMBER);
}
}

在xml中的代码:

<servlet>
   <servlet-name>line</servlet-name>
   <servlet-class>com.lyb.pic.Line</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>line</servlet-name>
   <url-pattern>/line</url-pattern>
</servlet-mapping>

你可能感兴趣的:(jfreechart)