2016-07-20 12 views
0

JFreeChart 자막을 중심 제목에 왼쪽 정렬하려면 주 제목의 중심이 ChartFrame이지만 가운데 부제가 정렬되어야합니다. 제목의 왼쪽 여백까지 이 작업을 수행 할 수있는 유일한 방법은 제목과 부제를 HorizontalAlignment.LEFT으로 설정하는 것입니다. 그런 다음 프로그램에서 수동으로 제목의 왼쪽 패딩을 가운데 맞춤으로 설정 한 다음 제목과 일치하도록 자막 패딩을 설정하여 줄에 맞게 계산 된 왼쪽 여백까지 정렬합니다. 이 같은 프레임의 중앙까지 제목 :자막을 중심 제목에 왼쪽 정렬하려고하면 JFreeChart chart.getWidth()가 작동하지 않습니다.

// 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 반환하고, 그 이유를 알아낼 수 없습니다. 나는 을 AbstractBlock으로 캐스팅하려했지만 아무런 차이가 없다. 나는 그것이 JavaDoc for the getWidth() method in the AbstractBlock class에있는 사실과 관련이 있다고 믿는다. 미리 알고 있다면 너비를 반환 할 것이라고 언급한다. 분명히 알지 못한다.

getWidth() 기능을 사용하는지 여부에 관계없이 차트의 제목이 올바르게 표시되도록하는 방법을 알고 싶습니다. 패딩을 조정하는 대신 ChartFrame의면이 아닌 차트의 요소를 서로 정렬하는 더 좋은 방법이 있는지 알고 싶습니다.

크로스 게시 here.

+0

크로스 게시 [여기] (http://www.jfree.org/forum/viewtopic.php?f=3&t=117630). – trashgod

답변

1

대답하는대로 here. 나는 제목의 왼쪽 여백에 자막을 정렬하지만, 프레임의 중앙에 정렬 전체를 유지하기 위해해야 ​​할 일을했을 무엇

ColumnArrangementBlockContainer에 두 요소를 넣어, 다음 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);