jfreechart饼图和柱状图 ajax

/**
 *
 */
package com.ccit.csw.queryfee.action;

import java.awt.Color;
import java.awt.Font;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.CategoryAxis;
import org.jfree.chart.axis.CategoryLabelPositions;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.labels.ItemLabelAnchor;
import org.jfree.chart.labels.ItemLabelPosition;
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.CategoryPlot;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.renderer.category.BarRenderer3D;
import org.jfree.chart.servlet.ServletUtilities;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.ui.TextAnchor;

import com.ccit.csw.common.action.base.BaseAction;
import com.ccit.csw.common.exception.AppCodeException;
import com.ccit.csw.common.exception.BaseException;
import com.ccit.csw.common.log.Logger;
import com.ccit.csw.common.outinterface.impl.MyNode;
import com.ccit.csw.common.service.ErrorMessageService;
import com.ccit.csw.common.util.BusinessUtil;
import com.ccit.csw.common.util.CommonConstant;
import com.ccit.csw.common.util.Util;
import com.ccit.csw.common.vo.SessionVo;
import com.ccit.csw.points.vo.PointsBalanceVo;
import com.ccit.csw.queryfee.service.QueryfeeCommonService;
import com.ccit.csw.queryfee.util.QueryfeeServiceUtil;
import com.ccit.csw.queryfee.vo.BalanceVo;
import com.ccit.csw.queryfee.vo.BlancePayQrVo;
import com.ccit.csw.queryfee.vo.CustBillIQrVo;
import com.ccit.csw.queryfee.vo.CustBillInfoVo;
import com.ccit.csw.queryfee.vo.NewBillSvrVo;
import com.ccit.csw.queryfee.vo.ProductBillVo;
import com.opensymphony.xwork2.ActionContext;

/**
 * @author 陳立志 E-Mail:chenlizhi@ccit.com.cn
 * @version 0.1
 * @since Sep 15, 2009 10:42:04 AM
 */
public class ReckQueryNewAction extends BaseAction {
 
 private static final long serialVersionUID = 1L;

 private String productInfo;                    // 所選產品 產品類型:產品號碼的形式
 
 private String queryType;                      // 所選查詢方式 1產品級 2賬戶級
 
 private String period;                         // 賬期 20090701-20090731 的形式  //新賬期 為201006 的形式
 
// private PointsManagerService pointsManagerService; // 查詢積分餘額service 
 
 private QueryfeeCommonService queryfeeService; // 查詢餘額service
 
 private ErrorMessageService messageService;    // 查詢平台錯誤信息
  
 private CustBillIQrVo custBillQrVo;            // 賬戶級賬單返回信息
 
 private ProductBillVo productBillVo;           // 產品級賬單返回信息
 
 private BlancePayQrVo  blancePayQrVo;     //歷史累積量返回信息

 private PointsBalanceVo pointsBalanceVo;   // 接口返回的積分餘額信息
 
 private String picName;      //餅圖路徑
 
 public String execute() throws BaseException {
//         try {
  
   Logger.getLogger().info(ReckQueryAction.class, "execute()",
     "賬單查詢 用戶選擇的產品號碼:"+productInfo+" 查詢類型:"+queryType+" 賬期:"+period, null);
  
 
   // 得到SessionVo 中的信息
   SessionVo sesionVo = (SessionVo) getSession(CommonConstant.USER_INFO);
   BalanceVo balanceVo = new BalanceVo();
   //檢驗選擇產品是否在客戶的產品列表中,防止被人篡改
   if(!BusinessUtil.checkProdInList((List) ActionContext.getContext().getSession().get(
   "userNewList"),productInfo)){
    if("2".equals(queryType)){
     productBillVo= new ProductBillVo();
     balanceVo.setErrorCode(CommonConstant.NOLOGINUSER_ERRORCODE);
     balanceVo.setErrorDesc(messageService.getErrorMessageByCode(CommonConstant.NOLOGINUSER_ERRORCODE).getErrdesc());
    }else{
     custBillQrVo= new CustBillIQrVo();
     custBillQrVo.setErrorCode(CommonConstant.NOLOGINUSER_ERRORCODE);
     custBillQrVo.setErrorDesc(messageService.getErrorMessageByCode(CommonConstant.NOLOGINUSER_ERRORCODE).getErrdesc());
    }
   
   }else{
    try{
      //調用餘額查詢接口
/**     balanceVo = queryfeeService.acctBalanceQuery(sesionVo, productInfo);
     //調用累積量查詢接口
     blancePayQrVo = queryfeeService.queryPackageUserInfo(sesionVo, productInfo, queryType,period);
    }catch(Exception ex){
     Logger.getLogger().error(ReckQueryAction.class, "excute()",
       ex.getMessage(), ex);
    }
     //如果為產品級賬單,則調用產品級賬單查詢接口
     if("1".equals(queryType)){
      productBillVo =queryfeeService.productBillQuery(sesionVo,productInfo, period);
      if(productBillVo!=null && balanceVo!=null){
       productBillVo.setBalance(balanceVo.getBalanceFullFee());
      }
     }else{
      //否則調用賬戶級賬單
      custBillQrVo =queryfeeService.custBillQuery(sesionVo,productInfo, period);
      if(custBillQrVo!=null && balanceVo!=null){
       custBillQrVo.setOutBalance(balanceVo.getBalanceFullFee());
      }
     }
*/
    
      //新賬單查詢,同一更改為查詢客戶級賬單 edit by heavy 2010-08-20
     String type=Util.getProductType(productInfo);
     String number=Util.getProductNumber(productInfo);
     if(String.valueOf(CommonConstant.PHONE_TYPE).equals(type)||String.valueOf(CommonConstant.PHS_TYPE).equals(type)||String.valueOf(CommonConstant.LAN_TYPE).equals(type)){
      number=sesionVo.getOrgCode()+number;
     }
     NewBillSvrVo vo = queryfeeService.newBillSrv(sesionVo,
       number, period,queryType,type);

     Logger.getLogger().info(ReckQueryAction.class,"","查詢賬單xml:"+vo.getXml_out(),null);
     custBillQrVo=queryfeeService.getCustBillFromNewBillSvr(vo);
    
    
     //以下下用來生成餅圖 只有產品級才生成
     if("1".equals(queryType)){
      picName=createPic(custBillQrVo.getCustBillInfoList(),"2",super.getRequest().getSession());     
     }
    
     if("0".equals(queryType)){
      picName=creat3Dpic(custBillQrVo.getStatisticalList(),period,super.getRequest().getSession());
     }
    
//     if(custBillQrVo.isResultFlag()){
//         pointsBalanceVo = pointsManagerService.queryPointsBalance(sesionVo, productInfo);
//     }
    
     //Logger.getLogger().info(ReckQueryAction.class,"","積分信息:"+pointsBalanceVo.getViewList().size(),null);
  

  } catch (BaseException e) {
   Logger.getLogger().error(ReckQueryAction.class, "excute()",
     e.getMessage(), e);
   throw e;
  }
  catch (Exception ex) {
    Logger.getLogger().error(ReckQueryAction.class, "execute()",
     ex.getMessage(), ex);
   throw new AppCodeException("賬單查詢失敗", ex);
  }}
  
  return SUCCESS;
 }
 
 /**
  * 生成圖片流
  * @param list 
  * @param level  級別
  * @return
  */
 private static String createPic(List list,String level,HttpSession session){
  if(list==null||list.size()==0){
   return null;
  }
 
  DefaultPieDataset dataset = new DefaultPieDataset();
  String filename =null;
  for(int i=0;i<list.size();i++){
   CustBillInfoVo vo = (CustBillInfoVo)list.get(i);
   if(vo!=null&&level.equals(vo.getItemLevel())){
    dataset.setValue(" "+vo.getItemName(), Double.valueOf(vo.getItemCharge()).doubleValue()/100);
   }
  } 
 
  JFreeChart chart = ChartFactory.createPieChart3D(" 賬單消費分布(單位:元)", // chart
                 // title
    dataset,// data
    true,// include legend
    true, false);
  PiePlot3D plot = (PiePlot3D) chart.getPlot();
  // 圖片中顯示百分比:默認方式
  // plot.setLabelGenerator(new
  // StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
  // 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數值, {2} 表示所佔比例 ,小數點後兩位
  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}={1}({2})", NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));
  // 圖例顯示百分比:自定義方式, {0} 表示選項, {1} 表示數值, {2} 表示所佔比例
  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}={1}({2})", NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));
  // 設置背景色為白色
  chart.setBackgroundPaint(Color.white);
  // 指定圖片的透明度(0.0-1.0)
  plot.setForegroundAlpha(1.0f);
  // 指定顯示的餅圖上圓形(false)還橢圓形(true)
  plot.setCircular(true);
  // 設置圖標題的字體
  Font font = new Font(" 黑體", Font.CENTER_BASELINE, 20);
  TextTitle title = new TextTitle(" 賬單消費分布(單位:元)");
  title.setFont(font);
  chart.setTitle(title);
//  PrintWriter pw=null;
//  try {
//   pw =ServletActionContext.getResponse().getWriter();
//  }catch (IOException e) {
//   // TODO Auto-generated catch block
//   e.printStackTrace();
//  }
  try {
   filename = ServletUtilities.saveChartAsPNG(chart, 540, 280, session);
//   ChartUtilities.writeImageMap(pw, "map0", new ChartRenderingInfo(new StandardEntityCollection()), false);
  
  } catch (Exception e) {
   Logger.getLogger().error(ReckQueryAction.class, "賬單查詢生成餅圖出錯!",
     e.getMessage(), e);
  }
 
 
  return filename;
 }
 
 
 private static String creat3Dpic(List list,String period,HttpSession session){
  if(list==null||list.size()==0){
   return null;
  }
 
  period=((MyNode)QueryfeeServiceUtil.getFeeMonthList("-3").get(0)).getValue();
 
  List newList=list;
  for(int i=0;i<newList.size();i++){
   CustBillInfoVo vo = (CustBillInfoVo)list.get(i);
   if(vo!=null){
    if(vo.getItemId().indexOf("57255")<0){
     newList.remove(i);
     i--;
    }
   }
  }
 
  DefaultCategoryDataset dataset=new DefaultCategoryDataset();
  for(int j=0;j<6;j++){
     for(int i=0;i<list.size();i++){
   CustBillInfoVo vo = (CustBillInfoVo)list.get(i);
   if(vo!=null&&Integer.parseInt(vo.getItemId().substring(vo.getItemId().length()-1))==j){
      dataset.addValue(Double.valueOf(vo.getItemCharge()),"1", vo.getItemName());
      break;
   }else{
    if(i==list.size()-1){
      dataset.addValue(0,"1", getPeriod(period,j));
      break;
    }
   }
    }
  }
 
     JFreeChart chart=ChartFactory.createBarChart3D(
                "近六個月總費用支出圖",
                "月份",//目錄軸的顯示標籤
                "費用(元)",//數值軸的顯示標籤
                dataset,
                PlotOrientation.VERTICAL,//設置圖表方向
                false,
                false,
                false      
        );
      
        //設置標題
        chart.setTitle(new TextTitle("近六個月總費用支出圖",new Font("黑體",Font.CENTER_BASELINE,22)));
        chart.setBackgroundPaint(Color.white);
      
        //設置圖表部分
        CategoryPlot plot=(CategoryPlot)chart.getPlot();
        plot.setBackgroundAlpha(0.1f);
        plot.setDataAreaRatio(0.00);
        CategoryAxis categoryAxis=plot.getDomainAxis();//取得橫軸
        categoryAxis.setLabelFont(new Font("宋體",Font.BOLD,18));//設置橫軸顯示標籤的字體
        //categoryAxis.setLabelPaint(Color.decode("#F8D661"));
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);//分類標籤以45度傾斜
        categoryAxis.setTickLabelFont(new Font("宋體",Font.LAYOUT_LEFT_TO_RIGHT,10));//分類標籤字體
      
        NumberAxis numberAxis=(NumberAxis)plot.getRangeAxis();//取得縱軸
        numberAxis.setLabelFont(new Font("宋體",Font.BOLD,18));//設置縱軸顯示標籤字體
        BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
      
        customBarRenderer.setPositiveItemLabelPosition(new ItemLabelPosition(
          ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
        customBarRenderer.setMaxBarWidth(0.05);
        customBarRenderer.setItemMargin(0.000000005);

        customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//顯示每個柱的數值
        customBarRenderer.setBaseItemLabelsVisible(true);
        //注意:此句很關鍵,若無此句,那數字的顯示會被覆蓋,給人數字沒有顯示出來的問題
        customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
        ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        customBarRenderer.setItemLabelAnchorOffset(8D);// 設置柱形圖上的文字偏離值
        customBarRenderer.setItemLabelFont(new Font("宋體",Font.ITALIC,12));

        customBarRenderer.setSeriesPaint(0, Color.decode("#24F4DB")); // 給series1 Bar
//        customBarRenderer.setSeriesPaint(1, Color.decode("#7979FF")); // 給series2 Bar
//        customBarRenderer.setSeriesPaint(2, Color.decode("#FF5555")); // 給series3 Bar
//        customBarRenderer.setSeriesPaint(3, Color.decode("#F8D661")); // 給series4 Bar
//        customBarRenderer.setSeriesPaint(4, Color.decode("#F284DC")); // 給series5 Bar
//        customBarRenderer.setSeriesPaint(5, Color.decode("#00BF00")); // 給series6 Bar
      
//        customBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(1,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(2,Color.BLACK); //邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(3,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(4,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(5,Color.BLACK); //邊框為黑色
        customBarRenderer.setItemLabelsVisible(true);
       // customBarRenderer.setMinimumBarLength(0.9D);
      
//        FileOutputStream fos=null;
//        fos=new FileOutputStream("D://book1.jpg");
//        ChartUtilities.writeChartAsJPEG(
//                fos,
//                1,
//                chart,
//                540,
//                330,
//                null
//      
//        );
//        fos.close();
        String filename=null;
     try {
   filename = ServletUtilities.saveChartAsPNG(chart, 540, 280, session);
//   ChartUtilities.writeImageMap(pw, "map0", new ChartRenderingInfo(new StandardEntityCollection()), false);
  
  } catch (Exception e) {
   Logger.getLogger().error(ReckQueryAction.class, "賬單查詢生成柱狀圖出錯!",
     e.getMessage(), e);
  }
 
 
  return filename;
 }
 
 private static String getPeriod(String currPeriod,int latter){
  String result="";
  int year=Integer.valueOf(currPeriod.substring(0, 4)).intValue();
  int month=Integer.valueOf(currPeriod.substring(4, 6)).intValue();
  if(month>latter){
   month=month-latter;
  }else{
   month=month+12-latter;
   year=year-1;
  }
  if(month<10){
   result=String.valueOf(year)+"0"+String.valueOf(month);
  }else{
   result=String.valueOf(year)+String.valueOf(month);
  } 
  return result;
 }
 
 
 
 /**
  * @return the productInfo
  */
 public String getProductInfo() {
  return productInfo;
 }

 /**
  * @param productInfo the productInfo to set
  */
 public void setProductInfo(String productInfo) {
  this.productInfo = productInfo;
 }

 /**
  * @return the queryType
  */
 public String getQueryType() {
  return queryType;
 }

 /**
  * @param queryType the queryType to set
  */
 public void setQueryType(String queryType) {
  this.queryType = queryType;
 }

 /**
  * @return the period
  */
 public String getPeriod() {
  return period;
 }

 /**
  * @param period the period to set
  */
 public void setPeriod(String period) {
  this.period = period;
 }

 /**
  * @return the serialVersionUID
  */
 public static long getSerialVersionUID() {
  return serialVersionUID;
 }

 /**
  * @param queryfeeService the queryfeeService to set
  */
 public void setQueryfeeService(QueryfeeCommonService queryfeeService) {
  this.queryfeeService = queryfeeService;
 }

 /**
  * @param messageService the messageService to set
  */
 public void setMessageService(ErrorMessageService messageService) {
  this.messageService = messageService;
 }

 /**
  * @return the custBillQrVo
  */
 public CustBillIQrVo getCustBillQrVo() {
  return custBillQrVo;
 }

 /**
  * @param custBillQrVo the custBillQrVo to set
  */
 public void setCustBillQrVo(CustBillIQrVo custBillQrVo) {
  this.custBillQrVo = custBillQrVo;
 }

 /**
  * @return the productBillVo
  */
 public ProductBillVo getProductBillVo() {
  return productBillVo;
 }

 /**
  * @param productBillVo the productBillVo to set
  */
 public void setProductBillVo(ProductBillVo productBillVo) {
  this.productBillVo = productBillVo;
 }
 
 public BlancePayQrVo getBlancePayQrVo() {
  return blancePayQrVo;
 }

 public void setBlancePayQrVo(BlancePayQrVo blancePayQrVo) {
  this.blancePayQrVo = blancePayQrVo;
 }

 public PointsBalanceVo getPointsBalanceVo() {
  return pointsBalanceVo;
 }

 public void setPointsBalanceVo(PointsBalanceVo pointsBalanceVo) {
  this.pointsBalanceVo = pointsBalanceVo;
 }

// public void setPointsManagerService(PointsManagerService pointsManagerService) {
//  this.pointsManagerService = pointsManagerService;
// }

 public String getPicName() {
  return picName;
 }

 public void setPicName(String picName) {
  this.picName = picName;
 }


}

 

 

 

ajax里取圖片時 用servlet的形式,向取驗證碼一樣,需要在web.xml里配置一個servlet,jfreechart組件中的。

 

 

 

 

 

 

 

 

 

一下兩個單獨的例子

 

package test;

import java.awt.Color;
import java.awt.Font;
import java.io.FileOutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.jfree.data.general.PieDataset;

public class TestJFreeTree {

 public static void main(String[] args) {
  PieDataset dataset = getDataSet();
  JFreeChart chart = ChartFactory.createPieChart3D(" 項目進度分布", // chart
                 // title
    dataset,// data
    true,// include legend
    true, false);
  PiePlot3D plot = (PiePlot3D) chart.getPlot();
  // 圖片中顯示百分比:默認方式
  // plot.setLabelGenerator(new
  // StandardPieSectionLabelGenerator(StandardPieToolTipGenerator.DEFAULT_TOOLTIP_FORMAT));
  // 圖片中顯示百分比:自定義方式,{0} 表示選項, {1} 表示數值, {2} 表示所佔比例 ,小數點後兩位
  plot.setLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}={1}({2})", NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));
  // 圖例顯示百分比:自定義方式, {0} 表示選項, {1} 表示數值, {2} 表示所佔比例
  plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator(
    "{0}={1}({2})",NumberFormat.getNumberInstance(),
    new DecimalFormat("0.00%")));
  // 設置背景色為白色
  chart.setBackgroundPaint(Color.white);
  // 指定圖片的透明度(0.0-1.0)
  plot.setForegroundAlpha(1.0f);
  // 指定顯示的餅圖上圓形(false)還橢圓形(true)
  plot.setCircular(true);
  // 設置圖標題的字體
  Font font = new Font(" 黑體", Font.CENTER_BASELINE, 20);
  TextTitle title = new TextTitle(" 項目狀態分布");
  title.setFont(font);
  chart.setTitle(title);
  FileOutputStream fos_jpg = null;
  try {
   fos_jpg = new FileOutputStream("D:// 項目狀態分布.jpg");
   ChartUtilities
     .writeChartAsJPEG(fos_jpg, 100, chart, 540, 280, null);
   fos_jpg.close();
  } catch (Exception e) {
  }
 }

 private static PieDataset getDataSet() {
  DefaultPieDataset dataset = new DefaultPieDataset();
  dataset.setValue(" 市場前期", new Double(10.5));
  dataset.setValue(" 立項", new Double(15.99));
  dataset.setValue(" 計劃", new Double(10));
  dataset.setValue(" 需求與設計", new Double(10));
  dataset.setValue(" 執行控制", new Double(35.8));
  dataset.setValue(" 收尾", new Double(10));
  dataset.setValue(" 運維", new Double(10));
  return dataset;
 }

}


 

package test;

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

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.axis.NumberAxis;
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.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;
import org.jfree.ui.TextAnchor;

public class BarChart3DDemo {

    /**
     * @param args
     */
    public static void main(String[] args) throws IOException{
        JFreeChart chart=ChartFactory.createBarChart3D(
                "近六個月總費用支出圖",
                "月份",//目錄軸的顯示標籤
                "費用(元)",//數值軸的顯示標籤
                getDataSet(),
                PlotOrientation.VERTICAL,//設置圖表方向
                false,
                false,
                false      
        );
      
        //設置標題
        chart.setTitle(new TextTitle("近六個月總費用支出圖",new Font("黑體",Font.CENTER_BASELINE,22)));
        chart.setBackgroundPaint(Color.white);
      
        //設置圖表部分
        CategoryPlot plot=(CategoryPlot)chart.getPlot();
        plot.setBackgroundAlpha(0.1f);
        plot.setDataAreaRatio(0.00);
        CategoryAxis categoryAxis=plot.getDomainAxis();//取得橫軸
        categoryAxis.setLabelFont(new Font("宋體",Font.BOLD,18));//設置橫軸顯示標籤的字體
        //categoryAxis.setLabelPaint(Color.decode("#F8D661"));
        categoryAxis.setCategoryLabelPositions(CategoryLabelPositions.STANDARD);//分類標籤以45度傾斜
        categoryAxis.setTickLabelFont(new Font("宋體",Font.LAYOUT_LEFT_TO_RIGHT,10));//分類標籤字體
      
        NumberAxis numberAxis=(NumberAxis)plot.getRangeAxis();//取得縱軸
        numberAxis.setLabelFont(new Font("宋體",Font.BOLD,18));//設置縱軸顯示標籤字體
        BarRenderer3D customBarRenderer = (BarRenderer3D) plot.getRenderer();
      
        customBarRenderer.setPositiveItemLabelPosition(new ItemLabelPosition(
          ItemLabelAnchor.OUTSIDE12,TextAnchor.BASELINE_LEFT));
        customBarRenderer.setMaxBarWidth(0.05);
        customBarRenderer.setItemMargin(0.000000005);

        customBarRenderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());//顯示每個柱的數值
        customBarRenderer.setBaseItemLabelsVisible(true);
        //注意:此句很關鍵,若無此句,那數字的顯示會被覆蓋,給人數字沒有顯示出來的問題
        customBarRenderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(
        ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_CENTER));
        customBarRenderer.setItemLabelAnchorOffset(8D);// 設置柱形圖上的文字偏離值
        customBarRenderer.setItemLabelFont(new Font("宋體",Font.ITALIC,12));

        customBarRenderer.setSeriesPaint(0, Color.decode("#24F4DB")); // 給series1 Bar
//        customBarRenderer.setSeriesPaint(1, Color.decode("#7979FF")); // 給series2 Bar
//        customBarRenderer.setSeriesPaint(2, Color.decode("#FF5555")); // 給series3 Bar
//        customBarRenderer.setSeriesPaint(3, Color.decode("#F8D661")); // 給series4 Bar
//        customBarRenderer.setSeriesPaint(4, Color.decode("#F284DC")); // 給series5 Bar
//        customBarRenderer.setSeriesPaint(5, Color.decode("#00BF00")); // 給series6 Bar
      
//        customBarRenderer.setSeriesOutlinePaint(0,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(1,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(2,Color.BLACK); //邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(3,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(4,Color.BLACK);//邊框為黑色
//        customBarRenderer.setSeriesOutlinePaint(5,Color.BLACK); //邊框為黑色
        customBarRenderer.setItemLabelsVisible(true);
       // customBarRenderer.setMinimumBarLength(0.9D);
      
        FileOutputStream fos=null;
        fos=new FileOutputStream("D://book1.jpg");
        ChartUtilities.writeChartAsJPEG(
                fos,
                1,
                chart,
                540,
                330,
                null
      
        );
        fos.close();
    }
    private static CategoryDataset getDataSet(){
        DefaultCategoryDataset dataset=new DefaultCategoryDataset();
        dataset.addValue(43.5,"1", "201010");
        dataset.addValue(180,"1","201009");
        dataset.addValue(92.8, "1", "201008");
        dataset.addValue(25.3, "1", "201007");
        dataset.addValue(25.3, "1", "201006");
        dataset.addValue(25.3, "1", "201005");
        return dataset;
    }

}

本篇文章來源于職場幫(www.guan8.net) 原文出處︰http://www.guan8.net/Java/180113.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值