JFreeChart chart.getWidth() 在尝试 left-align 字幕居中时不起作用
JFreeChart chart.getWidth() not working when trying to left-align subtitle to centered title
我正在尝试 left-align 一个 JFreeChart
副标题居中的标题,这样主标题在 ChartFrame
中居中,但副标题与 ChartFrame
的左边距对齐标题。我能想到的唯一方法是将标题和副标题设置为 HorizontalAlignment.LEFT
。然后我让程序手动设置标题的左填充,使其居中,然后设置字幕填充以匹配标题的填充,从而将它们排列成相同的 left-margin 计算为将标题对齐到框架的中心,如下所示:
// 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()
方法每次都是 returning 0.0
,我不明白为什么。我试过将 chart.getTitle()
转换为 AbstractBlock
但这没有区别。我相信这与 JavaDoc for the getWidth()
method in the AbstractBlock
class 中的事实有关,它提到如果它事先知道宽度,它将 return 宽度,显然它不知道。
我想知道如何使图表标题正确 return 其宽度,无论是否使用 getWidth()
函数。我还想知道是否有更好的方法将图表的元素彼此对齐,而不是 ChartFrame
的两侧而不是调整它们的填充。
交叉发布 here.
如回答here。
我必须做的是将字幕与标题的左边距对齐,但要使整个内容与框架的中心对齐,就是将两个元素都放入 BlockContainer
和 ColumnArrangement
,然后从 BlockContainer
创建一个 CompositeTitle
,并相应地对齐 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);
我正在尝试 left-align 一个 JFreeChart
副标题居中的标题,这样主标题在 ChartFrame
中居中,但副标题与 ChartFrame
的左边距对齐标题。我能想到的唯一方法是将标题和副标题设置为 HorizontalAlignment.LEFT
。然后我让程序手动设置标题的左填充,使其居中,然后设置字幕填充以匹配标题的填充,从而将它们排列成相同的 left-margin 计算为将标题对齐到框架的中心,如下所示:
// 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()
方法每次都是 returning 0.0
,我不明白为什么。我试过将 chart.getTitle()
转换为 AbstractBlock
但这没有区别。我相信这与 JavaDoc for the getWidth()
method in the AbstractBlock
class 中的事实有关,它提到如果它事先知道宽度,它将 return 宽度,显然它不知道。
我想知道如何使图表标题正确 return 其宽度,无论是否使用 getWidth()
函数。我还想知道是否有更好的方法将图表的元素彼此对齐,而不是 ChartFrame
的两侧而不是调整它们的填充。
交叉发布 here.
如回答here。
我必须做的是将字幕与标题的左边距对齐,但要使整个内容与框架的中心对齐,就是将两个元素都放入 BlockContainer
和 ColumnArrangement
,然后从 BlockContainer
创建一个 CompositeTitle
,并相应地对齐 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);