jfreechart 柱状图 开发实例

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;
}}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值