Home > Java > javaTutorial > How Can I Customize JFreeChart's Size within a JPanel?

How Can I Customize JFreeChart's Size within a JPanel?

DDD
Release: 2024-12-14 08:40:12
Original
814 people have browsed it

How Can I Customize JFreeChart's Size within a JPanel?

Customizing JFreeChart Size for a JPanel

In a JPanel, JFreeChart's default size may not always be optimal. Here's how you can customize the size:

1. Specify Preferred Size in Constructor:

JPanel graph = new JPanel(new GridLayout());
graph.add(new ChartPanel(chart, 500, 300));
Copy after login

2. Invoke setPreferredSize()

ChartPanel cp = new ChartPanel(chart);
cp.setPreferredSize(new Dimension(500, 300));
Copy after login

3. Override getPreferredSize()

@Override
public Dimension getPreferredSize() {
    return new Dimension(w, h);
}
Copy after login

4. Choose Container Layout

JPanel's FlowLayout is not ideal for resizing, whereas BorderLayout in JFrame allows dynamic resizing.

5. Other Methods

  • setMaximumDrawHeight() and setMaximumDrawWidth() limit the chart's drawable area.
  • setZoomOutFactor() shrinks the chart by a factor.

Example:

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, 500, 300);

    chart.setBackgroundPaint(Color.white);
Copy after login

The above is the detailed content of How Can I Customize JFreeChart's Size within a JPanel?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template