2012-04-23 120 views
3

我添加了一個JFreeChartJPanel(使用BorderLayout),這是巨大的。我能做些什麼來縮小它?如何更改一個JFreeChart的大小

public void generateChart() 
{ 
    DefaultCategoryDataset dataset = new DefaultCategoryDataset(); 

    //set the values of the chart 
    for(int i=0; i<8; i++) 
    { 
     dataset.setValue(income_array[i], "Income", 
      Double.toString(percent_array[i])); 
    } 

    JFreeChart chart = ChartFactory.createBarChart(
     "Required Annual Income for a Variety of Interest Rates", 
     "Percent", "Income", dataset, PlotOrientation.VERTICAL, 
     false,true, false); 
    ChartPanel cp = new ChartPanel(chart); 

    chart.setBackgroundPaint(Color.white); 
    chart.getTitle().setPaint(Color.black); 
    CategoryPlot p = chart.getCategoryPlot(); 
    p.setRangeGridlinePaint(Color.blue); 

    //cp.setMaximumDrawHeight(5); 
    //cp.setMaximumDrawWidth(5); 
    //cp.setZoomOutFactor(.1); 
    JPanel graph = new JPanel(); 
    graph.add(cp); 
    middle.add(graph, BorderLayout.CENTER); 
} 

回答

8

當您創建ChartPanel,你有一個影響結果的幾個選項:

  1. 接受DEFAULT_WIDTHDEFAULT_HEIGHT:680 * 420

  2. 指定構造函數中的首選widthheight

  3. 如果appropriate明確調用setPreferredSize()

  4. 重寫getPreferredSize()來動態計算大小。

    @Override 
    public Dimension getPreferredSize() { 
        // given some values of w & h 
        return new Dimension(w, h); 
    } 
    
  5. 選擇容器的layout到的ChartPanel將被添加。請注意,JPanel的默認佈局是FlowLayout,而JFrame的默認佈局是BorderLayout。作爲一個具體示例,ThermometerDemo在構造函數中使用了首選值,並在容器中使用了GridLayout以允許動態調整大小。

image

0

嘗試設置面板圖表中的大小。

您可能需要同時設置的JPanel中,ChartPanel CP

0

除了回答 「4」 @trashgod的,我有同樣的問題,並設法解決這個問題是這樣的: (1)創建其擴展JPanel 的自定義類(2 )得到的大小在某種程度上,你想傳遞給你的圖表 (3)創建它返回一個「ChartPanel」對象這樣的方法:

ChartPanel chart() { 
    //... custom code here 
    JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false);`enter code here` 
    // Now: this is the trick to manage setting the size of a chart into a panel!: 
    return new ChartPanel(chart) { 
     public Dimension getPreferredSize() { 
      return new Dimension(width, height); 
     } 
    }; 
} 

我準備了一個SSCCE,讓你知道它是如何工作:

import java.awt.Dimension; 
import java.util.ArrayList; 
import javax.swing.JFrame; 
import javax.swing.JPanel; 
import org.jfree.chart.ChartFactory; 
import org.jfree.chart.ChartPanel; 
import org.jfree.chart.JFreeChart; 
import org.jfree.data.general.DefaultPieDataset; 

public class MyPieChart extends JPanel { 

    public static void main(String[] args) { 
     example1(); 
     example2(); 
     example3(); 
    } 

    public static void example1() { 
     JPanel panel = new JPanel(); 
     panel.setBounds(50, 80, 100, 100); 
     MyPieChart piePanel = new MyPieChart("Example 1", dataset(), panel); 
     panel.add(piePanel); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(10, 10, 200, 300); 
     frame.add(panel); 
     frame.setVisible(true); 
    } 

    public static void example2() { 
     MyPieChart piePanel = new MyPieChart("Example 2", dataset(), 30, 50, 100, 100); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(210, 10, 200, 300); 
     frame.add(piePanel); 
     frame.setVisible(true); 
    } 

    public static void example3() { 
     MyPieChart piePanel = new MyPieChart("Example 3", dataset(), 100, 100); 
     piePanel.setLocation(0,0); 
     JFrame frame = new JFrame(); 
     frame.setLayout(null); 
     frame.setBounds(410, 10, 200, 300); 
     frame.add(piePanel); 
     frame.setVisible(true); 
    } 

    static ArrayList<ArrayList<String>> dataset() { 
     ArrayList<ArrayList<String>> dataset = new ArrayList<ArrayList<String>>(); 
     dataset.add(row("Tom", "LoggedIn", "Spain")); 
     dataset.add(row("Jerry", "LoggedOut", "England")); 
     dataset.add(row("Gooffy", "LoggedOut", "France")); 
     return dataset; 
    } 

    static ArrayList<String> row(String name, String actualState, String country) { 
     ArrayList<String> row = new ArrayList<String>(); 
     row.add(name); row.add(actualState); row.add(country); 
     return row; 
    } 

    ArrayList<ArrayList<String>> dataset; 
    DefaultPieDataset pieDataset = new DefaultPieDataset(); 
    int width, height, posX, posY; 
    int colState = 1; 
    String title; 
    String LoggedIn = "LoggedIn"; 
    String LoggedOut = "LoggedOut"; 

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, int...args) { 

     if(args.length==2) { 
      this.width = args[0]; 
      this.height = args[1]; 
      this.setSize(width, height); 
     } 
     else if(args.length==4) { 
      this.posX = args[0]; 
      this.posY = args[1]; 
      this.width = args[2]; 
      this.height = args[3]; 
      this.setBounds(posX, posY, width, height); 
     } 
     else { 
      System.err.println("Error: wrong number of size/position arguments"); 
      return; 
     } 

     this.title = title; 
     this.dataset = dataset; 
     this.add(chart()); 
    } 

    public MyPieChart(String title, ArrayList<ArrayList<String>> dataset, JPanel panel) { 
     this.title = title; 
     this.dataset = dataset; 
     this.width = panel.getWidth(); 
     this.height = panel.getHeight(); 
     this.setBounds(panel.getBounds()); 
     this.add(chart()); 
    } 

    ChartPanel chart() { 

     int totalLoggedIn = 0; 
     int totalLoggedOut = 0; 

     for(ArrayList<String> user : dataset) { 
      if(user.get(colState).equals(LoggedIn)) totalLoggedIn++; 
      else totalLoggedOut++; 
     } 
     pieDataset.clear(); 
     pieDataset.setValue(LoggedIn +": "+ totalLoggedIn, totalLoggedIn); 
     pieDataset.setValue(LoggedOut +": "+ totalLoggedOut, totalLoggedOut); 

     JFreeChart chart = ChartFactory.createPieChart(title, pieDataset, false, false, false); 

     return new ChartPanel(chart) { // this is the trick to manage setting the size of a chart into a panel! 
      public Dimension getPreferredSize() { 
       return new Dimension(width, height); 
      } 
     }; 
    } 
} 

我真的希望它有幫助!

0

我的餅圖太大,BorderLayout也有問題。我最終通過將圖表轉換爲圖像來解決我的問題。

以前 enter image description here

enter image description here

代碼

private PieDataset updateCSFDataSet(){ 
     DefaultPieDataset dataSet = new DefaultPieDataset(); 
      dataSet.setValue("Clear(" + clearCount + ")" , clearCount); 
      dataSet.setValue("Smoky(" + smokyCount + ")", smokyCount); 
      dataSet.setValue("Foggy(" + foggyCount + ")", foggyCount); 
      dataSet.setValue("Skipped(" + skipCount + ")", skipCount); 
      dataSet.setValue("Unlabeled(" + unlabeledCount + ")", unlabeledCount); 
     return dataSet; 
    } 

    private ImageIcon createChart(String title, PieDataset dataSet){ 
     JFreeChart chart = ChartFactory.createPieChart(
       title, 
       dataSet, 
       true, 
       false, 
       false 
     ); 

     PiePlot plot = (PiePlot) chart.getPlot(); 
     plot.setLabelFont(new Font("SansSerif", Font.PLAIN, 12)); 
     plot.setNoDataMessage("No data available"); 
     plot.setCircular(true); 
     plot.setIgnoreZeroValues(true); 
     plot.setLabelGap(0.02); 

     return new ImageIcon(chart.createBufferedImage(400,300)); 
    }