java 百分比餅圖的實現代碼

來源:互聯網
上載者:User

一個顯示百分比的餅圖如下

實現代碼

 代碼如下 複製代碼

<%@ page contentType="text/html;charset=GBK"%>
 
 <%@ page
     import="org.jfree.chart.*,org.jfree.chart.plot.PiePlot,
     org.jfree.data.general.DefaultPieDataset,
     org.jfree.chart.servlet.ServletUtilities,
     java.awt.*,org.jfree.chart.title.TextTitle"%>
 <%@ page
     import="org.jfree.chart.labels.StandardPieSectionLabelGenerator"%>
 <%@ page import="java.text.NumberFormat"%>
 <%@page import="java.text.DecimalFormat"%>
 
 <%
     //設定資料集
     DefaultPieDataset dataset = new DefaultPieDataset();
     dataset.setValue("初中進階程式員", 0.52);
     dataset.setValue("專案經理", 0.1);
     dataset.setValue("系統分析師", 0.1);
     dataset.setValue("軟體架構師", 0.1);
     dataset.setValue("其他", 0.18);
 
     //通過工廠類產生JFreeChart對象
     JFreeChart chart = ChartFactory.createPieChart3D("IT行業職業分布圖",
             dataset, true, false, false);
 
     PiePlot pieplot = (PiePlot) chart.getPlot();
     //一塊突出的餅圖,在網上搜了好久也沒找到可行的實現方式,歡迎大俠指導 <?後期如果找到解決方案再來做修改?>
 //pieplot.setExplodePercent("A",0.3D);//炸開的餅圖,目前實現還有問題
 
 //DecimalFormat:
 //NumberFormat:
 //StandardPieSectionLabelGenerator:
 //setLabelGenerator():   
     DecimalFormat df = new DecimalFormat("0.00%");//獲得一個DecimalFormat對象,主要是設定小數問題
     NumberFormat nf = NumberFormat.getNumberInstance();//獲得一個NumberFormat對象
     StandardPieSectionLabelGenerator sp = new StandardPieSectionLabelGenerator(
             "{0}{2}", nf, df);//獲得StandardPieSectionLabelGenerator對象
     pieplot.setLabelGenerator(sp);//設定餅圖顯示百分比
 
 //沒有資料的時候顯示的內容
     pieplot.setNoDataMessage("無資料顯示");
     pieplot.setCircular(false);
     pieplot.setLabelGap(0.02D);
 
     pieplot.setIgnoreNullValues(true);//設定不顯示空值
     pieplot.setIgnoreZeroValues(true);//設定不顯示負值
 
 //標題文字亂碼  IT行業職業分布圖
     TextTitle textTitle = chart.getTitle();
     textTitle.setFont(new Font("宋體", Font.PLAIN, 20));
 
     //餅上的文字亂碼
     PiePlot plot = (PiePlot) chart.getPlot();
     plot.setLabelFont(new Font("宋體", Font.PLAIN, 12));
 
     //圖例文字亂碼 餅圖下面的5個說明
     chart.getLegend().setItemFont(new Font("宋體", Font.PLAIN, 12));
 
     String filename = ServletUtilities.saveChartAsPNG(chart, 500, 300,
             null, session);
     String graphURL = request.getContextPath()
             + "/DisplayChart?filename=" + filename;
 %>
 
 <html>
     <head>
         <title>餅狀圖2(加上百分比並反白某塊)</title>
     </head>
     <body>
         <img src="<%=graphURL%>" width=500 height=300 border=0
             usemap="#<%= filename %>">
     </body>
 </html>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.