JFreeChart饼图, java程序中加载宋体字库
项目里之前做的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); } }@Autowiredprivate UserService userService;@Autowiredprivate ApplicationService applicationService;@Autowiredprivate AnalysisRegLogService analysisRegLogService;
}
JFreeChart饼图, java程序中加载宋体字库相关推荐
- JAVA入门级教学之(JAVA程序的加载和运行)
JAVA程序的加载和运行 多思考多动脑(边参考文章最后的示意图,边按步骤理解) 1.JAVA程序的加载和运行包括两个非常重要的阶段: 编译阶段 运行阶段 2.我们先来了解一下什么是编译阶段: 首先,我 ...
- 在 ArcGIS Engine 应用程序中加载搜狗地图和谷歌地图。
在ArcGIS Engine 应用程序中加载搜狗地图和谷歌地图. Adding sogou map or google map in ArcGIS Engine application WebmapL ...
- 在ArcGIS Engine 应用程序中加载搜狗地图和谷歌地图
在ArcGIS Engine 应用程序中加载搜狗地图和谷歌地图. Adding sogou map or google map in ArcGIS Engine application WebmapL ...
- android加载声音文件,Android是在应用程序中加载和播放声音的最快方式
我正在开展一个项目,我必须在一个活动中加载6种不同的声音,并在按钮点击时播放所有声音.声音文件不是那么大,但问题是它们可能会更多.所以我的问题是这是在单个活动中加载声音文件的最快方法.出于测试目的,我 ...
- java程序动态加载jar包,并调用其中的方法
再编写Java应用程序的时候我们通常需要动态的加载jar,具体的代码如下: demo1.jar中的invoke方法: package com.amx.test; public class Test ...
- css文件无法应用,无法在dash应用程序中加载静态css文件
我已经构建了一个单页的dash应用程序,当作为单个文件运行时,它可以按预期运行,但是当我试图将它作为一个完整的应用程序运行时,CSS无法正确加载.在 下面是我的文件夹结构 当我通过manage.py加 ...
- cocos2dx 3.10如何把cocosstudio中的散图合图并且能在程序中加载plist使用
在项目收尾阶段,所有工程使用的还都是散图,这个时候为了降低加载和drawcall就必须要合图了,但是我们使用texturepacker合成的plist即使加载后,cocosstudio也读取不到纹理, ...
- 如何在微信小程序中加载自己的地图数据
由于微信小程序无法进行DOM操作,导致像openlayers.leaflet这种常用的js库无法在微信小程序内使用,导致加载高德.百度.mapbox还有自定义的瓦片地图数据变得很困难. 目前,大多数情 ...
- Joomla!程序中加载JS和CSS的方法
1.加载单独的js文件: $js = JURI::base().'components/com_foobar/assets/script.js'; $document =& JFactory: ...
最新文章
- 菜鸟requireJS教程---1、初识requirejs
- 各类电脑高效率神器使用及下载地址
- mysql的主从项目经验_mysql5.5主从经验分享
- 推荐系统的发展与简单回顾
- 关于Linux的修复(重新引导)
- 集存款(复利单利)贷款为一体的计算器(最新版)
- 将Mac OS X从Snow Leopard升级到Mountain Lion
- Redis张工的set存储结构(实现)原理
- 使用Java和Spring构建现代Web应用程序
- wordpress雪花下雪WP Snow Effect插件
- android+模拟器上gdb,使用gdb在Android Emulator中进行调试c程序
- T+T+.....+T的递归下降子程序
- micropython教程nucleo-f767zi开发板_Micropython教程之TPYBoard开发板制作电子时钟(萝卜学科编程教育)...
- S9300 STP 配置规范
- 互联网架构技术干货视频分享地址发布和情况说明
- 小明的烦恼 详解(C++)
- ubuntu LVM
- linux系统资源信息监控
- 第七篇,STM32串口通信编程
- 视频教程-JSP+Servlet实战视频课程-Java