liuhuapiaoyuan

第一步:准备圆饼图数据

 /**
     * 创建一个数据集合
     * 
     * @return 返回的是一个圆饼图数据
     */ 
    private static DefaultPieDataset getDataSet() { 
        DefaultPieDataset dataset = new DefaultPieDataset(); 
        dataset.setValue("苹果", 100); 
        dataset.setValue("梨子", 200); 
        dataset.setValue("葡萄", 300); 
        dataset.setValue("香蕉", 400); 
        dataset.setValue("荔枝", 500); 
        return dataset; 
    } 

第二步:利用HttpServlet的service方法

  public void service(ServletRequest req, ServletResponse res) 
            throws ServletException, IOException { 
        res.setContentType("image/jpeg");// 将输出设置为image/jepg格式 
        DefaultPieDataset data = getDataSet();// 创建数据集合容器 
        JFreeChart chart = ChartFactory.createPieChart3D("水果产量图", // 图表标题 
                data, // 数据集 
                true, // 是否显示图例 
                false, // 是否生成工具 
                false // 是否生成URL链接 
                );// 创建图表  
        ChartUtilities.writeChartAsJPEG(res.getOutputStream(), 1.0f, chart, 
                800, 450, null);// 输出图表  
    }  

第三步:配置WEB.XML之中 的 servlet,这里不废话

 第四步:在JSP页面显示:

<img src="servlet的配置路径"/>

 第五步:补充,修正中文出现 口口 的问题:

Font titleFont = new Font("黑体", Font.BOLD, 20); 
    TextTitle textTitle = chart.getTitle(); 
    textTitle.setFont(titleFont);// 为标题设置上字体 
 
Font plotFont = new Font("宋体", Font.PLAIN, 16); 
PiePlot plot = (PiePlot) chart.getPlot(); 
plot.setLabelFont(plotFont); // 为饼图元素设置上字体 
 
Font LegendFont = new Font("楷体", Font.PLAIN, 18); 
LegendTitle legend = chart.getLegend(0); 
legend.setItemFont(LegendFont);// 为图例说明设置字体

第六步:让圆饼图显示每一种情况占用了多少百分比

plot.setLabelGenerator(new StandardPieSectionLabelGenerator(

"{0} {2}",NumberFormat.getNumberInstance(),

new DecimalFormat("0.00%")));// 显示百分比 

最终显示:

 

 

 

 

 

分类:

技术点:

相关文章:

  • 2021-07-26
  • 2021-12-31
  • 2022-01-11
  • 2021-12-31
  • 2022-12-23
  • 2022-12-23
  • 2021-10-06
  • 2022-12-23
猜你喜欢
  • 2021-08-22
  • 2022-02-02
  • 2022-12-23
  • 2022-12-23
  • 2021-08-01
  • 2022-03-06
  • 2021-08-07
相关资源
相似解决方案