尝试将字幕左对齐到居中的标题

时间:2016-07-20 15:23:37

标签: java charts jfreechart appearance

我正在尝试将JFreeChart字幕左对齐到居中的标题,以便主标题位于ChartFrame的中心,但字幕与标题的左边距对齐。我能想到的唯一方法是将标题和副标题设置为HorizontalAlignment.LEFT。然后,我让程序手动设置标题的左边距,使其居中,然后设置字幕填充以匹配标题,从而将它们排列到相同的左边距,计算到将标题排到框架的中心,如下所示:

// Make the chart
JFreeChart chart = ChartFactory.createTimeSeriesChart(title, "Time (Hour)", "Vehicles Parked", dataset, true, true, false);

ChartFrame frame = new ChartFrame("Chart", chart);
frame.pack();
frame.setVisible(true);

chart.getTitle().setHorizontalAlignment(HorizontalAlignment.LEFT);
chart.getTitle().setPadding(0, (frame.getWidth()/2)-(chart.getTitle().getWidth()/2), 0, 0);

TextTitle subtitle1 = new TextTitle(
        "This is a test subtitle in which I would like\nthe subtitle to be lined up to the title", // text
        chart.getTitle().getFont().deriveFont(chart.getTitle().getFont().getSize() * 0.6f), // font (shrunk title)
        chart.getTitle().getPaint(), // paint
        RectangleEdge.TOP, // position
        HorizontalAlignment.LEFT, //chart2.getTitle().DEFAULT_HORIZONTAL_ALIGNMENT, // horizontal alignment
        VerticalAlignment.BOTTOM, // vertical alignment
        chart.getTitle().getPadding() // padding
);
chart.addSubtitle(subtitle1);

尝试执行此操作时,chart.getTitle().getWidth()方法每次都会返回0.0,我无法弄清楚原因。我尝试将chart.getTitle()投射到AbstractBlock,但这没有任何区别。我认为它与JavaDoc for the getWidth() method in the AbstractBlock class中的事实有关,它提到如果事先知道它会返回宽度,显然它不会。

我想知道如何使图表标题正确返回其宽度,无论是否使用getWidth()函数。我还想知道是否有更好的方法将图表的元素彼此对齐而不是ChartFrame的边,而不是调整它们的填充。

交叉发布here

1 个答案:

答案 0 :(得分:1)

回答here

我需要做的是将字幕与标题的左边距对齐,但保持整个事物与框架的中心对齐是将两个元素放入BlockContainer ColumnArrangement ,然后从CompositeTitle创建BlockContainer,并相应地对齐CompositeTitle

JFreeChart chart = ChartFactory.createXYLineChart(
        "Has to have a wider title than subtitle", // chart title
        "X", // x axis label
        "Y", // y axis label
        dataset, // data
        PlotOrientation.VERTICAL,
        true, // include legend
        true, // tooltips
        false // urls
);

String subtitleText = "This is a test subtitle\nIt is also a test of whether or not newlines work";

TextTitle subtitle = new TextTitle(
        subtitleText, // text
        chart.getTitle().getFont().deriveFont(chart.getTitle().getFont().getSize() * 0.6f), // font (shrunk title)
        chart.getTitle().getPaint(), // paint
        RectangleEdge.TOP, // position
        HorizontalAlignment.LEFT, //chart2.getTitle().DEFAULT_HORIZONTAL_ALIGNMENT, // horizontal alignment
        VerticalAlignment.BOTTOM, // vertical alignment
        chart.getTitle().getPadding() // padding
);

BlockContainer blockContainer = new BlockContainer(new ColumnArrangement(HorizontalAlignment.LEFT, VerticalAlignment.TOP, 0, 0));
blockContainer.add(chart.getTitle());
blockContainer.add(subtitle);
CompositeTitle compositeTitle = new CompositeTitle(blockContainer);
compositeTitle.setPosition(RectangleEdge.TOP);
compositeTitle.setVerticalAlignment(VerticalAlignment.CENTER);
chart.getTitle().setVisible(false);
chart.addSubtitle(compositeTitle);

ChartFrame frame = new ChartFrame("Frame", chart);
frame.pack();
frame.setVisible(true);