jfreechart 饼状图示例- 3D效果
/**
*
*<p>Title: geCustomeCurrentFee</p>
*<p>Description:TODO 得到当前话费</p>
* @param @param mobileTeleNo
* @param @return 设定文件
* @return String 返回类型
* @throws
*/
public String getCustomeCurrentFee(){
String mobileTeleNo=HttpUtil.getParameter("mobileTeleNo");
HttpServletResponse response= ServletActionContext.getResponse();
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
response.setContentType("text/html; charset=GBK");
response.setHeader("Pragma", "No-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
try{
if(LoginUtil.getUserInfo()==null){
logger.debug("Session已失效!");
PrintWriter writer= response.getWriter();
writer.print("Session已失效,请重新登陆!");
response.flushBuffer();
return null;
}
try{
session.removeAttribute("JFreeChart_Deleter");
JFreeChart chart = createChart(mobileTeleNo);
String filename = ServletUtilities.saveChartAsPNG(chart, 215, 200, null, session);//宽度,长度
String graphURL = graphURL = request.getContextPath()+ "/servlet/DisplayChart?filename=" + filename;
PrintWriter writer= response.getWriter();
writer.print(graphURL);
response.flushBuffer();
} catch (IOException e1) {
logger.error(e1.getMessage());
return "";
}
}catch(Exception e){
try {
PrintWriter writer= response.getWriter();
writer.print("保存数据失败!");
response.flushBuffer();
return null;
} catch (IOException e1) {
logger.error(e.getMessage());
return "";
}
}
return null;
}
private JFreeChart createChart(String mobileTeleNo) throws Exception{
JFreeChart chart = null;
PieDataset data = this.createDataset(mobileTeleNo);
if(data != null){
PiePlot plot = new PiePlot(data);
plot.setDataset(data);
//整个标签包括连接线都不显示
plot.setLabelGenerator(null);
//没有数据时显示的消息
plot.setNoDataMessage("无法获取到该业务号码的话费信息");
//图例显示百分比:{0}表示选项,{1}表示数值,{2}表示所占比例
plot.setLegendLabelGenerator(new StandardPieSectionLabelGenerator("{0}:{1}元({2})"));
//设置是否忽略null值
plot.setIgnoreNullValues(true);
//设置是否忽略0值
plot.setIgnoreZeroValues(false);
chart = new JFreeChart("", JFreeChart.DEFAULT_TITLE_FONT, plot, true);
//可选,设置图片背景色
chart.setBackgroundPaint(Color.WHITE);
//RenderingHints做文字渲染参数的修改,VALUE_TEXT_ANTIALIAS_OFF表示将文字的抗锯齿关闭.
chart.getRenderingHints().put(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
chart.setBackgroundPaint(new Color(248,252,253));
//设置图例说明Legend上的文字字体
chart.getLegend().setItemFont(new Font("宋体", Font.PLAIN, 12));
//设置图例说明Legend上的文字颜色
chart.getLegend().setItemPaint(new Color(43,74,120));
//设置图例说明Legend的边框
chart.getLegend().setBorder(0, 0, 0, 0);
chart.getLegend().setBackgroundPaint(new Color(248,252,253));
//设置饼图的边框颜色
chart.getPlot().setOutlinePaint(Color.WHITE);
chart.getPlot().setBackgroundPaint(new Color(248,252,253));
}
return chart;
}
private PieDataset createDataset(String mobileTeleNo) throws Exception {
DefaultPieDataset data = new DefaultPieDataset();
data.setValue("月租费", 13);
data.setValue("市话费", 3.36);
data.setValue("短信费", 25.6);
data.setValue("漫游费", 38);
data.setValue("国内长途", 26.45);
data.setValue("国际长途", 10);
return data;
}