|
<%@ 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> |