JFreeChart 中文乱码解决(不用添加iTextAsian.jar)

JFreeChart 中文乱码主要是缺少字体,可将其字体设置为系统存在的字体,乱码即可解决:

    public static java.awt.Font getFont(){
		java.awt.Font font;
		try {
			File fontFile = ResourceUtils.getFile("classpath:font/simsun.ttc");//字体文件要放在class path的font文件夹下
			GraphicsEnvironment gEnv = GraphicsEnvironment.getLocalGraphicsEnvironment();
			String[] evnfonts = gEnv.getAvailableFontFamilyNames();
			List<String> list = Arrays.asList(evnfonts);			
			if (!list.contains("宋体")) {//系统没有安装宋体,用字体文件创建一个
				font = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, fontFile);				
			} else {
				font = new java.awt.Font("宋体", java.awt.Font.PLAIN, 12);//读取系统字体
			}
			return font;
		} catch (FileNotFoundException e) {
			log.error("The font file is not exist! Please add the simsun font file to 'classpath:font/simsun.ttc'.");
		} catch (Exception e) {
			log.error("Error occured when creating simsun font.");
		}
		return null;		
    }

 设置JFreeChart 字体:

 

    public static JFreeChart createBarChart3D(DefaultCategoryDataset dataset,String title, String xAxisLabel, String yAxisLabel) {
		JFreeChart chart = ChartFactory.createBarChart3D(title, xAxisLabel, yAxisLabel, dataset, PlotOrientation.VERTICAL, false,
				false, false);
		CategoryPlot plot = chart.getCategoryPlot();	
		chart.setBackgroundPaint(Color.WHITE);

		BarRenderer3D renderer = new BarRenderer3D();
		renderer.setBaseOutlinePaint(Color.BLACK);
		renderer.setMaximumBarWidth(0.1);
		renderer.setSeriesPaint(0, new Color(20, 180, 20));

		renderer.setItemMargin(0.1);
		renderer.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator());
		renderer.setItemLabelPaint(Color.BLACK);
		renderer.setItemLabelsVisible(true);
		renderer.setBaseItemLabelsVisible(true);
		renderer.setBasePositiveItemLabelPosition(new ItemLabelPosition(ItemLabelAnchor.OUTSIDE12, TextAnchor.BASELINE_LEFT));
		plot.setRenderer(renderer);
		plot.setForegroundAlpha(0.8f);
		Font font = FontUtils.getFont();
		if(font != null){
			plot.getDomainAxis().setLabelFont(font);
			plot.getDomainAxis().setTickLabelFont(font);
			plot.getRangeAxis().setLabelFont(font);
			plot.getRangeAxis().setTickLabelFont(font);
			Font titleFont = font.deriveFont(Font.BOLD,22);		
			chart.getTitle().setFont(titleFont);
		}	
		return chart;
    }
 

simsun.ttc 字体的下载见:(文章的附件)

PDF iText 使用简单介绍(附中文乱码解决方案)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值