2013-05-05 127 views
1

當最大值是一個固定值時,是否有可能使所有位於條形圖外部的巴特圖都可見?JFreeChart中的利差圖和圖表

它看起來如何以及我希望看起來如何的示例。它不應該是超過750(檢查美國,最後一排)的任何值:

JFreechart with and without margins

任何提示或想法,如果it's也許可能使情節和圖形,使所有標籤之間的緣可見(或其他解決方案)?

回答

1

更新:空白邊緣(在範圍軸沒有值)

好了,我會用NumberAxis各地保持setUpperBound(),但一塌糊塗。

這裏是完整的代碼:

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爲[SSCCE](http://sscce.org/);次要的nit-'setItemLabelsVisible()'被棄用。 – trashgod 2013-05-05 23:03:41

+1

好吧,好吧...我懶得實際點擊不推薦使用的警告......用'setBaseItemLabelsVisible'代替它。 – kiwiwings 2013-05-05 23:34:38

+0

謝謝。但是我的目標是,如圖所示,我不希望x軸設置爲同一時間的上界和有餘量。我不認爲你的解決方案涵蓋了這個? 通過將上限設置爲850來解決此問題,然後製作一個涵蓋850的矩形。但Works有點難看。 – Grains 2013-05-06 08:14:34