3

I use this code to generate gagawa chart:

private Img createChart(LatencyHistogram current, LatencyHistogram baseLine) {

    Img image = null;
    final CategoryDataset dataset = fillDataSet(current, baseLine);

    final JFreeChart chart = ChartFactory.createBarChart(
            "Latecny histogram",       // chart title
            "Type",                    // domain axis label
            "Value",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
    );
    chart.setBackgroundPaint(java.awt.Color.white);
    // save it to an image
    try {
        final ChartRenderingInfo info = new ChartRenderingInfo(new StandardEntityCollection());

        String latency_graph = Constants.LATENCY_GRAPH_NAME;
        final String pathname = Constants.HTML_PAGES_PATH + "images/"+latency_graph;
        final File file1 = new File(pathname);
        ChartUtilities.saveChartAsPNG(file1, chart, 2200, 1000, info);
        image = new Img("latency", "../images/"+latency_graph).setWidth("1300");
        return image;
    } catch (IOException e) {
        e.printStackTrace();
    }
    return image;
}

and I get this graph:

enter image description here

how can I enlarge the text (x- axis, y-axis, legend and title) ?

Elad Benda
  • 35,076
  • 87
  • 265
  • 471

2 Answers2

0

May be this will help you.

CategoryPlot plot = chart.getCategoryPlot();
CategoryAxis axis = plot.getDomainAxis();

CategoryPlot p = chart.getCategoryPlot(); 
ValueAxis axis2 = p.getRangeAxis();

Font font = new Font("Dialog", Font.PLAIN, 25);
axis.setTickLabelFont(font);
Font font2 = new Font("Dialog", Font.PLAIN, 15);
axis2.setTickLabelFont(font2);

Font font3 = new Font("Dialog", Font.PLAIN, 25); 
plot.getDomainAxis().setLabelFont(font3);
plot.getRangeAxis().setLabelFont(font3);

According to this Question

Community
  • 1
  • 1
Sahil Patel
  • 1,570
  • 2
  • 15
  • 34
0

It appears that the JFreeChart (Javadoc) class contains a Font object. It's sad to see the developers not enabling the modification of the font by default, but anyway here's how I solved this...

class CustomChartFactory extends ChartFactory{ 
     public static JFreeChart createCustomBarChart(String title,
            String categoryAxisLabel, String valueAxisLabel,
            CategoryDataset dataset, PlotOrientation orientation,
            boolean legend, boolean tooltips, boolean urls, Font font) {

        ParamChecks.nullNotPermitted(orientation, "orientation");
        CategoryAxis categoryAxis = new CategoryAxis(categoryAxisLabel);
        ValueAxis valueAxis = new NumberAxis(valueAxisLabel);

         BarRenderer renderer = new BarRenderer();
        if (orientation == PlotOrientation.HORIZONTAL) {
            ItemLabelPosition position1 = new ItemLabelPosition(
                    ItemLabelAnchor.OUTSIDE3, TextAnchor.CENTER_LEFT);
            renderer.setBasePositiveItemLabelPosition(position1);
            ItemLabelPosition position2 = new ItemLabelPosition(
                    ItemLabelAnchor.OUTSIDE9, TextAnchor.CENTER_RIGHT);
            renderer.setBaseNegativeItemLabelPosition(position2);
        } else if (orientation == PlotOrientation.VERTICAL) {
            ItemLabelPosition position1 = new ItemLabelPosition(
                    ItemLabelAnchor.OUTSIDE12, TextAnchor.BOTTOM_CENTER);
            renderer.setBasePositiveItemLabelPosition(position1);
            ItemLabelPosition position2 = new ItemLabelPosition(
                    ItemLabelAnchor.OUTSIDE6, TextAnchor.TOP_CENTER);
            renderer.setBaseNegativeItemLabelPosition(position2);
        }
        if (tooltips) {
            renderer.setBaseToolTipGenerator(
                    new StandardCategoryToolTipGenerator());
        }
        if (urls) {
            renderer.setBaseItemURLGenerator(
                    new StandardCategoryURLGenerator());
        }

        CategoryPlot plot = new CategoryPlot(dataset, categoryAxis, valueAxis,
                renderer);
        plot.setOrientation(orientation);
        JFreeChart chart = new JFreeChart(title, font,
                plot, legend);
        currentTheme.apply(chart);
       return chart;

   }
}

This is literally ripping their entire source code, found here, and replacing the default font object with the given font object from the constructor.

For your code, replacing

final JFreeChart chart = ChartFactory.createBarChart(
            "Latecny histogram",       // chart title
            "Type",                    // domain axis label
            "Value",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false                      // urls
    ); 

with:

Font customFont = new Font("SansSerif", Font.BOLD, 25);
final JFreeChart chart = CustomChartFactory.createBarChart(
            "Latecny histogram",       // chart title
            "Type",                    // domain axis label
            "Value",                   // range axis label
            dataset,                   // data
            PlotOrientation.VERTICAL,  // orientation
            true,                      // include legend
            true,                      // tooltips
            false,                     // urls
            customFont                 //font
);

Should do the trick, assuming you have specified (customFont).

For reference, the default font is

Static Final Font DEFAULT_TITLE_FONT = new Font("SansSerif", Font.BOLD, 18);

This way, the entire chart uses the same font by essentially replacing the default font. SAHIL.R2050's answer is also valid, though it does require that each individual section of the chart have their fonts set.

My answer is good for setting a new default font for all custom charts to use, whereas SAHIIL.R2050's answer is more in line with proper usage of the package. SAHIL.R2050's answer also allows you to specify the sizes of each section rather than all of them at once.

To sum up the ChartFactory / CustomChartFactory, all it really does is create a ChartTheme then apply it to the chart using it's apply() method.

Hope this helped. :)