Grains
Grains

Reputation: 950

Custom JFreeChart title font is far too small

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):

enter image description here

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

Upvotes: 1

Views: 1132

Answers (1)

Duncan Jones
Duncan Jones

Reputation: 69339

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.

Upvotes: 2

Related Questions