2013-05-20 62 views
2

是否有可能檢索BarChart的所有rangeAxis值?我已經設法吸引新的網格線(標記)是這樣的:上面的網格線JFreeChart中的圖表

enter image description here

但我需要知道哪些是能夠吸引所有行是pressent在價值座標軸上的值。任何想法如何獲得「值」軸?(RangeAxis)

public class BarChartDemo extends ApplicationFrame { 

    /** 
    * Creates a new demo instance. 
    * 
    * @param title the frame title. 
    */ 
    public BarChartDemo(final String title) { 
     super(title); 
     final CategoryDataset dataset = createDataset(); 
     final JFreeChart chart = createChart(dataset); 
     final ChartPanel chartPanel = new ChartPanel(chart); 
     chartPanel.setPreferredSize(new Dimension(500, 270)); 
     setContentPane(chartPanel); 
    } 

    /** 
    * Returns a sample dataset. 
    * 
    * @return The dataset. 
    */ 
    private CategoryDataset createDataset() { 

     // row keys... 
     final String series1 = "First"; 

     // column keys... 
     final String category1 = "Category 1"; 
     final String category2 = "Category 2"; 
     final String category3 = "Category 3"; 

     // create the dataset... 
     final DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 

     dataset.addValue(3.5, series1, category1); 
     dataset.addValue(4.0, series1, category2); 
     dataset.addValue(3.0, series1, category3); 
     return dataset; 
    } 

    /** 
    * Creates a sample chart. 
    * 
    * @param dataset the dataset. 
    * 
    * @return The chart. 
    */ 
    private JFreeChart createChart(final CategoryDataset dataset) { 

     // create the chart... 
     final JFreeChart chart = ChartFactory.createBarChart(
      "Bar Chart Demo",   // chart title 
      "Category",    // domain axis label 
      "Value",     // range axis label 
      dataset,     // data 
      PlotOrientation.VERTICAL, // orientation 
      false,      // include legend 
      false,      // tooltips? 
      false      // URLs? 
     ); 
     CategoryPlot plot = chart.getCategoryPlot(); 
     plot.getDomainAxis().setCategoryMargin(.01); 

     plot.setRangeGridlinesVisible(false); 
     for(int i=1; i<=4; i++){ 
      Marker marker = new ValueMarker(i); 
      marker.setStroke(new BasicStroke(
        1.0f, BasicStroke.CAP_ROUND, BasicStroke.JOIN_ROUND, 
        1.0f, new float[] {3.0f, 8.0f}, 0.0f 
       )); 
      marker.setPaint(new Color(224,224,224)); 
      plot.addRangeMarker(marker); 
     } 

     return chart; 
    } 

    public static void main(final String[] args) { 
     final BarChartDemo demo = new BarChartDemo("Bar Chart Demo"); 
     demo.pack(); 
     demo.setVisible(true); 
    } 
} 
+0

你試過啓用跟蹤,[這裏]所示(http://stackoverflow.com/a/5522583/230513)? – trashgod

+0

嘗試過,但它似乎是基於「ChartPanel」,它似乎是一個JPanel。但我想直接用iText將它繪製成pdf。 – Grains

+0

請編輯您的問題以澄清此用法,幷包含一個[sscce](http://sscce.org/),其中顯示您嘗試使用的「標記」;請參閱演示示例。 – trashgod

回答

0

看起來你能夠在類似這樣的領域example一個辦法,把槓桿addRangeMarker()所有的價值。匹配標記和刻度單位的一個方法是指定一個TickUnitSource,包括您所選擇的標記:

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

如果您需要自定義源,您可以將其在createIntegerTickUnits()createStandardTickUnits()建模。

0

沒有設法理解如何以「好」的方式做到這一點。做了步法知道往哪裏放線當前圖表最大值:

private static double getGridStep(double max){ 
     double step = 0; 
     if(max > 86){ 
      step = 10; 
     } 
     else if(max >= 30){ 
      step = 5; 
     } 
     else if(max > 17){ 
      step = 2.5; 
     } 
     else if(max > 7){ 
      step = 1; 
     } 
     else if(max > 2){ 
      step = 0.5; 
     } 
     else if(max > 1){ 
      step = 0.25; 
     } 
     else 
      step = 0.1; 
     return step; 
}