Java JFreeChart chart.getWidth()在尝试将副标题与居中标题左对齐时不起作用
我正在尝试将Java JFreeChart chart.getWidth()在尝试将副标题与居中标题左对齐时不起作用,java,charts,jfreechart,appearance,Java,Charts,Jfreechart,Appearance,我正在尝试将JFreeChart副标题与居中标题左对齐,以便主标题在ChartFrame中居中,但副标题与标题的左边距对齐。我能想到的唯一方法是将标题和副标题设置为水平对齐。然后,我会让程序手动设置标题的左边距,使其居中,然后设置字幕边距以匹配标题的左边距,从而将它们排列到相同的左边距,该边距计算为将标题排列到帧的中心,如下所示: // Make the chart JFreeChart chart = ChartFactory.createTimeSeriesChart(title, "Tim
JFreeChart
副标题与居中标题左对齐,以便主标题在ChartFrame
中居中,但副标题与标题的左边距对齐。我能想到的唯一方法是将标题和副标题设置为水平对齐。然后,我会让程序手动设置标题的左边距,使其居中,然后设置字幕边距以匹配标题的左边距,从而将它们排列到相同的左边距,该边距计算为将标题排列到帧的中心,如下所示:
// 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
,但这没有什么区别。我相信这与这样一个事实有关,在中,它提到如果它事先知道宽度,它将返回宽度,但显然它没有
我想知道如何让图表标题正确返回其宽度,无论是否使用getWidth()
函数。我还想知道是否有更好的方法将图表的元素彼此对齐,而不是调整其填充
交叉张贴。如回答所示
我要做的是将字幕与标题的左边距对齐,但将整个内容与框架的中心对齐,就是将这两个元素放入具有列排列的块容器
,然后从块容器
中创建一个合成文件
,并相应地对齐该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);
交叉张贴。