How do I change a JFreeChart’s size

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

Leave a Comment