论坛首页 入门技术论坛

完整Jfreechart饼图实例包含大部分设置

浏览 11931 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2008-06-26  

/**

**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>

  • pie.rar (1.4 KB)
  • 下载次数: 973
   发表时间:2008-12-08  
0 请登录后投票
   发表时间:2009-03-15  
顶,正需要这样的资源
0 请登录后投票
   发表时间:2009-03-15  
来一个曲线图的实例
0 请登录后投票
论坛首页 入门技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics