JFreeChart中的块状图

在Servlet中的代码:

package com.lyb.pic;

import java.awt.Color;
import java.awt.Font;
import java.io.IOException;

importjavax.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.axis.CategoryLabelPositions;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.RectangleInsets;

public class Area extendsHttpServlet{

public voiddoGet(HttpServletRequest request,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.createAreaChart("我的书籍", "组成", "比例", cd,PlotOrientation.VERTICAL, true, true, false);
  //设置标题字体,防止中文乱码
  jfc.getTitle().setFont(newFont("黑体", 0, 15));
  //设置图例字体,防止中文乱码
  jfc.getLegend().setItemFont(newFont("黑体", 0, 10));
  //获取图表区域对象
  CategoryPlot cp =jfc.getCategoryPlot();
  //设置前景色透明度
  cp.setForegroundAlpha(0.8F);
  //设置X轴偏移量
  cp.setAxisOffset(newRectangleInsets(5D, 5D, 5D, 5D));
  //设置背景色为浅灰色
  cp.setBackgroundPaint(Color.lightGray);
  //设置显示网格线
  cp.setDomainGridlinesVisible(true);
  //设置网格线为白色
  cp.setDomainGridlinePaint(Color.white);
  //设置显示边界线
  cp.setRangeGridlinesVisible(true);
  //设置显示边界线为白色
  cp.setRangeGridlinePaint(Color.white);
  //获得X轴对象
  CategoryAxis categoryaxis =cp.getDomainAxis();
  //设置标签的字体,防止中文乱码
  categoryaxis.setLabelFont(newFont("黑体", 0, 10));
  //设置图例上方的标签字体
  categoryaxis.setTickLabelFont(newFont("黑体", 0, 10));
  //设置X轴标签位置为45度角
  categoryaxis.setCategoryLabelPositions(CategoryLabelPositions.UP_45);
  //设置与X轴的补白为0
  categoryaxis.setLowerMargin(0.0D);
  categoryaxis.setUpperMargin(0.0D);
  //设置竖轴的标签字体
  cp.getRangeAxis().setLabelFont(newFont("黑体", 0, 10));

  //将图表已数据流的方式返回给客户端
  ChartUtilities.writeChartAsPNG(response.getOutputStream(),jfc,
    500,270);
}

private static CategoryDataset getData() {
  //时间维度,用于在x轴显示
  String [] c1 ={"第一季度","第二季度","第三季度","第四季度"};
  //分类的维度,由数量表示,在y轴显示
  String [] c2 ={"java","c","c++","动画"};
  //创建数据对象
  DefaultCategoryDataset dcd =new DefaultCategoryDataset();
  //通过双重for循环取出值,然后添加进dpd中
  for(int i = 0;i< c1.length;i++) {
   String c3 =c1[i];
   for(int j =0;j < c2.length;j++) {
    Stringc4 = c2[j];
    dcd.setValue(DataUtils.getNumber(),c4, c3);
   }
  }
  return dcd;
}
}



产生的假数据:

package com.lovo.pic;

import java.util.Random;

public class DataUtils {

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



在xml中的代码:

<servlet>
   <servlet-name>area</servlet-name>
   <servlet-class>com.lyb.pic.Area</servlet-class>
</servlet>
<servlet-mapping>
   <servlet-name>area</servlet-name>
   <url-pattern>/area</url-pattern>
</servlet-mapping>

你可能感兴趣的:(java)