2013-11-14 5 views
2

가로 막대 차트 카테고리 라벨의 텍스트 정렬을 변경하려면 :, 어떻게 나는 다음과 같은 표현이 차트에서 일하고 있어요

enter image description here

을 ¿ 어떻게 분류 라벨 왼쪽해야합니까 정렬되었거나 정당화 되었습니까? 실제로 그들은 중심으로 보입니다. 그러나 그것은 내가 필요로하는 것이 아닙니다.

JFreeChart chart = getChart(); 
CategoryPlot plot = (CategoryPlot) chart.getPlot(); 

BarRenderer renderer = (BarRenderer) plot.getRenderer(); 
renderer.setDrawBarOutline(true); 

CategoryAxis domainAxis = plot.getDomainAxis(); 
domainAxis.setMaximumCategoryLabelLines(5); 

CategoryLabelPositions p = domainAxis.getCategoryLabelPositions(); 

CategoryLabelPosition left = new CategoryLabelPosition(
    RectangleAnchor.LEFT, TextBlockAnchor.CENTER_LEFT, 
    TextAnchor.CENTER_LEFT, 0.0, 
    CategoryLabelWidthType.RANGE, 0.70f //Assign 70% of space for category labels 
); 

domainAxis.setCategoryLabelPositions(CategoryLabelPositions 
     .replaceLeftPosition(p, left)); 

NumberAxis rangeAxis = (NumberAxis) plot.getRangeAxis(); 
rangeAxis.setStandardTickUnits(NumberAxis.createIntegerTickUnits()); 

BasicStroke stroke = new BasicStroke(1); 
plot.setDomainGridlinePaint(Color.black); 
plot.setDomainGridlineStroke(stroke); 
plot.setRangeGridlinePaint(Color.black); 
plot.setRangeGridlineStroke(stroke); 

CategoryDataset cd = plot.getDataset(); 

setBarColors(renderer, plot, cd); 

감사 : 내가 사용

코드는 다음과 같다.

답변

2

카테고리 라벨이 setLineAlignment(HorizontalAlignment alignment) 방법을 가지고있는 org.jfree.text.TextBlock의 인스턴스입니다,하지만 난 CategoryPlot 또는 CategoryAxis의 API를 통해 그들에게 얻을 수있는 방법을 확인할 수 없습니다 (이 방법과 캐스트의 톤, 그래서 내가 할 수있는 분명히 그런 방법이 존재하지 않는다는 것을 당신에게 말하지 마라, 나는 그것을 발견하지 못했다.) 하지만 메서드를 CategoryAxis에 재정 의하여 정렬 작업을 설정합니다.

(코드는 도메인 축 얻을 전에) : 당신은 데이비드 길버트가 결정적이 대답하는 가장 좋은 사람이 될 것 같은 JFreeChart Forums 또는 JFreeChart Feature Requests에 요청을 게시 할 수 있습니다

plot.setDomainAxis(new CategoryAxis() { 
    @Override 
    protected TextBlock createLabel(Comparable category, float width, RectangleEdge edge, Graphics2D g2) { 
     TextBlock label = TextUtilities.createTextBlock(category.toString(), getTickLabelFont(category), getTickLabelPaint(category), width, this.getMaximumCategoryLabelLines(), new G2TextMeasurer(g2)); 
     label.setLineAlignment(HorizontalAlignment.LEFT); 
     return label; 
    } 
}); 

합니다.

+0

레이블 텍스트가 왼쪽으로 정렬됩니다. 올바른 것입니다. 이제는 왼쪽면 정렬을 해결해야합니다. Zebby에게 고마워요. – dovahkiin

+0

왼쪽면 정렬은 위 코드에서 이미 사용 된 CategoryLabelPositions로 수행됩니다. – dovahkiin

관련 문제