下面的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;
}
}
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;
}
}
解决方案 »
- 一个网站需要注册码,想知道它的原理?
- hibernate级联子表删除操作失败
- JDK版本查看问题
- 关于ejb jar的问题
- 使用ant运行xdoclet生成hbm.xml问题,运行通过但不生成hbm.xml
- struts+ajax
- 请问,如何用jxl将在jsp页面中xml数据写入到服务器上的excel文件?
- 我用javamail做的邮件发送,发送出去的邮件被当作垃圾邮件处理了,是什么原因?
- 请教struts书籍
- 关于jboss中使用log4j的问题,大哥们救救我啊!
- 求解,关于java gui 想要在paintComponent里面添加新组建的问题
- DOM4J 问题,以下是XML文件 ,我在读取XML文件的时候,不能将两个配置都读取出来,不知道大家有没办法。。。
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>
我记得在JFreeChart 中有生成map的方法,你把两个都传回页面显示,只要坐标对了就行了。
<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>
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;
}
//设置数据集
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">不知道对楼主是否有用