您的位置:首页 > 编程语言 > Java开发

JFreeChart饼图, java程序中加载宋体字库

2014-03-04 18:20 260 查看
项目里之前做的JFreeChart饼图被openFlashChart替掉了, 把原来JFreeChart做的饼图记录一下 不然恐怕就没了

生成图没什么技术含量,JFreeChart的资料也比openFlashChart多

就是在linux下用java加载字库得记录一下 , 用的宋体 ,默认linux中没有宋体的话 显示都是"口"..  程序里所用的宋体字体给个链接:http://download.csdn.net/detail/ruantao1989/6992169

另外各种图的配置备份一份

欢迎使用“统一用户管理系统的后台”<span style="color:gray;float:right;">(版本:<%=Const.VERSION%>)</span>
<br/>
<div style="float:left;">今日注册数:${toDayRegCount}</div>
<br/>
<img src="${userChartURL}">
<img src="${appChartURL}">


package com.XXXX.controller;

import java.awt.Color;
import java.awt.Font;
import java.io.BufferedInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.DecimalFormat;
import java.text.NumberFormat;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartUtilities;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot;
import org.jfree.chart.title.LegendTitle;
import org.jfree.chart.title.TextTitle;
import org.jfree.data.general.DefaultPieDataset;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.xxx.controller.commons.BaseController;
import com.xxx.entity.Application;
import com.xxx.entity.User;
import com.xxx.service.AnalysisRegLogService;
import com.xxx.service.ApplicationService;
import com.xxx.service.UserService;
import com.xxx.util.Const;

/**
* 饼图生成器
* 饼图具体配置参考 http://blog.csdn.net/xyw_blog/article/details/8685162 * (柱状图配置 http://blog.csdn.net/bolg_hero/article/details/8932884) * @author tao
*/
@Controller
public class ChartController extends BaseController
{
private int perUserCount;//缓存查询结果
private int comUserCount;
private int app_1Count;
private int app_2Count;

private static final String _perUserStr = "个人用户";
private static final String _comUserStr = "企业用户";
private static final String _app_1Str = "自动授权应用";
private static final String _app_2Str = "人工审核应用";

private static Font font10;//普通字体 10号
private static Font font14;//标题字体 14号

static//静态加载字体,实现linux中不安装字体 使jre显示中文
{
try
{
BufferedInputStream fb = new BufferedInputStream(ChartController.class.getResourceAsStream("/simsun.ttf"));
Font nf = Font.createFont(Font.TRUETYPE_FONT, fb);
font10 = nf.deriveFont(Font.BOLD, 10);
font14 = nf.deriveFont(Font.BOLD, 14);
} catch (Exception e) {
//TODO 抛出"配置错误"异常
e.printStackTrace();
}
}

@RequestMapping(value = "/getMajorChart")
public ModelAndView getMajorChart(HttpServletRequest request,HttpServletResponse response, ModelMap modelMap) throws Exception
{
String ctxPath = request.getSession().getServletContext().getRealPath("/")+ "chartImg/";

//今日注册用户数量
modelMap.put("toDayRegCount", analysisRegLogService.getRegCountInToday());

//用户饼图
JFreeChart userChart = makeUserChart();
outPutChart(ctxPath,"userChart",userChart,modelMap,request);// 将图片对象输出到客户端

//应用饼图
JFreeChart appChart = makeAppChart();
outPutChart(ctxPath,"appChart",appChart,modelMap,request);

return new ModelAndView("default",modelMap);
}

/**
* 输出图片
* @param ctxPath
* @param fileName
* @param chart
* @param modelMap
* @param request
* @throws IOException
*/
private void outPutChart(String ctxPath,String fileName,JFreeChart chart,ModelMap modelMap,HttpServletRequest request) throws IOException
{
OutputStream ous = new FileOutputStream(ctxPath+fileName+".png");
ChartUtilities.writeChartAsPNG(ous, chart, 400, 300);

ous.flush();
ous.close();

String chartURL= request.getContextPath()+"/chartImg/"+fileName+".png";
modelMap.put(fileName+"URL", chartURL);
}

private JFreeChart makeUserChart()
{
DefaultPieDataset dataset = getUserPieDataSet();
JFreeChart chart = ChartFactory.createPieChart("用户统计", dataset, true, true,false);
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setCircular(true); //设置饼图是圆的(true),还是椭圆的(false);默认为true
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));//以默认方式显示百分比 "{0}:{1}({2})"
pieplot.setNoDataMessage("无数据显示");//没有数据的时候显示的内容
pieplot.setLabelGap(0.02D);
//背景色
chart.setBackgroundPaint(Color.white);
pieplot.setBackgroundPaint(Color.white);
//副标题显示总数
setSubTitle(chart,"user");
//字体
setChartFont(chart);

return chart;
}
private JFreeChart makeAppChart()
{
DefaultPieDataset dataset = getAppPieDataSet();
JFreeChart chart = ChartFactory.createPieChart("应用统计", dataset, true, true,false);
PiePlot pieplot = (PiePlot) chart.getPlot();
pieplot.setCircular(true); //设置饼图是圆的(true),还是椭圆的(false);默认为true
pieplot.setLabelGenerator(new StandardPieSectionLabelGenerator("{0}({2})", NumberFormat.getNumberInstance(), new DecimalFormat("0.00%")));//以默认方式显示百分比
pieplot.setNoDataMessage("无数据显示");//没有数据的时候显示的内容
pieplot.setLabelGap(0.02D);
//背景色
chart.setBackgroundPaint(Color.white);
pieplot.setBackgroundPaint(Color.white);
//副标题显示总数
setSubTitle(chart,"app");
//字体
setChartFont(chart);

return chart;
}

/**
* 副标题显示总数
* @param chart
* @param mode
*/
private void setSubTitle(JFreeChart chart,String mode )
{
if(mode.equals("user"))
{
chart.addSubtitle(new TextTitle("总用户数:"+userService.getCountByUser(null), font10));
chart.addSubtitle(new TextTitle(_perUserStr+"总数:"+perUserCount+"  "+_comUserStr+"总数:"+comUserCount, font10));
}
else if(mode.equals("app"))
{
chart.addSubtitle(new TextTitle("总应用数:"+applicationService.getCountByAppType(null), font10));
chart.addSubtitle(new TextTitle(_app_1Str+"总数:"+app_1Count+"  "+_app_2Str+"总数:"+app_2Count, font10));
}
}

private DefaultPieDataset getUserPieDataSet()
{
DefaultPieDataset dataset = new DefaultPieDataset();

User perUser = new User();
perUser.setComOrPer(Const.PERSONAL_TYPE);
User comUser = new User();
comUser.setComOrPer(Const.COMPANY_TYPE);

perUserCount = userService.getCountByUser(perUser);
comUserCount = userService.getCountByUser(comUser);

dataset.setValue(_perUserStr,  perUserCount);
dataset.setValue(_comUserStr, comUserCount);

return dataset;
}
private DefaultPieDataset getAppPieDataSet()
{
DefaultPieDataset dataset = new DefaultPieDataset();

Application app_1 = new Application();
app_1.setCode(1000);
Application app_2 = new Application();
app_2.setCode(2000);

app_1Count = applicationService.getCountByAppType(app_1);
app_2Count = applicationService.getCountByAppType(app_2);

dataset.setValue(_app_1Str, app_1Count);
dataset.setValue(_app_2Str, app_2Count);

return dataset;
}

/**
* 设置字体,解决乱码
* @param chart
*/
private void setChartFont(JFreeChart chart)
{
//Font titleFont = new Font("宋体",Font.BOLD, 14);//表头字体
//Font songFont = new Font("宋体",Font.CENTER_BASELINE, 10);//统一图例字体
PiePlot pieplot = (PiePlot) chart.getPlot();

chart.getTitle().setFont(font14);

pieplot.setLabelFont(font10);
LegendTitle legend = chart.getLegend();
if (legend!=null)
{
legend.setItemFont(font10);
}
}

@Autowired
private UserService userService;
@Autowired
private ApplicationService applicationService;
@Autowired
private AnalysisRegLogService analysisRegLogService;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: