当前位置: 代码迷 >> Web前端 >> jfreechart 柱状图示范- 3D效果
  详细解决方案

jfreechart 柱状图示范- 3D效果

热度:848   发布时间:2012-07-24 17:47:58.0
jfreechart 柱状图示例- 3D效果
/**
   *
   *<p>Title: geCustomeCurrentFee</p>
   *<p>Description:TODO 得到当前话费</p>
   * @param @param mobileTeleNo
   * @param @return 设定文件
   * @return String 返回类型
   * @throws
   */
  public  String getTopWorkSummaryData(){
  String mobileTeleNo=HttpUtil.getParameter("mobileTeleNo");
   HttpServletResponse response= ServletActionContext.getResponse();
   HttpServletRequest request = ServletActionContext.getRequest();
   HttpSession session = request.getSession();
    response.setContentType("text/html; charset=GBK");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
  try{
   if(LoginUtil.getUserInfo()==null){
                logger.debug("Session已失效!");
                PrintWriter writer= response.getWriter();
                writer.print("Session已失效,请重新登陆!");
                response.flushBuffer();
                return null;
            }
     try{
   session.removeAttribute("JFreeChart_Deleter");
   JFreeChart chart = createTopWorkSummaryDataChart(mobileTeleNo);   
  String filename = ServletUtilities.saveChartAsPNG(chart, 265, 180, null, session);//宽度,长度
  String graphURL = graphURL = request.getContextPath()+ "/servlet/DisplayChart?filename=" + filename;
  PrintWriter writer= response.getWriter();
            writer.print(graphURL);
            response.flushBuffer();
} catch (IOException e1) {
       logger.error(e1.getMessage());
return "";
}
     }catch(Exception e){
            try {
            PrintWriter writer= response.getWriter();
            writer.print("保存数据失败!");
response.flushBuffer();
return null;
} catch (IOException e1) {
logger.error(e.getMessage());
return "";
}
          
}
     return null;  
   }
 
   private JFreeChart createTopWorkSummaryDataChart(String mobileTeleNo) throws Exception{
JFreeChart chart = null;
DefaultCategoryDataset dataset = this.createTopWorkSummaryData(mobileTeleNo);
if(dataset != null){
    chart = ChartFactory.createBarChart3D("", "", "", dataset, PlotOrientation.VERTICAL,  true, false, false); 
chart.setBackgroundPaint(Color.WHITE); 
chart.setBackgroundImageAlpha(0.9f);
CategoryPlot plot = chart.getCategoryPlot(); 


/*
ValueAxis   rangeAxis   =   plot.getRangeAxis();
//设置最高的一个   Item   与图片顶端的距离
rangeAxis.setUpperMargin(0.35);
//设置最低的一个   Item   与图片底端的距离
rangeAxis.setLowerMargin(0.45);
//设置Y轴的最小值
//rangeAxis.setLowerBound(0);
//设置Y轴的最大值
// rangeAxis.setUpperBound(800); */
CategoryAxis domainAxis = plot.getDomainAxis(); 
domainAxis.setAxisLineVisible(false); 
NumberAxis numberaxis = (NumberAxis) plot.getRangeAxis();
TextTitle textTitle = chart.getTitle();
textTitle.setFont(new Font("黑体", Font.PLAIN, 20));
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11));
domainAxis.setLabelFont(new Font("宋体", Font.PLAIN, 12));
numberaxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12));
numberaxis.setLabelFont(new Font("黑体", Font.PLAIN, 12));
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
plot.setDomainAxis(domainAxis); 
BarRenderer3D renderer = new BarRenderer3D();

renderer.setBaseOutlinePaint(Color.WHITE);
// 显示每个柱的数值,并修改该数值的字体属性 
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
// 设置每个地区所包含的平行柱的之间距离 
renderer.setItemMargin(0.1); 
// 设置柱的数值可见
renderer.setBaseItemLabelsVisible(true);
// 显示每个柱的数值,并修改该数值的字体属性 
   renderer.setItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
renderer.setItemLabelsVisible(true); 
renderer.setShadowVisible(false);
plot.setRenderer(renderer); 
// 设置柱的透明度 
plot.setForegroundAlpha(0.8f); 
}
return chart;
}

  private  DefaultCategoryDataset createTopWorkSummaryData(String mobileTeleNo) throws Exception {
    DefaultCategoryDataset dataset = new DefaultCategoryDataset();
    InitiaWorkbenchManager workbenchManager=(InitiaWorkbenchManager)DefaultBeanFactory.getBean(InitiaWorkbenchManager.BEAN_NAME);
String[] results=new String[]{"AGREE"};
List<AgentWorkCountBean>  gentWorkCountList=workbenchManager.queryTopWorkSummary(results,5);
if(!gentWorkCountList.isEmpty()&&gentWorkCountList.size()>0){
for(int i=0;i<gentWorkCountList.size();i++){
AgentWorkCountBean bean=gentWorkCountList.get(i);
String userCode=gentWorkCountList.get(i).getUserCode();
String userName=userCode+"["+UserCacheUtil.getUserName(userCode)+("]");
int agreeCount=gentWorkCountList.get(i).getAgreeCount();
   
         dataset.addValue(agreeCount, userName, userCode);
    }
    
}
return dataset;
  }