下面的JFreeChartAction可以生成图片了,但不知在struts2中如何生成热点图片,请高手指教一下,谢谢 !import java.awt.*;import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartRenderingInfo;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.entity.StandardEntityCollection;
import org.jfree.chart.labels.StandardPieToolTipGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.urls.StandardPieURLGenerator;
import org.jfree.data.general.DefaultPieDataset;
import com.opensymphony.xwork2.ActionSupport;public class JFreeChartAction extends ActionSupport{
    private static final long serialVersionUID = 5752180822913527064L;    private JFreeChart chart;
    
    private DefaultPieDataset getData() {
        DefaultPieDataset data = new DefaultPieDataset();
        data.setValue("Java", new Double(43.2));
        data.setValue("Visual Basic", new Double(1.0));
        data.setValue("C/C++", new Double(17.5));
        data.setValue("tangjun", new Double(60.0));
        return data;
    }
    
    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }    public JFreeChart getChart() {
        chart = ChartFactory.createPieChart("",getData(),true,true,true);
        LegendTitle legend = chart.getLegend(0);
        legend.setItemFont(new Font("宋体", Font.BOLD, 13));
        PiePlot plot = (PiePlot) chart.getPlot();
        plot.setLabelFont(new Font("隶书", Font.BOLD, 26));
        plot.setURLGenerator(new StandardPieURLGenerator("index.jsp"));
        plot.setToolTipGenerator(new StandardPieToolTipGenerator());
        return chart;
    }
}

解决方案 »

  1.   

    jsp是类似这样产生的,不知struts2中是如何产生的.<%
    plot.setToolTipGenerator(new StandardPieItemLabelGenerator()); 
    StandardEntityCollection sec = new StandardEntityCollection(); 
    ChartRenderingInfo info = new ChartRenderingInfo(sec); 
    PrintWriter w = new PrintWriter(out);//输出MAP信息 
    //500是图片长度,300是图片高度 
    String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, info, session); 
    ChartUtilities.writeImageMap(w, "map0", info, false); String graphURL = request.getContextPath() + "/servlet/DisplayChart?filename=" + filename; %> <P ALIGN="CENTER"> 
    <img src="<%= graphURL %>" width=500 height=300 border=0 usemap="#map0"> 
    </P> 
      

  2.   

    有一个笨方法,MAP和图片其实都是一样。。
    我记得在JFreeChart 中有生成map的方法,你把两个都传回页面显示,只要坐标对了就行了。
      

  3.   

    先在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>/servlet/DisplayChart</url-pattern>
    </servlet-mapping>
      

  4.   

    然后java代码引用生成的图片String filename;
    try
    {
    filename = ServletUtilities.saveChartAsJPEG(chart, 980, 400, null,
    getSession());
    }
    catch (IOException e)
    {
    filename = null;
    e.printStackTrace();
    }
    if (filename == null)
    {
    graphURL = null;
    }
    else
    {
    // getRequest().getContextPath()+
    graphURL = "/servlet/DisplayChart?filename=" + filename;
    }
      

  5.   

    public byte[] getChart(){
    //设置数据集   
    DefaultPieDataset dataset = new DefaultPieDataset();   
    dataset.setValue("初中高级程序员", 0.5);   
    dataset.setValue("项目经理", 0.1);   
    dataset.setValue("系统分析师", 0.1);   
    dataset.setValue("软件架构师", 0.1);   
    dataset.setValue("其他", 0.2);   
    //通过工厂类生成JFreeChart对象   
    JFreeChart chart = ChartFactory.createPieChart3D("IT行业职业分布图", dataset, true, false, false);   
    PiePlot pieplot = (PiePlot) chart.getPlot();   
    pieplot.setLabelFont(new Font("宋体", 0, 12));   
    //pieplot.setExplodePercent();   
      
    //标题字体   
    Font font = new Font("SimSun", 10, 20);   
    TextTitle textTitle = chart.getTitle();   
    textTitle.setFont(font);   
    textTitle.setPaint(Color.BLUE);   
      
    //联想细节   
    LegendTitle legend = chart.getLegend();   
    legend.setItemFont(new Font("宋体", Font.PLAIN, 15));   
    legend.setItemPaint(Color.BLUE);   
      
    //没有数据的时候显示的内容   
    pieplot.setNoDataMessage("无数据显示");   
    pieplot.setCircular(false);   
    pieplot.setLabelGap(0.02D);   
    try {
    String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300, null, null);
    } catch (IOException e1) {
    e1.printStackTrace();
    }  

     
    FileOutputStream fos_jpg = null;   

    ByteArrayOutputStream out = new ByteArrayOutputStream(); try {
    ChartUtilities.writeChartAsJPEG(out, chart, 400, 300);
    } catch (IOException e) {
    e.printStackTrace();
    }   
    byte[] buff = out.toByteArray();
    return buff;
    }action中
    public void getChart(){
    byte[] buff = chartService.getChart();
    HttpServletResponse response = ServletActionContext.getResponse();
    response.setContentType("image/jpeg");
    try {
    response.getOutputStream().write(buff);
    } catch (IOException e) {
    e.printStackTrace();
    }
    }
    jsp中
    <img alt="" src="getChart">不知道对楼主是否有用