Java 如何更改JFreeChart';s码

Java 如何更改JFreeChart';s码,java,swing,layout,jfreechart,sizing,Java,Swing,Layout,Jfreechart,Sizing,我在JPanel(使用BorderLayout)中添加了一个JFreeChart),它非常庞大。我能做些什么使它变小吗 public void generateChart() { DefaultCategoryDataset dataset = new DefaultCategoryDataset(); //set the values of the chart for(int i=0; i<8; i++) { dataset.setVal

我在
JPanel
(使用
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);
}   
public void generateChart()
{
DefaultCategoryDataset数据集=新的DefaultCategoryDataset();
//设置图表的值

对于(int i=0;i请尝试设置图表所在面板的大小

您可能需要同时设置JPanel middle和ChartPanel cp

当您创建时,有几个选项会影响结果:

  • 接受
    默认宽度
    默认高度
    :680 x 420

  • 在构造函数中指定首选的
    宽度
    高度

  • 如果需要,显式调用
    setPreferredSize()

  • 重写
    getPreferredSize()
    以动态计算大小

    @Override
    public Dimension getPreferredSize() {
        // given some values of w & h
        return new Dimension(w, h);
    }
    
  • 选择将添加
    ChartPanel
    的容器的默认布局。请注意,
    JPanel
    的默认布局为
    FlowLayout
    ,而
    JFrame
    的默认布局为
    BorderLayout
    。作为一个具体示例,在构造函数中使用首选值,在容器中使用
    GridLayout
    r以允许动态调整大小

  • 除了@trashgood的答案“4”之外,我还遇到了同样的问题,并设法解决了这个问题: (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);
                }
            };
        }
    }
    
    导入java.awt.Dimension;
    导入java.util.ArrayList;
    导入javax.swing.JFrame;
    导入javax.swing.JPanel;
    导入org.jfree.chart.ChartFactory;
    导入org.jfree.chart.ChartPanel;
    导入org.jfree.chart.JFreeChart;
    导入org.jfree.data.general.DefaultPieDataset;
    公共类MyPieChart扩展了JPanel{
    公共静态void main(字符串[]args){
    例1();
    例2();
    例3();
    }
    公共静态无效示例1(){
    JPanel面板=新的JPanel();
    面板.立根(50,80,100,100);
    MyPieChart piePanel=新的MyPieChart(“示例1”,dataset(),panel);
    面板。添加(面板);
    JFrame=新JFrame();
    frame.setLayout(空);
    框架.立根(10,10,200,300);
    框架。添加(面板);
    frame.setVisible(true);
    }
    公共静态无效示例2(){
    MyPieChart Piecanel=新的MyPieChart(“示例2”,dataset(),30,50,100,100);
    JFrame=新JFrame();
    frame.setLayout(空);
    机架立根(210、10、200、300);
    框架。添加(面板);
    frame.setVisible(true);
    }
    公共静态无效示例3(){
    MyPieChart Piecanel=新的MyPieChart(“示例3”,dataset(),100100);
    面板设置位置(0,0);
    JFrame=新JFrame();
    frame.setLayout(空);
    框架.立根(410,10,200,300);
    框架。添加(面板);
    frame.setVisible(true);
    }
    静态ArrayList数据集(){
    ArrayList数据集=新的ArrayList();
    添加(行(“Tom”、“LoggedIn”、“西班牙”);
    添加(行(“杰里”、“LoggedOut”、“英格兰”);
    添加(行(“Gooffy”、“LoggedOut”、“France”);
    返回数据集;
    }
    静态ArrayList行(字符串名称、字符串实际状态、字符串国家){
    ArrayList行=新的ArrayList();
    行添加(名称);行添加(实际状态);行添加(国家);
    返回行;
    }
    ArrayList数据集;
    DefaultPieDataset=新的DefaultPieDataset();
    int宽度、高度、posX、posY;
    int colState=1;
    字符串标题;
    字符串LoggedIn=“LoggedIn”;
    字符串LoggedOut=“LoggedOut”;
    公共MyPieChart(字符串标题、ArrayList数据集、int…args){
    如果(参数长度==2){
    this.width=args[0];
    this.height=args[1];
    此.setSize(宽度、高度);
    }
    else if(args.length==4){
    this.posX=args[0];
    this.posY=args[1];
    this.width=args[2];
    this.height=args[3];
    此.setBounds(posX、posY、宽度、高度);
    }
    否则{
    System.err.println(“错误:大小/位置参数数量错误”);
    返回;
    }
    this.title=标题;
    this.dataset=数据集;
    添加(chart());
    }
    公共MyPieChart(字符串标题、ArrayList数据集、JPanel面板){
    this.title=标题;
    this.dataset=数据集;
    this.width=panel.getWidth();
    this.height=panel.getHeight();
    this.setBounds(panel.getBounds());
    添加(chart());
    }
    图表面板图表(){
    int totaloggedin=0;
    int totaloggedout=0;
    for(ArrayList用户:数据集){
    if(user.get(colState).equals(LoggedIn))totaloggedin++;
    else totaloggedout++;
    }
    pieDataset.clear();
    设置值(LoggedIn+:“+totaloggedin,totaloggedin);
    设置值(LoggedOut+“:”+totaloggedout,totaloggedout);
    JFreeChart chart=ChartFactory.createPieChart(标题、pieDataset、false、false、false);
    returnnewchartpanel(chart){//这是管理将图表大小设置为面板的技巧!
    公共维度getPreferredSize(){
    返回新尺寸(宽度、高度);
    }
    };
    }
    }
    

    我真的希望它能帮上忙!

    我的饼图也有一个问题,边界布局太大了。我最终通过将图表转换为图像解决了问题

    之前

    之后

    代码

     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));
        }
    

    如果合适的话,从我在大多数的(被接受的)回答中看到的,这是不合适的;好的,我知道你在这里做了什么。