jfreechart 柱状图 开发实例

来源:互联网 发布:景茗网络是啥 编辑:程序博客网 时间:2024/05/01 12:51

package cn.report.jfreechart;

 

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

import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;

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.StandardCategoryItemLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

import cn.report.excel.Production;

 

public class BarChartServlet extends HttpServlet {
 
      String  equipment_id=null;
  
  public void service(ServletRequest req, ServletResponse res)
     throws ServletException, IOException   
 {
      res.setContentType("image/jpeg");
     equipment_id=req.getParameter("nodeId");
        System.out.print(equipment_id);
        DefaultCategoryDataset dcd=createDataset();
      
        JFreeChart chart = ChartFactory.createBarChart3D("不良品统计图",
                   "类型",
                   "数量",
                   dcd ,
                   PlotOrientation.VERTICAL,
                   true,
                   true,
                   false);
        /**  chart.setBackgroundPaint(Color.WHITE);  
         CategoryPlot plot=chart.getCategoryPlot();  
         plot.setBackgroundPaint(Color.GRAY);  
         //设置横轴  
         CategoryAxis categoryAxis=plot.getDomainAxis();  
         //设置标签之间的距离是20%  
         categoryAxis.setCategoryMargin(0.2);  
         categoryAxis.setLowerMargin(0.1);  
         categoryAxis.setUpperMargin(0.1);  
         //设置纵轴  
         ValueAxis valueAxis=plot.getRangeAxis();  
         valueAxis.setLowerMargin(0.1);  
         valueAxis.setUpperMargin(0.1);  
           
         BarRenderer3D render=new BarRenderer3D();  
         render.setSeriesPaint(0,Color.RED);  
         render.setSeriesPaint(1,Color.GREEN);         
         plot.setRenderer(render);  
         plot.setDomainAxisLocation(AxisLocation.BOTTOM_OR_LEFT);  
         plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_LEFT);  
*/
       //设置标题字体
    //chart.setTitle(new TextTitle(chart.getTitle().getText(),titleFont));
    //设置绘图属性
       //设置标题字体
     Font xfont = new Font("黑体",Font.PLAIN,12) ;// X 轴
  Font yfont = new Font("黑体",Font.PLAIN,12) ;// Y 轴
  Font kfont = new Font("宋体",Font.BOLD,12) ;// 底部
  Font titleFont = new Font("隶书", Font.BOLD , 25) ; // 图片标题
   chart.setTitle(new
   TextTitle(chart.getTitle().getText(),titleFont));
   //设置绘图属性
   CategoryPlot plot = chart.getCategoryPlot() ;
   plot.setBackgroundPaint(Color.white) ; //网格背景为白色
   plot.setDomainGridlinePaint(Color.pink) ; //网格线的竖线
   plot.setRangeGridlinePaint(Color.pink) ; //网格线的横线
   //设置每个柱子上显示各自的数值
   BarRenderer3D renderer = new BarRenderer3D() ;
   renderer.setBaseItemLabelGenerator(new
   StandardCategoryItemLabelGenerator()) ;
   renderer.setBaseItemLabelsVisible(true) ;
   renderer.setBasePositiveItemLabelPosition(new
   ItemLabelPosition(ItemLabelAnchor.OUTSIDE12,
   TextAnchor.BASELINE_LEFT)) ;
   renderer.setItemLabelAnchorOffset(10D) ;
   plot.setRenderer(renderer) ;
          ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart,
       800, 400, null);
 
 }
 
 public  DefaultCategoryDataset createDataset() {     
  List <Production> productions=StateReportManger.getIntense().getZhuClientLevelCount(equipment_id);
  DefaultCategoryDataset dcd = new DefaultCategoryDataset();//数据源
  for(Production production : productions) {
   dcd.setValue(production.getGuiluo(),"硅落",production.getDate1());
   dcd.setValue(production.getLiangbian(),"亮边",production.getDate1());
   dcd.setValue(production.getXianheng(),"线痕",production.getDate1());
   dcd.setValue(production.getTtv(),"ttv",production.getDate1());
   dcd.setValue(production.getBengbian(),"崩边",production.getDate1());
   //dcd.setValue(production.getXianheng(),"线痕",production.getDate1());
     //dcd.setValue(production.getGuiluo(),production.getDate1(),"硅落");
  }
 
     return dcd;
}}

原创粉丝点击