自定义的JFreeChart标题字体太小了 [英] Custom JFreeChart title font is far too small

查看:272
本文介绍了自定义的JFreeChart标题字体太小了的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用代码为 JFreeChart 中的标题设置自定义 Font

  InputStream is = new FileInputStream(test.ttf); 
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,is);
customFont.deriveFont(24f);
chart.getTitle()。setFont(customFont);

结尾的字体非常小(几乎看不见):


任何想法如何添加一个自定义字体 JFreeChart title?

  public class Function2DDemo1 extends ApplicationFrame {

public Function2DDemo1(String title){
super(title);
JPanel chartPanel = createDemoPanel();
chartPanel.setPreferredSize(new java.awt.Dimension(500,270));
setContentPane(chartPanel);


private static JFreeChart createChart(XYDataset dataset){
//创建图表...
JFreeChart图表= ChartFactory.createXYLineChart(Function2DDemo1,/ / chart
// title
X,// x轴标签
Y,// y轴标签
dataset,//数据
PlotOrientation。 VERTICAL,true,//包含图例
true,//工具提示
false //网址
);

//设置自定义标题字体
尝试{
InputStream is = new FileInputStream(test.ttf);
java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT,is);
customFont.deriveFont(24f);
chart.getTitle()。setFont(customFont);
} catch(FileNotFoundException e){
e.printStackTrace();
} catch(FontFormatException e){
e.printStackTrace();
} catch(IOException e){
e.printStackTrace();
}

XYPlot plot =(XYPlot)chart.getPlot();
plot.getDomainAxis()。setLowerMargin(0.0);
plot.getDomainAxis()。setUpperMargin(0.0);
回报图;


public static XYDataset createDataset(){
XYDataset result = DatasetUtilities.sampleFunction2D(new X2(),-4.0,4.0,40,f(x)) ;
返回结果;


public static JPanel createDemoPanel(){
JFreeChart chart = createChart(createDataset());
返回新的ChartPanel(图表);


static class X2 implements Function2D {

public double getValue(double x){
return x * x + 2;



$ b public static void main(String [] args){
Function2DDemo1 demo = new Function2DDemo1(JFreeChart:Function2DDemo1.java );
demo.pack();
RefineryUtilities.centerFrameOnScreen(demo);
demo.setVisible(true);


$ / code $ / pre

解决方案

deriveFont 方法返回您忘记存储的字体对象。将您的代码更改为:

  customFont = customFont.deriveFont(24f); 

我用这个修正测试了你的代码,使用 http://www.urbanfonts.com ,它似乎工作得很好(在Windows上)。我的猜测是,当你加载一个字体文件,默认大小是1。


I am trying to set a custom Font for the title in JFreeChart with the code:

    InputStream is = new FileInputStream("test.ttf");
    java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
    customFont.deriveFont(24f);
    chart.getTitle().setFont(customFont);

Ends up with a very small font title(almost invisible):

Any ideas how to add a custom Font to JFreeChart title?

public class Function2DDemo1 extends ApplicationFrame {

    public Function2DDemo1(String title) {
        super(title);
        JPanel chartPanel = createDemoPanel();
        chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));
        setContentPane(chartPanel);
    }

    private static JFreeChart createChart(XYDataset dataset) {
        // create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart("Function2DDemo1 ", // chart
                                                                                // title
        "X", // x axis label
        "Y", // y axis label
        dataset, // data
        PlotOrientation.VERTICAL, true, // include legend
        true, // tooltips
        false // urls
        );

        // SET A CUSTOM TITLE FONT
        try {
            InputStream is = new FileInputStream("test.ttf");
            java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
            customFont.deriveFont(24f);
            chart.getTitle().setFont(customFont);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (FontFormatException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        XYPlot plot = (XYPlot) chart.getPlot();
        plot.getDomainAxis().setLowerMargin(0.0);
        plot.getDomainAxis().setUpperMargin(0.0);
        return chart;
    }

    public static XYDataset createDataset() {
        XYDataset result = DatasetUtilities.sampleFunction2D(new X2(), -4.0, 4.0, 40, "f(x)");
        return result;
    }

    public static JPanel createDemoPanel() {
        JFreeChart chart = createChart(createDataset());
        return new ChartPanel(chart);
    }

    static class X2 implements Function2D {

        public double getValue(double x) {
            return x * x + 2;
        }

    }

    public static void main(String[] args) {
        Function2DDemo1 demo = new Function2DDemo1("JFreeChart: Function2DDemo1.java");
        demo.pack();
        RefineryUtilities.centerFrameOnScreen(demo);
        demo.setVisible(true);
    }
}

解决方案

The deriveFont method returns a Font object that you forgot to store. Change your code to:

customFont = customFont.deriveFont(24f);

I tested your code, with this fix, using a free font from http://www.urbanfonts.com and it seems to work just fine (on Windows). My guess is that when you load a font file, the default size is 1.

这篇关于自定义的JFreeChart标题字体太小了的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆