java - 如何放大gagawa图中的文字?

标签 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 轴、图例和标题)?

最佳答案

看来 JFreeChart (Javadoc)类包含一个字体对象。很遗憾看到开发人员默认情况下不启用字体修改,但无论如何我是这样解决这个问题的……

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() 方法将它应用于图表。

希望这对您有所帮助。 :)

关于java - 如何放大gagawa图中的文字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31394113/

相关文章:

html - 内联显示两个框

html - 如何在台式机上使用 Chrome 测试我的响应式网站的屏幕宽度是否低于 500 像素?

html - 中心导航菜单文本

JavaScript removeClass 不适用于 bootstrap-select

java - 执行 war 文件时 jetty-runner-9.2.13.v20150730.jar 中没有主要 list 属性

java - 在网络应用程序中检测用户区域的可靠方法是什么?

java - 在 Java GUI 中延迟操作的任何方法

java - where子句中的未知列错误,使用MySQL和Java

javascript - 如何关闭模态?

html - HTML5 和 CSS3 中类似杂志的水平滚动