Adding custom font JFreeChart

2019-07-06 17:21发布

问题:

I am trying to add the following Font to my JFreeChart title: http://www.urbanfonts.com/fonts/Back_to_Bay_6.htm

Trying to achieve this with the code:

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

Ends up with a normal font:

Any ideas why? Is it possible it has something to do that I am running Mac?

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("backtobay.ttf");
            java.awt.Font customFont = java.awt.Font.createFont(java.awt.Font.TRUETYPE_FONT, is);
            customFont = customFont.deriveFont(24f);
            chart.getTitle().setFont(customFont);
            // This prints "Back to Bay 6"
            System.out.println(customFont.getFontName());
        } 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);
    }
}

回答1:

I'm getting the expected result for many older, well-behaved fonts, as shown below. There are a lot of ways for this to go awry. For example, I'm getting java.awt.FontFormatException: Font name not found in createFont() for a lot of the newer .ttf fonts in /Library/Fonts. You might try to validate the font in another context.

As tested:

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import javax.swing.JPanel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.data.function.Function2D;
import org.jfree.data.general.DatasetUtilities;
import org.jfree.data.xy.XYDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

public class Function2DDemo1 extends ApplicationFrame {

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

    private static JFreeChart createChart(XYDataset dataset) {
        // create the chart...
        JFreeChart chart = ChartFactory.createXYLineChart("Function2DDemo1 ",
        "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 {
            File f = new File("/Library/Fonts/Microsoft/Perpetua.ttf");
            Font customFont = Font.createFont(Font.TRUETYPE_FONT, f);
            customFont = customFont.deriveFont(36f);
            chart.getTitle().setFont(customFont);
            System.out.println(customFont.getFontName());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (FontFormatException | 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) {
        EventQueue.invokeLater(new Runnable() {

            @Override
            public void run() {
                Function2DDemo1 demo = new Function2DDemo1("JFreeChart: Function2DDemo1.java");
                demo.pack();
                RefineryUtilities.centerFrameOnScreen(demo);
                demo.setVisible(true);
            }
        });
    }
}