用JfreeChart創建熱點圖片

在實際項目開發中,尤其是報表開發經常要用來熱點圖片

JAVA的開源項目JfreeChart提供了熱點生成圖片的方法

這裏介紹幾種方法

第一在JSP頁面中:

<%@ page language="java" pageEncoding="UTF-8"%>
<%@ include file="/WEB-INF/jsp/include.jsp"%>
<%@ page import="org.jfree.data.general.DefaultPieDataset"%> 
<%@ page import="org.jfree.chart.*"%> 
<%@ page import="org.jfree.chart.plot.*"%> 
<%@ page import="org.jfree.chart.servlet.ServletUtilities"%> 
<%@ page import="org.jfree.chart.urls.StandardPieURLGenerator"%> 
<%@ page import="org.jfree.chart.entity.StandardEntityCollection"%> 
<%@ page import="java.io.*"%> 
<HTML> 
<HEAD> 
<META http-equiv=Content-Type content="text/html; charset=utf-8"> 
<TITLE>JfreeChart</TITLE> 
</HEAD> 
<BODY> 
<% 
DefaultPieDataset data 
= new DefaultPieDataset(); 
data.setValue(
"高中以下",370); 
data.setValue(
"高中",1530); 
data.setValue(
"大專",5700); 
data.setValue(
"本科",8280); 
data.setValue(
"碩士",4420); 
data.setValue(
"博士",80); 

PiePlot3D plot 
= new PiePlot3D(data);//3D餅圖 
plot.setURLGenerator(
new StandardPieURLGenerator("barview.jsp","lion"));//設定鏈接 
JFreeChart chart 
= new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true); 
chart.setBackgroundPaint(java.awt.Color.white);
//可選,設置圖片背景色 
chart.setTitle(
"程序員學歷情況調查表");//可選,設置圖片標題 

StandardEntityCollection sec 
= new StandardEntityCollection(); 
ChartRenderingInfo info 
= new ChartRenderingInfo(sec); 
PrintWriter w 
= new PrintWriter(out);//輸出MAP信息 
//500是圖片長度,300是圖片高度 
String filename = ServletUtilities.saveChartAsPNG(chart, 500300, info, session); 
ChartUtilities.writeImageMap(w, 
"map0", info, false); 
String graphURL = request.getContextPath() + "/servletDisplayChart?filename=" + filename; 
%> 
<ALIGN="CENTER"> 
<img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#map0"> 
</P> 
</BODY> 
</HTML> 

相應的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>/servletDisplayChart</url-pattern> 
  
</servlet-mapping>  

生成的圖片如下

該方法引用網站:http://dev.yesky.com/307/2036307.shtml

第二種方法

把java代碼寫在jsp頁面中在大的工程維護起來極不方便,我所用的項目採用Struts框架做表現層

這裏我就介紹我所用的方法

java文件:

public class ImageAction extends Action {

    
public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response) 
{
    ImageForm image 
= (ImageForm)form;
        
int width=0, height=0;

        JFreeChart chart 
= null;
    width 
= 248;
    height 
= 240;
    
    chart 
= createEventShareImage();
    
try {
                StandardEntityCollection sec 
= new StandardEntityCollection(); 
                ChartRenderingInfo info 
= new ChartRenderingInfo(sec); 
                
                String filename 
= ServletUtilities.saveChartAsJPEG(chart, width, 240, info, request.getSession());
                String mapMessage 
= ChartUtilities.getImageMap("map0", info);
                String src 
= request.getContextPath()+"/servletDisplayChart?filename=" + filename ;
                String useMap 
= "#" + filename ;

    }
 catch (IOException e) {
                e.printStackTrace();
    }


     image.setMap(mapMessage);
     image.setSrc(src) ;
     image.setUseMap(useMap) ;

         
return mapping.getInputForward();
    }


    
private JFreeChart createEventShareImage() {

        Object[] share 
= eventCommonService.getEventShare();

        DefaultPieDataset pieDataset 
= new DefaultPieDataset();

        pieDataset.setValue(
"嚴重"new Long("100"));
        pieDataset.setValue(
"高級"new Long("200"));
        pieDataset.setValue(
"中級"new Long("150"));
        pieDataset.setValue(
"低級"new Long("300"));
        pieDataset.setValue(
"輕微"new Long("288"));
        
        PiePlot plot 
= new PiePlot(pieDataset);
        
        plot.setNoDataMessage(
"No data available");

        plot.setSectionPaint(
"嚴重", Constant.TERRIBLE_COLOR);
        plot.setSectionPaint(
"高級", Constant.HIGH_COLOR);
        plot.setSectionPaint(
"中級", Constant.MIDDLE_COLOR);
        plot.setSectionPaint(
"低級", Constant.LOW_COLOR);
    plot.setSectionPaint(
"輕微", Constant.INFO_COLOR);

    plot.setLabelGenerator(
new StandardPieSectionLabelGenerator("{0} ({2})"));
    plot.setURLGenerator(
new StandardPieURLGenerator("eventImage.do","type","id"));        

        JFreeChart chart 
= new JFreeChart("",JFreeChart.DEFAULT_TITLE_FONT, plot, true); 

        
return chart;
    }

jsp頁面:

 

<bean:write name="imageForm" property="mapMessage" filter="false"/>

<img src="<bean:write name="imageForm" property="src" />" usemap="<bean:write name="imageForm" property="useMap" />"/>

xml的配置和上面一樣

生成圖片:

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章