如何放大gagawa图中的文字?

时间:2015-07-13 21:50:44

标签: java html css charts gagawa

我使用此代码生成gagawa图表:

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

我得到了这张图:

enter image description here

如何放大文字(x轴,y轴,图例和标题)?

2 个答案:

答案 0 :(得分:0)

可能这会对你有所帮助。

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

根据此Question

答案 1 :(得分:0)

JFreeChart (Javadoc)类似乎包含一个Font对象。它' S伤心地看到,开发商不能够默认字体的修改,但无论如何,在这里' S我是如何解决这个...

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;

   }
}

这实际上是翻译整个源代码,找到here,并用构造函数中的给定字体对象替换默认字体对象。

对于您的代码,替换

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

使用:

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

假设您已指定(customFont),应该可以解决这个问题。

作为参考,默认字体为

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

这样,整个图表通过替换默认字体使用相同的字体。 SAHIL.R2050的答案也是有效的,但它确实要求图表的每个部分都设置了字体。

我的答案很适合为所有自定义图表设置新的默认字体,而SAHIIL.R2050的答案更符合正确使用的套餐。 SAHIL.R2050的答案还允许您一次指定每个部分的大小而不是所有部分的大小。

总结一下ChartFactory / CustomChartFactory,它真正做的就是创建一个ChartTheme,然后使用它的apply()方法将它应用到图表中。

希望这有帮助。 :)