How to Change a Jfreechart'S Size

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

How to change marker size in XYPlot of jfreechart

You can achieve this by XYItemRenderer.setSeriesShape. Please refer to how DefaultDrawingSupplier .createStandardSeriesShapes() works.

    XYItemRenderer renderer = xyPlot.getRenderer();
renderer.setSeriesPaint(0, Color.blue);
double size = 20.0;
double delta = size / 2.0;
Shape shape1 = new Rectangle2D.Double(-delta, -delta, size, size);
Shape shape2 = new Ellipse2D.Double(-delta, -delta, size, size);
renderer.setSeriesShape(0, shape1);
renderer.setSeriesShape(1, shape2);

Reference: JFreeChart: Increase Size of Data Point

Set item shape size in jFreechart

You can override getItemShape() in your XYLineAndShapeRenderer to return any desired Shape, as shown here. Alternatively, invoke one of the abstract parent's methods, setSeriesShape(), setBaseShape() etc.

@Nicolas S.Xu reports using code similar to the following:

Rectangle rect = new Rectangle(2, 2);
renderer.setSeriesShape(1, rect);

See also ShapeUtilities.

How to change point size or shape in FastScatterPlot (JFreeChart)?

For speed, you might try the alternate FastScatterPlot calculation suggested in the render() method's source comments; profile to compare. For size, you can change the rendered size in the same method; the following would quadruple the size of each point.

g2.fillRect(transX, transY, 2, 2);

Java&JFreeChart - How to set a JFreeChart's ChartPanel resize with it's container ( say JPanel )?

Try my code and it works fine.

Note that :

I have one frame which contains a panelMain, A panelMain contains a subPanel and a subPanel contains ChartPanel.

frame.setLayout(new BoxLayout(frame.getContentPane(), BoxLayout.Y_AXIS));                      
JPanel panelMain = new JPanel(new GridLayout(0,2));
ChartPanel chartPanel = createChart();
JPanel subPanel = new JPanel(new BorderLayout());
subPanel.setBorder(BorderFactory.createTitledBorder("Consommation"));
subPanel.setPreferredSize(new Dimension(400, 200));
subPanel.add(chartPanel);


panelMain.add(subPanel);
frame.add(panelMain);
frame.pack();
frame.setLocationRelativeTo(null);
frame.setVisible(true);

And now when you resize your window application. Your chartPanel will resized automatically. Hope this helps.

Resize JFreeChart.ChartPanel on maximizing screen

Calling ChartPanel.setSize after ChartPanel.setPreferredSize and JFrame.validate did the job! See the new code:

frame = new JFrame();
frame.addComponentListener(new ComponentListener() {
@Override
public void componentResized(ComponentEvent e) {
// frame.repaint();
ChartPanel cp = (ChartPanel)panel.getComponents()[0];
cp.setPreferredSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
cp.setSize(new java.awt.Dimension(panel.getWidth(), panel.getHeight()));
// frame.invalidate();
frame.validate();
// frame.repaint();
}
...
});
...

Recompute JFreeChart display on ChartPanel resize

Solving Common Layout Problems suggests, "be sure that your component's container uses a layout manager that respects the requested size of the component." Your fragment uses BorderLayout.CENTER, which is a good choice: it allows the the ChartPanel to resize smoothly as the frame is resized, but it ignores the panel's minimum size. Eventually, resampling artifact and distortion appear. As you want to retain the resize behavior, one approach is to set the chart panel's minimum draw width and heigh to zero and (optionally) limit the frame's minimum size accordingly:

cp.setMinimumDrawWidth(0);
cp.setMinimumDrawHeight(0);
// optionally
f.setMinimumSize(new Dimension(
cp.getMinimumDrawWidth(),
cp.getMinimumDrawHeight()));

The draw width and heigh may also be specified in the constructor, as shown here. In the example below, note:

  • Override getPreferredSize() to establish the initial preferred size, also discussed here.

  • Construct and manipulate Swing GUI objects only on the event dispatch thread.

Preferred size:
initial

Smaller size:
smaller

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.JFrame;
import javax.swing.JLabel;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.data.category.CategoryDataset;
import org.jfree.data.category.DefaultCategoryDataset;

/**
* @see https://stackoverflow.com/q/69720552/230513
*/
public class ChartTest {

private static final int W = 320;
private static final int H = 240;

private void display() {
JFrame f = new JFrame("JFreeChart Resizing");
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

CategoryDataset ds = new DefaultCategoryDataset();
JFreeChart chart = ChartFactory.createLineChart(
"Chart Title", "X axis", "Y axis", ds);
ChartPanel cp = new ChartPanel(chart) {
@Override
public Dimension getPreferredSize() {
return new Dimension(W, H);
}
};
f.add(cp, BorderLayout.CENTER);
f.add(new JLabel("Java v" + System.getProperty("java.version")
+ "; JFreeChart 1.5.3", JLabel.CENTER), BorderLayout.PAGE_END);
cp.setMinimumDrawWidth(0);
cp.setMinimumDrawHeight(0);
f.setMinimumSize(new Dimension(cp.getMinimumDrawWidth(), cp.getMinimumDrawHeight()));
f.pack();
f.setLocationRelativeTo(null);
f.setVisible(true);
}

public static void main(String[] args) {
EventQueue.invokeLater(new ChartTest()::display);
}
}


Related Topics



Leave a reply



Submit