jfreeChart柱狀圖各屬性詳細設置


一. 下載與環境配置 
此最新版本為 1.0.13 
解壓jfreechart-1.0.13.zip 將lib目錄下的jfreechart-1.0.13.jar 、jcommon-1.0.16.jar 復制到工程 WEB-INF\lib 文件夾中 
二. 配置 
我是用Struts1.2開發的。 
在工程的web.xml 文件中添加 
<servlet> 
            <servlet-name>DisplayChart</servlet-name> 
            <servlet-class>org.jfree.chart.servlet.DisplayChart</servlet-class> 
    </servlet> 
    <servlet-mapping> 
            <servlet-name>DisplayChart</servlet-name> 
            <url-pattern>/DisplayChart</url-pattern> 
    </servlet-mapping> 

三. 生成柱形圖 
新建個Action 最好是繼承DispatchAction 
public ActionForward toBarPic(ActionMapping mapping, ActionForm form, 
HttpServletRequest request, HttpServletResponse response) { 

DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
//添加數據 
dataset.addValue(440, "數據", "類型1"); 
dataset.addValue(360, "數據", "類型2"); 
dataset.addValue(510, "數據", "類型3"); 
dataset.addValue(390, "數據", "類型4"); 
        // 
JFreeChart  chart = ChartFactory.createBarChart3D("XXX統計圖", "類型","數據額", dataset, PlotOrientation.VERTICAL, true, false, false); 


HttpSession session=request.getSession(); 
String filename=""; 
try{ 
//生成寬600,高420 png格式的圖,並將圖片保存到session中,好像只能保存到session中,要不就設置為null 

filename = ServletUtilities.saveChartAsPNG(chart,600,420, null, session); 
}catch(Exception ex){ 
ex.printStackTrace(); 
} 
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; 
        request.setAttribute("graphURL", graphURL); 
        request.setAttribute("filename", filename); 
return mapping.findForward("success"); 
} 

頁面中 圖片鏈接方式 
   <img src="${graphURL}" width=630 height=450 border=0 usemap="#${filename}"> 

生成的圖如下: 
 

可見生成柱形圖很簡單,只需要以下幾步 
1、 DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
2、 dataset.addValue(440, "數據", "類型1"); //封數據 
3、 JFreeChart  chart = ChartFactory.createBarChart3D("XXX統計圖", "類型","數據額", dataset, PlotOrientation.VERTICAL, true, false, false); 
4、 String filename = ServletUtilities.saveChartAsPNG(chart,600,420, null, session);//返回圖片名稱 
//為圖片生成url訪問地址 
5、 String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; 
6、 將 圖片名稱filename 和圖片地址 graphURL 保存到作用域中,最后返回。 

以上是生成柱形圖最基本的步驟,但是生成的圖往往是不合人意的,因為太粗糙了,好比剛蓋好的房子還沒有裝修,下面就對圖形進行美化,裝修這個柱形房子。 
要設置圖片高級屬性先獲得 CategoryPlot 和 BarRenderer3D 大部分修改都是在這上面 
CategoryPlot plot = chart.getCategoryPlot();//設置圖的高級屬性 
  BarRenderer3D renderer = new BarRenderer3D();//3D屬性修改 
最后還得將renderer 放到plot 中 
  plot.setRenderer(renderer);//將修改后的屬性值保存到圖中 

在JFreeChart  chart = ChartFactory.createBarChart3D()下面添加 
CategoryPlot plot = chart.getCategoryPlot();//設置圖的高級屬性 
  BarRenderer3D renderer = new BarRenderer3D();//3D屬性修改 
plot.setRenderer(renderer);//將修改后的屬性值保存到圖中 
然后做的修改都是在 
BarRenderer3D renderer = new BarRenderer3D();//3D屬性修改 
plot.setRenderer(renderer);//將修改后的屬性值保存到圖中 
這兩行中間,也就是以下添加的任何代碼都是在上面這兩行中間添加。 
首先修改顏色 
  
          //設置網格豎線顏色 
  plot.setDomainGridlinePaint(Color.blue); 
  plot.setDomainGridlinesVisible(true); 
          //設置網格橫線顏色 
  plot.setRangeGridlinePaint(Color.blue); 
  plot.setRangeGridlinesVisible(true); 
          //圖片背景色 
  plot.setBackgroundPaint(Color.LIGHT_GRAY); 
  plot.setOutlineVisible(true); 
          //圖邊框顏色 
  plot.setOutlinePaint(Color.magenta); 
          //邊框顏色 
  renderer.setBaseOutlinePaint(Color.ORANGE); 
  renderer.setDrawBarOutline(true); 
          //設置牆體顏色 
  renderer.setWallPaint(Color.gray); 
效果圖如下 

 
  
接下來修改字體,看上圖還是亂碼,下面就對字體做調整 
    //對X軸做操作 
CategoryAxis domainAxis = plot.getDomainAxis(); 
//對Y軸做操作 
ValueAxis rAxis = plot.getRangeAxis(); 
  
/*----------設置消除字體的鋸齒渲染(解決中文問題)--------------*/ 
      chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
      
//設置標題字體 
TextTitle textTitle = chart.getTitle(); 
textTitle.setFont(new Font("黑體", Font.PLAIN, 20)); 
//設置X軸坐標上的文字 
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 
//設置X軸的標題文字 
domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); 
//設置Y軸坐標上的文字 
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 
//設置Y軸的標題文字 
rAxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));      
//底部漢字亂碼的問題  
chart.getLegend().setItemFont(new Font("宋體",Font.PLAIN,12)); 
效果圖如下 
 

在上面基礎上在做一下高級設計,設計字體顏色和節段 
//對X軸做操作 
  CategoryAxis domainAxis = plot.getDomainAxis(); 
  //對Y軸做操作 
  ValueAxis rAxis = plot.getRangeAxis(); 
  
  /*--------設置消除字體的鋸齒渲染(解決中文問題)---------*/ 
   chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
      
      //設置標題字體 
      TextTitle textTitle = chart.getTitle(); 
      textTitle.setFont(new Font("黑體", Font.PLAIN, 20)); 
          textTitle.setBackgroundPaint(Color.LIGHT_GRAY);//標題背景色 
          textTitle.setPaint(Color.cyan);//標題字體顏色 
          textTitle.setText("類型統計圖");//標題內容  
      //設置X軸坐標上的文字 
domainAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 11)); 
      //設置X軸的標題文字 
      domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); 
      
      domainAxis.setTickLabelPaint(Color.red);//X軸的標題文字顏色 
      domainAxis.setTickLabelsVisible(true);//X軸的標題文字是否顯示 
      domainAxis.setAxisLinePaint(Color.red);//X軸橫線顏色 
      domainAxis.setTickMarksVisible(true);//標記線是否顯示 
      domainAxis.setTickMarkOutsideLength(3);//標記線向外長度 
      domainAxis.setTickMarkInsideLength(3);//標記線向內長度 
      domainAxis.setTickMarkPaint(Color.red);//標記線顏色 
      /**  Y軸設計同X軸相類似  **/ 
      //設置Y軸坐標上的文字 
      rAxis.setTickLabelFont(new Font("宋體", Font.PLAIN, 12)); 
      rAxis.setMinorTickCount(7);//顯示有多少標記段 
      rAxis.setMinorTickMarksVisible(true); 

rAxis.setRange(100, 600); //Y軸取值范圍(或者如下設置) 
     // rAxis.setLowerBound(100);  //Y軸以開始的最小值 
     // rAxis.setUpperBound(600);Y軸的最大值 
      //設置Y軸的標題文字 
      rAxis.setLabelFont(new Font("黑體", Font.PLAIN, 12));      
     //底部漢字亂碼的問題  
     chart.getLegend().setItemFont(new Font("宋體",Font.PLAIN,12)); 
 
  
大多數情況下都需要在柱子上顯示對應的數值,設置如下: 
       renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
   renderer.setBaseItemLabelsVisible(true); 
   renderer.setBaseItemLabelPaint(Color.BLUE);//設置數值顏色,默認黑色 
   用下面設置也能達到同樣效果 
       renderer.setItemLabelGenerator( new  StandardCategoryItemLabelGenerator()); 
renderer.setItemLabelsVisible( true ); 
renderer.setItemLabelPaint(Color.BLUE); 
效果圖如下: 
   
默認的數值如上圖所示,但是大多數情況下我們要應對不同的需求對數值顯示的位置等進行調整。具體調整如下:
         //搭配ItemLabelAnchor TextAnchor 這兩項能達到不同的效果,但是 
ItemLabelAnchor最好選OUTSIDE,因為INSIDE顯示不出來 
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT)); 
     //下面可以進一步調整數值的位置,但是得根據ItemLabelAnchor選擇情況,例 
如我選的是OUTSIDE12,那么下面設置為正數時,數值就會向上調整,負數則向下 
       renderer.setItemLabelAnchorOffset(10); 
效果圖如圖六: 
 

有時候柱子過多,而且圖大小有限,就得將柱子類型傾斜顯示,或者將其放到上面等。看如下配置: 
    //將類型放到上面 
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); 
    //橫軸上的 Lable 45度傾斜 
      domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); 
//將默認放到左邊的數值放到右邊 
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 

效果圖如下: 
   

看上面的柱子太粗,實在不好看,下面就是調整柱子的寬度和透明度等: 
           //設置距離圖片左端距離 
      domainAxis.setUpperMargin(0.2); 
       //設置距離圖片右端距離 
      domainAxis.setLowerMargin(0.2); 
          //數據軸精度 
       NumberAxis na = (NumberAxis) plot.getRangeAxis(); 
          na.setAutoRangeIncludesZero(true); 
      DecimalFormat df = new DecimalFormat("#0.000"); 
      //數據軸數據標簽的顯示格式 
      na.setNumberFormatOverride(df); 
          //設置柱的透明度 
      plot.setForegroundAlpha(1.0f); 

   



詳細設置如下:DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 
//添加數據 
dataset.addValue(440, "數據", "類型1"); 
dataset.addValue(360, "數據", "類型2"); 
dataset.addValue(510, "數據", "類型3"); 
dataset.addValue(390, "數據", "類型4"); 
/**參數分別為:圖表標題 ,目錄軸的顯示標簽 ,數值軸的顯示標簽 ,數據集 ,是否生成URL鏈接 
圖表方向:水平、垂直, 是否顯示圖例(對於簡單的柱狀圖必須是false) ,是否生成工具 */ 
JFreeChart chart = ChartFactory.createBarChart3D("XXX統計圖", "類型","數據額", dataset, PlotOrientation.VERTICAL, true, false, true); 

/*----------設置消除字體的鋸齒渲染(解決中文問題)--------------*/ 
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, 
RenderingHints.VALUE_TEXT_ANTIALIAS_OFF); 
//底部漢字亂碼的問題 
chart.getLegend().setItemFont(new Font("宋體",Font.PLAIN,12)); 
//設置標題字體 
TextTitle textTitle = chart.getTitle(); 
textTitle.setFont(new Font("黑體", Font.PLAIN, 20)); 
textTitle.setBackgroundPaint(Color.LIGHT_GRAY);//標題背景色 
textTitle.setPaint(Color.cyan);//標題字體顏色 
textTitle.setText("類型統計圖");//標題內容 


CategoryPlot plot = chart.getCategoryPlot();//設置圖的高級屬性 
BarRenderer3D renderer = new BarRenderer3D();//3D屬性修改 
CategoryAxis domainAxis = plot.getDomainAxis();//對X軸做操作 
ValueAxis rAxis = plot.getRangeAxis();//對Y軸做操作 

/*** 
* domainAxis設置(x軸一些設置) 
**/ 

//設置X軸坐標上的文字 
domainAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 11)); 
//設置X軸的標題文字 
domainAxis.setLabelFont(new Font("宋體", Font.PLAIN, 12)); 
domainAxis.setLabel("");//X軸的標題內容 
domainAxis.setTickLabelPaint(Color.red);//X軸的標題文字顏色 
domainAxis.setTickLabelsVisible(true);//X軸的標題文字是否顯示 
domainAxis.setAxisLinePaint(Color.red);//X軸橫線顏色 
domainAxis.setTickMarksVisible(true);//標記線是否顯示 
domainAxis.setTickMarkOutsideLength(3);//標記線向外長度 
domainAxis.setTickMarkInsideLength(3);//標記線向內長度 
domainAxis.setTickMarkPaint(Color.red);//標記線顏色 
domainAxis.setUpperMargin(0.2);//設置距離圖片左端距離 
domainAxis.setLowerMargin(0.2); //設置距離圖片右端距離 
//橫軸上的 Lable 是否完整顯示 
domainAxis.setMaximumCategoryLabelWidthRatio(0.6f); 
//橫軸上的 Lable 45度傾斜 
domainAxis.setCategoryLabelPositions(CategoryLabelPositions.DOWN_45); 

/** 
* rAxis設置 Y軸設置 
* 
**/ 

//設置Y軸坐標上的文字 
rAxis.setTickLabelFont(new Font("sans-serif", Font.PLAIN, 12)); 
//設置Y軸的標題文字 
rAxis.setLabelFont(new Font("黑體", Font.PLAIN, 12)); 
//Y軸取值范圍(下面不能出現 rAxis.setAutoRange(true) 否則不起作用) 
rAxis.setRange(100, 600); 
// rAxis.setLowerBound(100); //Y軸以開始的最小值 
// rAxis.setUpperBound(600);//Y軸的最大值 
rAxis.setLabel("content");//Y軸內容 
rAxis.setLabelAngle(1.6);//標題內容顯示角度(1.6時候水平) 
rAxis.setLabelPaint(Color.red);//標題內容顏色 
rAxis.setMinorTickMarksVisible(true);//標記線是否顯示 
rAxis.setMinorTickCount(7);//節段中的刻度數 
rAxis.setMinorTickMarkInsideLength(3);//內刻度線向內長度 
rAxis.setMinorTickMarkOutsideLength(3);//內刻度記線向外長度 
rAxis.setTickMarkInsideLength(3);//外刻度線向內長度 
rAxis.setTickMarkPaint(Color.red);//刻度線顏色 
rAxis.setTickLabelsVisible(true);//刻度數值是否顯示 
// 所有Y標記線是否顯示(如果前面設置rAxis.setMinorTickMarksVisible(true); 則其照樣顯示) 
rAxis.setTickMarksVisible(true); 
rAxis.setAxisLinePaint(Color.red);//Y軸豎線顏色 
rAxis.setAxisLineVisible(true);//Y軸豎線是否顯示 
//設置最高的一個 Item 與圖片頂端的距離 (在設置rAxis.setRange(100, 600);情況下不起作用) 
rAxis.setUpperMargin(0.15); 
//設置最低的一個 Item 與圖片底端的距離 
rAxis.setLowerMargin(0.15); 
rAxis.setAutoRange(true);//是否自動適應范圍 
rAxis.setVisible(true);//Y軸內容是否顯示 

//數據軸精度 
NumberAxis na = (NumberAxis) plot.getRangeAxis(); 
na.setAutoRangeIncludesZero(true); 
DecimalFormat df = new DecimalFormat("#0.000"); 
//數據軸數據標簽的顯示格式 
na.setNumberFormatOverride(df); 

/** 
* renderer設置 柱子相關屬性設置 
*/renderer.setBaseOutlinePaint(Color.ORANGE); //邊框顏色 
renderer.setDrawBarOutline(true); 
renderer.setWallPaint(Color.gray);//設置牆體顏色 
renderer.setMaximumBarWidth(0.08); //設置柱子寬度 
renderer.setMinimumBarLength(0.1); //設置柱子高度 
renderer.setSeriesPaint(0,Color.ORANGE); //設置柱的顏色 
renderer.setItemMargin(0); //設置每個地區所包含的平行柱的之間距離 
//在柱子上顯示相應信息 
renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
renderer.setBaseItemLabelsVisible(true); 
renderer.setBaseItemLabelPaint(Color.BLACK);//設置數值顏色,默認黑色 
//搭配ItemLabelAnchor TextAnchor 這兩項能達到不同的效果,但是ItemLabelAnchor最好選OUTSIDE,因為INSIDE顯示不出來 
renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.CENTER_LEFT)); 
//下面可以進一步調整數值的位置,但是得根據ItemLabelAnchor選擇情況. 
renderer.setItemLabelAnchorOffset(10); 

/** 
* plot 設置 
***/ 
//設置網格豎線顏色 
plot.setDomainGridlinePaint(Color.blue); 
plot.setDomainGridlinesVisible(true); 
//設置網格橫線顏色 
plot.setRangeGridlinePaint(Color.blue); 

plot.setRangeGridlinesVisible(true); 
//圖片背景色 
plot.setBackgroundPaint(Color.LIGHT_GRAY); 
plot.setOutlineVisible(true); 
//圖邊框顏色 
plot.setOutlinePaint(Color.magenta); 
//設置柱的透明度 
plot.setForegroundAlpha(1.0f); 
//將類型放到上面 
plot.setDomainAxisLocation(AxisLocation.TOP_OR_RIGHT); 
//將默認放到左邊的數值放到右邊 
plot.setRangeAxisLocation(AxisLocation.BOTTOM_OR_RIGHT); 
plot.setRenderer(renderer);//將修改后的屬性值保存到圖中 

HttpSession session=request.getSession(); 
String filename=""; 

try{ 
//生成寬600,高420 png格式的圖,並將圖片保存到session中,好像只能保存到session中,要不就設置為null 
filename = ServletUtilities.saveChartAsPNG(chart,600,420, null, session); 
}catch(Exception ex){ 
ex.printStackTrace(); 
} 
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename; 
request.setAttribute("graphURL", graphURL); 
request.setAttribute("filename", filename); 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM