/**
**QQ:252574345
**MSN:lele_love_lomboz@hotmail.com
*/
饼图效果如下,包含大部分的配置
开发环境:
eclipse 3.2
Jfreechart 1.0.6
tomcat 5.0.28
代码如下:
pie.jsp(付后下载)
<%@ page contentType="text/html;charset=GBK"%>
<%@ page import="java.awt.Color,
org.jfree.chart.ChartFactory,
org.jfree.chart.JFreeChart,
org.jfree.chart.servlet.ServletUtilities,
java.awt.Font,
org.jfree.chart.block.*,
org.jfree.chart.plot.PiePlot,
org.jfree.chart.title.LegendTitle,
org.jfree.data.general.DefaultPieDataset,
org.jfree.data.general.PieDataset,
org.jfree.ui.*,
org.jfree.chart.labels.StandardPieItemLabelGenerator,
org.jfree.chart.labels.StandardPieSectionLabelGenerator,
java.text.DecimalFormat,
java.text.NumberFormat
"%>
<%!
private static PieDataset createDataset()
{
DefaultPieDataset defaultpiedataset = new DefaultPieDataset();
defaultpiedataset.setValue("图书", new Double(33.2));
defaultpiedataset.setValue("电器", new Double(10D));
defaultpiedataset.setValue("玩具", new Double(27.5D));
defaultpiedataset.setValue("水果", new Double(9.5D));
defaultpiedataset.setValue("饮料", new Double(10D));
defaultpiedataset.setValue("酒类", new Double(9.8));
return defaultpiedataset;
}
%>
<%
JFreeChart jfreechart = ChartFactory.createPieChart("自定义图例Legend", createDataset(), false, true, false);
PiePlot pieplot = (PiePlot)jfreechart.getPlot();
pieplot.setLabelFont(new Font("宋体", 0, 12));
pieplot.setNoDataMessage("无数据");
pieplot.setCircular(true);
pieplot.setLabelGap(0.02D);
pieplot.setBackgroundPaint(new Color(199,237,204));
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator(
"{0} {2}",
NumberFormat.getNumberInstance(),
new DecimalFormat("0.00%")));
pieplot.setLegendLabelGenerator(new StandardPieItemLabelGenerator("{0} {2}"));
LegendTitle legendtitle = new LegendTitle(jfreechart.getPlot());
BlockContainer blockcontainer = new BlockContainer(new BorderArrangement());
blockcontainer.setBorder(new BlockBorder(1.0D, 1.0D, 1.0D, 1.0D));
LabelBlock labelblock = new LabelBlock("本周商品出货单:", new Font("宋体", 1, 12));
labelblock.setPadding(5D, 5D, 5D, 5D);
blockcontainer.add(labelblock, RectangleEdge.TOP);
LabelBlock labelblock1 = new LabelBlock("by 赵永亮 2008.6.24");
labelblock1.setPadding(8D, 20D, 2D, 5D);
blockcontainer.add(labelblock1, RectangleEdge.BOTTOM);
BlockContainer blockcontainer1 = legendtitle.getItemContainer();
blockcontainer1.setPadding(2D, 10D, 5D, 2D);
blockcontainer.add(blockcontainer1);
legendtitle.setWrapper(blockcontainer);
legendtitle.setPosition(RectangleEdge.RIGHT);
legendtitle.setHorizontalAlignment(HorizontalAlignment.LEFT);
jfreechart.addSubtitle(legendtitle);
jfreechart.setBackgroundPaint(new Color(199,237,204));
String filename = ServletUtilities.saveChartAsPNG(jfreechart, 400, 400, session);
//调用DisplayChart(配置在webxml中),生成图形
String graphURL = request.getContextPath() + "/DisplayChart?filename=" + filename;
%>
<table width="100%">
<tr>
<td align="center">
<!-- 生成图形 -->
<img src="<%= graphURL %>" width=400 height=400 border=0 alt="">
</td>
</tr>
</table>