JFreeChart解决中文乱码问题

洼地云 tuoyidashi.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解决中文乱码问题

评论 抢沙发

合作&反馈&投稿

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

广告合作侵权联系

登录

找回密码

注册