2013-05-05 3 views
1

최대 값이 고정 값일 때 barchart의 막대 바깥에있는 모든 bartitle을 볼 수 있습니까?JFreeChart의 마진 플롯 및 차트

어떻게 보이고 어떻게 보이게하는지 예가 나와 있습니다. 줄거리와 모든 레이블을 할 수있는 그래프 사이의 마진을 만들기 위해

JFreechart with and without margins

어떤 조언, 아이디어 가능 어쩌면 키우면가있는 경우 : 750 이상의 모든 값 (미국, 마지막 행을 확인) 안 보이는 (또는 다른 해결책)?

답변

1

업데이트 : 빈 여백 (범위 축에 아무런 값) ​​확인

, 그래서 나는 NumberAxis와 주변의 setUpperBound()을 유지하지만, 엉망 수 있습니다.

그리고 여기 완전한 코드 : sscce]의

import java.awt.Graphics2D; 
import java.awt.geom.Rectangle2D; 

import org.jfree.chart.*; 
import org.jfree.chart.axis.*; 
import org.jfree.chart.labels.StandardCategoryItemLabelGenerator; 
import org.jfree.chart.plot.*; 
import org.jfree.chart.renderer.category.CategoryItemRenderer; 
import org.jfree.data.category.*; 
import org.jfree.ui.*; 

@SuppressWarnings("serial") 
public class BarWithMargin extends ApplicationFrame { 
    public BarWithMargin(final String title) { 
     super(title); 
     final JFreeChart chart = constructChart(); 
     final ChartPanel panel = new ChartPanel(chart); 
     setContentPane(panel); 
    } 


    JFreeChart constructChart() { 
     JFreeChart chart = ChartFactory.createBarChart(
      "","","",getDataSet(),PlotOrientation.HORIZONTAL,false,false,false 
     ); 

     CategoryPlot cp = chart.getCategoryPlot(); 

     CategoryItemRenderer cir = cp.getRenderer(); 
     cir.setBaseItemLabelGenerator(new StandardCategoryItemLabelGenerator()); 
     cir.setBaseItemLabelsVisible(true); 

     final double upperMargin = 80; 
     final double maxOfDataset = 750; 
     final double upperLimit = maxOfDataset+upperMargin; 

     ValueAxis va = new NumberAxis(){ 
      protected void drawAxisLine(Graphics2D g2, double cursor, Rectangle2D dataArea, RectangleEdge edge) { 
       int margin = (int)(dataArea.getWidth() * upperMargin/upperLimit); 

       Rectangle2D newArea = new Rectangle2D.Double(
        dataArea.getX(), dataArea.getY(), dataArea.getWidth()-margin, dataArea.getHeight()); 
       super.drawAxisLine(g2, cursor, newArea, edge); 
      } 

      protected int calculateVisibleTickCount() { 
       return (int)Math.ceil(super.calculateVisibleTickCount() * maxOfDataset/upperLimit); 
      } 
     }; 

     va.setLabel(cp.getRangeAxis().getLabel()); 
     va.setUpperBound(upperLimit); 
     cir.getPlot().setRangeAxis(va); 

     return chart; 
    } 

    private CategoryDataset getDataSet() { 
     DefaultCategoryDataset ds = new DefaultCategoryDataset(); 
     ds.addValue(59, "Country", "Norway"); 
     ds.addValue(69, "Country", "Switzerland"); 
     ds.addValue(85, "Country", "France"); 
     ds.addValue(93, "Country", "Syria"); 
     ds.addValue(96, "Country", "Germany"); 
     ds.addValue(111, "Country", "China"); 
     ds.addValue(116, "Country", "Australia"); 
     ds.addValue(121, "Country", "Egypt"); 
     ds.addValue(129, "Country", "England & Wales"); 
     ds.addValue(157, "Country", "New Zealand"); 
     ds.addValue(205, "Country", "Chile"); 
     ds.addValue(229, "Country", "Iran"); 
     ds.addValue(359, "Country", "Singapore"); 
     ds.addValue(404, "Country", "South Africa"); 
     ds.addValue(405, "Country", "Ukraine"); 
     ds.addValue(750, "Country", "USA"); 
     return ds; 
    }  


    public static void main(String[] args) { 
     BarWithMargin demo = new BarWithMargin("Bar with margin"); 
     demo.pack(); 
     RefineryUtilities.centerFrameOnScreen(demo); 
     demo.setVisible(true); 
    } 
} 
+0

+1 (http://sscce.org/); minor nit-'setItemLabelsVisible()'은 더 이상 사용되지 않습니다. – trashgod

+1

ok, ok ... 나는 더 이상 사용되지 않는 경고를 클릭하는 것을 게으른 것이 었습니다 ...'setBaseItemLabelsVisible'으로 대체했습니다. – kiwiwings

+0

고마워요. 그러나 내 목표는 이미지가 보여 주듯이, x 축을 상한으로 설정하고 동시에 여백을 갖는 것을 좋아하지 않는다는 것입니다. 그리고 나는 당신의 솔루션이 이것을 커버한다고 생각하지 않습니까? 상한선을 850으로 설정하여 해결 한 다음 850을 덮는 직사각형을 만듭니다. 작동하지만 조금 못생긴 해결책입니다. – Grains