简体   繁体   English

图表和自定义字体

[英]Charts and custom fonts

I have some JasperReports report that uses custom font (Dejavu Serif) which works fine both on development and production machines. 我有一些使用自定义字体(Dejavu Serif)的JasperReports报告,它在开发和生产机器上都能正常工作。 Today I added some chart to that report and configured all the font settings to Dejavu Serif. 今天我在该报告中添加了一些图表,并将所有字体设置配置为Dejavu Serif。 The chart itself works fine but I can't see any labels so I think the problem with custom font.. I found one post , but can't understand how they solved the problem.. 图表本身工作正常,但我看不到任何标签所以我认为自定义字体的问题..我发现一个帖子 ,但无法理解他们是如何解决问题的..

Update #1: So I implemented Customizer class which configures the font of labels: 更新#1:所以我实现了Customizer类来配置标签的字体:

public class CustomFontCategoryChartCustomizer implements JRChartCustomizer  {

    /**
     * 
     */
    @Override
    public void customize(JFreeChart chart, JRChart jasperChart) {
        CategoryPlot plot = chart.getCategoryPlot();
        Font customFont = new Font("DejaVu Serif", Font.BOLD, 12);

        // Works fine
        plot.getDomainAxis().setTickLabelFont(customFont);
        plot.getRangeAxis().setTickLabelFont(customFont);
        plot.getRangeAxis().setUpperMargin(0.5);
        // Doesn't work
        LegendItemCollection legends = plot.getLegendItems();
        for (int i = 0; i < legends.getItemCount(); i++) {
            legends.get(i).setLabelFont(customFont);
        }
        plot.getLegendItems().addAll(legends);
        // Doesn't work
        LegendItemCollection legends = plot.getLegendItems();
        for (int i = 0; i < legends.getItemCount(); i++) {
            plot.getLegendItems().get(i).setLabelFont(customFont);
        }
    }
}

Everything fine except of legend labels.. Can't make it work with custom font.. Any help? 一切都很好,除了传说标签..无法使其与自定义字体一起工作..任何帮助?

Nobody answered, so I'll dot it by myself... In order to use custom fonts in JasperReports's charts you should do the following: 没有人回答,所以我会自己点一下......为了在JasperReports的图表中使用自定义字体,您应该执行以下操作:

  1. Overload the JRChartCustomizer's customize method as showed: 重载JRChartCustomizer的customize方法,如下所示:

     public class CustomFontCategoryChartCustomizer implements JRChartCustomizer { @Override public void customize(JFreeChart chart, JRChart jasperChart) { Font customFont = new Font("DejaVu Serif", Font.PLAIN, 9); CategoryPlot plot = chart.getCategoryPlot(); // Set legend's font chart.getLegend().setItemFont(customFont); // Set categories label's font plot.getDomainAxis().setTickLabelFont(customFont); // Set amounts label's font plot.getRangeAxis().setTickLabelFont(customFont); } } 
  2. Add reference to iReport to your JRChartCustomizer implementation or to jar file JRChartCustomizer iReport的引用添加到JRChartCustomizer实现或jar文件中

  3. Configure Customizer Class option inside iReport's chart object with apapropriate class name. 使用适当的类名在iReport的图表对象中配置Customizer Class选项。 In my case with CustomFontCategoryChartCustomizer 在我的情况下使用CustomFontCategoryChartCustomizer

That's it. 而已。

public class CustomFontCategoryChartCustomizer implements JRChartCustomizer  {
@Override
public void customize(JFreeChart chart, JRChart jasperChart) {
    Font customFont = getOpenSansFont("OpenSans-Regular.ttf")
    CategoryPlot plot = chart.getCategoryPlot();

    // Set legend's font        
    chart.getLegend().setItemFont(customFont);
    // Set categories label's font
    plot.getDomainAxis().setTickLabelFont(customFont);
    // Set amounts label's font 
    plot.getRangeAxis().setTickLabelFont(customFont);
  }

 private Font getOpenSansFont(String ttfFont) {
    try {
        String openSansAbsolutePath = "absolute/path/to/font"
        Font font = Font.createFont(Font.TRUETYPE_FONT, new File(openSansAbsolutePath));
        return font.deriveFont(10f);
    } catch (IOException | FontFormatException e) {
        println "Exception occur during create font." + e
    }
  }
}

You need to put ttf font in you application class path. 您需要将ttf字体放在应用程序类路径中。 It worked perfectly for me. 它对我来说很完美。 I had used it one of my project. 我曾经用过我的一个项目。

Happy Coding. 快乐的编码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM