gpt4 book ai didi

java - 图表和自定义字体

转载 作者:行者123 更新时间:2023-12-02 05:38:02 26 4
gpt4 key购买 nike

我有一些使用自定义字体(Dejavu Serif)的 JasperReports 报告,它在开发和生产机器上都可以正常工作。今天,我向该报告添加了一些图表,并将所有字体设置配置为 Dejavu Serif。图表本身工作正常,但我看不到任何标签,所以我认为自定义字体有问题。我找到了一个 post ,但不明白他们是如何解决这个问题的..

更新#1:所以我实现了定制器类来配置标签的字体:

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);
}
}
}

除了图例标签之外一切都很好..无法使其与自定义字体一起使用..有什么帮助吗?

最佳答案

没有人回答,所以我自己点一下...为了在 JasperReports 的图表中使用自定义字体,您应该执行以下操作:

  1. 重载 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. 将对 iReport 的引用添加到您的 JRChartCustomizer 实现或 jar 文件

  3. 使用适当的类名在 iReport 的图表对象内配置Customizer Class 选项。就我而言,使用 CustomFontCategoryChartCustomizer

就是这样。

关于java - 图表和自定义字体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23633489/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com