JFreeChart解决中文乱码问题

realnode.png vps-under-title.png 科学上网教程 | 机场推荐 | 云深不知处(机场)

jfreechart-chinese-luanma.png

比如柱状图,柱状图的柱子说明文字中文情况下乱码,解决代码如下:

// JFreeChart主题设置,解决中文乱码
StandardChartTheme standardChartTheme = new StandardChartTheme("CN");//这里的"name"参数;是什么意思我也不知道,反正这样可以用
standardChartTheme.setLargeFont(new java.awt.Font("楷体", Font.BOLD, 12));//可以改变轴向的字体
standardChartTheme.setRegularFont(new java.awt.Font("宋体", Font.BOLD, 12));//可以改变图例的字体
standardChartTheme.setExtraLargeFont(new java.awt.Font("隶书", Font.BOLD, 12));//可以改变图标的标题字体
ChartFactory.setChartTheme(standardChartTheme);//Theme.getTheme()

DefaultCategoryDataset dataset=new DefaultCategoryDataset();
dataset.setValue(10,"a","管理人员");
dataset.setValue(20,"b","市场人员");
dataset.setValue(40,"c","开发人员");
dataset.setValue(15,"d","其他人员");
JFreeChart chart = ChartFactory.createBarChart("年度单位整体落实情况排名", null, null, dataset, PlotOrientation.VERTICAL, false, false, false);
chart.getTitle().setFont(new java.awt.Font("隶书", Font.BOLD, 26));//设置title标题
chart.setBackgroundPaint(new Color(238, 238, 255));//设置背景色

CategoryPlot categoryplot = (CategoryPlot) chart.getPlot();//图本身
CategoryAxis categoryaxis = categoryplot.getDomainAxis();
categoryaxis.setLabelFont(new java.awt.Font("黑体", Font.ITALIC, 18)); // 设置时字体(横轴)


//将freechart柱状图添加到pdf文档中
ChartUtilities.saveChartAsPNG(new File(reportImgPath + parameters.get("srYear")  + ".png"), chart, 800, 500);
Image image = Image.getInstance(reportImgPath + parameters.get("srYear") + ".png");
image.setAlignment(Image.ALIGN_CENTER);
image.scaleAbsolute(600, 300);
document.add(image);

上面的代码加上主题设置一段代码中文乱码就解决了。

赞(0)
未经允许禁止转载:优米格 » JFreeChart解决中文乱码问题

评论 抢沙发

合作&反馈&投稿

商务合作、问题反馈、投稿,欢迎联系

广告合作侵权联系

登录

找回密码

注册

Ads Blocker Image Powered by Code Help Pro

检测到广告已被屏蔽

尊敬的优米格用户,

您好,系统检测到您使用了广告屏蔽组件。本网站并不包含任何侵入性广告,如果本站能给您一点帮助,还请将本站加入拦截白名单,这是我们仅有的收入来源!

网站的持续运作和改进,需要您的慷概帮助,感谢您的支持。

此致,
优米格 团队