Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
217 views
in Technique[技术] by (71.8m points)

java - How do I change a JFreeChart's size

I've added a JFreeChart to a JPanel (using a BorderLayout), and it's huge. Is there something I can do to make it smaller?

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);
}   
Question&Answers:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

When you create your ChartPanel, you have several options that affect the result:

  1. Accept the DEFAULT_WIDTH and DEFAULT_HEIGHT: 680 x 420.

  2. Specify the preferred width and height in the constructor.

  3. Invoke setPreferredSize() explicitly if appropriate.

  4. Override getPreferredSize() to calculate the size dynamically.

    @Override
    public Dimension getPreferredSize() {
        // given some values of w & h
        return new Dimension(w, h);
    }
    
  5. Choose the layout of the container to which the ChartPanel will be added. Note that the default layout of JPanel is FlowLayout, while that of JFrame is BorderLayout. As a concrete example, ThermometerDemo uses both preferred values in the constructor and a GridLayout for the container to allow dynamic resizing.

image


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...